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

Didn't quite get this...

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.

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.
--------------------------------------------------------------------------------

What are the files called? Can you keep them in a seperate save directory?

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.

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.

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.

The above logic makes (rather unreasonable) assumption that the time stamps are exactly 7:30pm and 8pm for the 1st and 3rd files, which may not be possible practically... though, you can overcome this by increaing the time range by few minute on either ends...

Yes the best way would be (as Perderabo suggests) to create the "latest" 3 files in a separate directory, instead of cluttering them with unwanted files.. it would amount to a simple logic using date command...