Script to keep todays files based on Timestamp

Hi i need to keep todays files based on timestamp and archive the remaining files

ex:

Managerial_Country_PRD_20150907.csv
Managerial_Country_PRD_20150907.csv
Managerial_Country_PRD_20150906.csv
 Managerial_Country_PRD_20150905.csv

What - except for double posting - have you tried so far?

1 Like

Hello ram1228,

Following may help you in same.

DATE=`date +%Y%m%d`
for i in *.csv
do
    if [[ $i == "Managerial_Country_PRD_"$DATE".csv" ]]
    then
          B=1
    else
          echo "mv $i"
    fi
done
 

You can put it into a script and execute it. Once you are happy with results you can remove echo above.

Thanks,
R. Singh

1 Like

IF I have two files with different names like

 Physical_Location_PRD_20150907.csv
 Managerial_Country_PRD_20150907.csv
Physical_Location_PRD_20150906.csv
 Managerial_Country_PRD_20150906.csv

then I want to keep todays files....

Hello Ram,

You could try following code then, may help you. Let me know if you have any queries.

DATE=`date +%Y%m%d`
for i in *.csv
do
    if [[ $i == *$DATE".csv" ]]
    then
          B=1
    else
          echo "mv $i"
    fi
done
 

EDIT: You can use as follows too find command.

DATE=`date +%Y%m%d`
find -type f -iname "*.csv" ! \( -name "*$DATE.csv" \) 2>/dev/null
 

Thanks,
R. Singh

Hi Rudic,

I have tried with -mtime , but never worked on timestamp in file .

-mtime in what context?
Howsoever, did RavinderSingh13's proposal solve your problem? Then please tag the thread "solved". If not, please describe in detail WHAT is missing.

---------- Post updated at 19:23 ---------- Previous update was at 19:17 ----------

BTW, you might want to modify the name/header of your thread to make it more readable.