Purge files based on timestamp avl in file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Purge files based on timestamp avl in file name
# 1  
Old 02-29-2008
Purge files based on timestamp avl in file name

Dear All,

I have the followoing requirement..

REQ-1:
Suppose I have the following files
XX_20070202000101.zip
XX_20080223000101.zip
XX_20080226000101.zip
XX_20080227000101.zip
XX_20080228000101.zip
XX_20080229000101.zip

Suppose sysdate [today's date] = 29 Feb 2007
I need to delete all files older than 3 days from current sysdate

After my archival process,
i.e I will retain the files
XX_20080227000101.zip
XX_20080228000101.zip
XX_20080229000101.zip

AND DELETE The fololowing files
XX_20070202000101.zip
XX_20080223000101.zip
XX_20080226000101.zip

In my case , I need to compare the sysdate against the timestamp value which is avaialble in the file name [XX_YYYYMMDDHHMMSS.zip].

Can you please advice on teh same

Regards,
Suresh
# 2  
Old 02-29-2008
Here is a way:

First get the previous two days

Code:
# suppose today is 20080229 (date + %Y%m%d)
# then to get two days before you can write

yesterday1=`TZ=$TZ+24 date +%Y%m%d`
yesterday2=`TZ=$TZ+48 date +%Y%m%d`

# Then yesterday1 = 20080228  yesterday2 = 20080227

now you can perform a lot of things like
Code:
cp XX_${yesterday1}*.zip 1.zip
cp XX_${yesterday2}*.zip 2.zip

rm XX*

mv 1.zip XX_${yesterday1}*.zip
mv 2.zip XX_${yesterday2}*.zip


hope you get the point Smilie
# 3  
Old 02-29-2008
Hey ..

Thanks for the info
but in my development box

Both the commands below are showing the same results..can you please advice

comadm@czhs0255 /home/comadm/test
TZ=$TZ+48 date +%Y%m%d
20080229
comadm@czhs0255 /home/comadm/test
TZ=$TZ+24 date +%Y%m%d
20080229

Regards,
Suresh
# 4  
Old 02-29-2008
Code:
find /home/comadm/test -ctime +3 -exec rm -f {} \;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 (6 Replies)
Discussion started by: ram1228
6 Replies

2. UNIX for Dummies Questions & Answers

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_20150906.csv Managerial_Country_PRD_20150905.csv (2 Replies)
Discussion started by: ram1228
2 Replies

3. UNIX for Dummies Questions & Answers

Display files based on particular file timestamp

Hi, I have requirement to list out files that are created after particular file. ex. I have below files in my directory. I want to display files created after /dirdat/CG1/cg004440 file. ./dirdat/CG1/cg004438 09/07/14 0:44:05 ./dirdat/CG1/cg004439 09/07/14 6:01:48 ... (3 Replies)
Discussion started by: tmalik79
3 Replies

4. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

5. Shell Programming and Scripting

Urgent ...pls Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting. Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (1 Reply)
Discussion started by: robertbrown624
1 Replies

6. Shell Programming and Scripting

bash script to create txt files based on timestamp

Hi , please guide me for a bash script that will create a txt files and the name of the txt files will be as of timestamp so that each file name will be different from other and these files will be get created say after every 10 minutes in a folder(/home/p2000/sxs137), please guide me how would... (1 Reply)
Discussion started by: nks342
1 Replies

7. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

8. UNIX for Dummies Questions & Answers

How to pick only the latest files based on the timestamp?

I have a few log files which get generated on a daily basis..So, I need to pick only the ones which get generated for that particular day. -rw-r--r-- 1 staff 510732676 Apr 7 22:01 test.log040711 -rwxrwxrwx 1 staff 2147482545 Apr 7 21:30 test.log.2 -rwxrwxrwx 1 staff 2147482581 Apr 7 19:26... (43 Replies)
Discussion started by: win4luv
43 Replies

9. Shell Programming and Scripting

copy files based on creation timestamp

Dear friends.. I have the below listing of files under a directory in unix -rw-r--r-- 1 abc abc 263349631 Jun 1 11:18 CDLD_20110603032055.xml -rw-r--r-- 1 abc abc 267918241 Jun 1 11:21 CDLD_20110603032104.xml -rw-r--r-- 1 abc abc 257672513 Jun 3 10:41... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

10. Shell Programming and Scripting

copy lines from log files based on timestamp and sysdate

I'm looking for a command or simple script that will read lots of audit log file (*.aud) in log fold every 10 minutes, and will output to one file based on sysdate - 10 minutes. assume the script is run at 11:12:20, and it should grep the line from Wed Jun 17 11:02:43 2009 to end of file. after... (4 Replies)
Discussion started by: percvs88
4 Replies
Login or Register to Ask a Question