Purge files, keep 3 versions of last 4 days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Purge files, keep 3 versions of last 4 days
# 1  
Old 10-28-2002
Question Purge files, keep 3 versions of last 4 days

Hello,

I currently generate a file every 15 minutes for 12 hours a day. I would like to clean the directory on a daily basis. I only want to keep the latest 3 versions for the last 4 days in the directory.

Any suggestions?

Thanks,
Barbara
# 2  
Old 10-28-2002
Didn't quite get this...

Quote:
I only want to keep the latest 3 versions for the last 4 days in the directory.
if you want to delete all those file which are not modified in the last 4 days...

first try listing the files which were not modified during the last 4 days using...

find <directory path> -mtime +4 -print

if you are satisfied with what it lists... then you can delete the above selection using...

find <directory path> -mtime +4 -exec rm -f {} \;

you can put this line in a executable script file and schedule it daily in cron...

Cheers!
Vishnu.
# 3  
Old 10-28-2002
Hello,
Some more information:
On Monday I create files (49) from 8:00am until 8:00pm - one file every 15 minutes. On Tuesday I do the same thing, create 49 files. On Wednesday, Thursday, Friday. etc. - I do the same thing - create 49 files daily. A cleanup job runs every night which should delete unwanted files.

If its Thursday, the cleanup job will keep the 3 latest files (7:30pm, 7:45pm 8:00pm) for Thursday, Wednesday, Tuesday and Monday.

I will end up with 12 files left in the directory on any given day.

Does this explain it better?

quote:
--------------------------------------------------------------------------------
I only want to keep the latest 3 versions for the last 4 days in the directory.
--------------------------------------------------------------------------------
# 4  
Old 10-28-2002
What are the files called? Can you keep them in a seperate save directory?
# 5  
Old 10-28-2002
a typical ls output looks like this...

-rw-r----- 1 ltaber wheel 1876 Mar 28 16:55 tmp

assuming the time stamp is the 8th field and file is 9 th field (check this out)

I don't have UNIX shell with me at present... but try listing with this loop...

----------------------------------------------------------

for lsout in `ls -l`
do

hours=`echo $lsout | cut -d " " -f 8 | cut -d ":" -f 1`
filetodelete=`echo $lsout | cut -d " " -f 9`

if [ $hours -lt 19 -o $hours -ge 20 ]
then
echo $filetodelete
fi

done

----------------------------------------------------------

I hope you got what I'm trying to do...

if the above code lists the files you wanted to delete ( you may need to make some modifications )... you can replace the echo with a "rm -f $filetodelete"

the above scheme will work if none your files are older than 6 months.. because ls -l will not list time stamp if a file is older than 6 months... also I assume that none of your files is having spaces in its name...

wish to see better ways for this...

Cheers!
Vishnu.

Last edited by Vishnu; 10-28-2002 at 03:34 PM..
# 6  
Old 10-28-2002
She wants to lose the 7:00pm and the 7:15pm files while keeping the 7:30pm amd the 7:45pm files. So just looking at the hour isn't going to fly.

If everything is absolutely guaranteed to be as she described, at the end of the day we will have exactly 61 files. If we list them from newest to oldest, we get:
1-3 last 3 files from day n
4-49 46 unwanted files from day n
49-51 3 files from day n-1
52-54 3 files from day n-2
55-47 3 files from day n-3
58-61 3 unwanted files from day n-4

If it's that easy, this is a one liner:
rm `ls -t | sed -n "4,46p;58,61p"`

I think this is a dangerous solution though. If the system goes down for a while, the numbering is off. That's why saving the files in another directory is attractive, we then don't need to delete files from the middle of a list. And simply keeping the last 3 files in a directory is very simple.
# 7  
Old 10-28-2002
this may help...

REVISED
---------------------------------------

for lsout in `ls -l`
do

hours=`echo $lsout | cut -d " " -f 8 | cut -d ":" -f 1`
minutes=`echo $lsout | cut -d " " -f 8 | cut -d ":" -f 2`

timeinminutes=`expr $hours \* 60 + $minutes`

filetodelete=`echo $lsout | cut -d " " -f 9`

if [ $timeinminutes -lt 1170 -o $timeinminutes -gt 1200 ]
then
echo $filetodelete
fi

done

---------------------------------------

19:30 will become 1170 and 20:00 will become 1200

Thank you Perderabo.. I hope this solve the 7:30, 7:45, 8:00 problem...

Cheers!
Vishnu.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Purge X old days files and Print count

Hello All, I am trying to purge X old days of files from directory & Sub directories ./2016-01-13/1500/abc.txt ./2016-01-14/1605/caf.txt ./2016-01-14/1605/caf2.txt ./2016-01-14/1606/eaf.txt ..... ./2017-08-1/1701/ Should also remove directories and sub directories too Expected... (7 Replies)
Discussion started by: krux_rap
7 Replies

2. Shell Programming and Scripting

To create a script and schedule which purge 30 days old files

Hi Friends, Very new in Unix and i got a requirement like writing a script and schedule it, so that it removes 30 days old files from all the log locations of a unix box. Suppose i have a unix server ltbamdev1 and in this server i have a mount point opt/bam. In this mount point i have 3... (1 Reply)
Discussion started by: duos
1 Replies

3. Shell Programming and Scripting

Purge files and archive

Hi Friends, I have an urgent requirement. I have many files huge in size which has occupied almost the entire disk space. The files are not being moved to the archived folder. But at present I need to purge those files, new to shell scripting, not sure how to proceed. Eg. Directory... (3 Replies)
Discussion started by: unx100
3 Replies

4. Shell Programming and Scripting

Script to delete older versions of unique files

I have directory where new sub directories and files being created every few minutes. The directories are like abc_date, def_date, ghi_date. I am looking to keep the latest 2 unique directories and delete everything else. Here is what I have so far This gives me unique names excluding the... (5 Replies)
Discussion started by: zzstore
5 Replies

5. Shell Programming and Scripting

Need a script to delete previous versions of files

Hi. I need a script (either bash or perl) that can delete previous versions of files. For instance, from our continuous build process I get directories such as build5_dev_1.21 build5_dev_1.22 build5_dev_1.23 build5_dev_1.24 I need a script that I can run every night (using "at"... (6 Replies)
Discussion started by: jbsimon000
6 Replies

6. Shell Programming and Scripting

Shell script to check the files hourly and purge

I'm new to shell scripting... i have been given a task.. can any one help in this regard.... 1) Check hourly for files in <destination-path><destination-file-template><destination-file-suffix> for files older than <destination-file-retention> days and purge. It should then check... (1 Reply)
Discussion started by: satishpabba
1 Replies

7. Shell Programming and Scripting

shell script preserving last 2 versions of files

I need some help with the logic and syntax for a shell script (ksh) that will search a directory and look for similar files and save only the last two versions. The version number is in the file name. However, the files are of varying name lengths and may have 1 or many files, with no limit to... (6 Replies)
Discussion started by: synergy_texas
6 Replies

8. Shell Programming and Scripting

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 = 29 Feb 2007 I need to delete all files... (3 Replies)
Discussion started by: sureshg_sampat
3 Replies

9. UNIX for Dummies Questions & Answers

Find and purge a files in a dir

HI All, I have recuirement to purge the files in a directory . In that directory i an having many sub-directory . When i use find command like find ~/work/test/insert -name "*.*" -mtime +12 it is listing the file not accesed before 12 , It also takes the subdirectories inside the... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

10. Shell Programming and Scripting

ls latest 4 days or specify days of files in the directory

Hi, I would like to list latest 2 days, 3 days or 4 days,etc of files in the directory... how? is it using ls? (3 Replies)
Discussion started by: happyv
3 Replies
Login or Register to Ask a Question