tags:

views:

2792

answers:

4

I'm writing a bash script that needs to delete old files.

It's currently implemented using :

find $LOCATION -name $REQUIRED_FILES -type f -mtime +1 -delete

This will delete of the files older than 1 day.

However, what if I need a finer resolution that 1 day, say like 6 hours old? Is there a nice clean way to do it, like there is using find and -mtime?

+1  A: 

-mmin is for minutes.

Try looking at the man page.

man find

for more types.

GavinCattell
+14  A: 

Does your find have the -mmin option? That can let you test the number of mins since last modification

find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete

Or maybe look at using tmpwatch to do the same job. phjr also recommended tmpreaper in the comments.

Paul Dixon
Thanks for the answers everyone, -mmin is exactly what I needed :) somehow I missed it in the man page.
Tom Feiner
Mine doesn't have -mmin :(
xtofl
tmpwatch is for you then!
Paul Dixon
I find tmpreaper better than tmpwatch - could you also include it in your answer? Thanks.
phjr
A: 

You could to this trick: create a file 1 hour ago, and use the -newer file argument.

(Or use touch -t to create such a file).

xtofl
Yeah, I found this trick in google, however -mmin is much more elegant.
Tom Feiner
A: 

for SunOS 5.10

 Example 6 Selecting a File Using 24-hour Mode


 The descriptions of -atime, -ctime, and -mtime use the  ter-
 minology n ``24-hour periods''. For example, a file accessed
 at 23:59 is selected by:


   example% find . -atime -1 -print




 at 00:01 the next day (less than 24 hours  later,  not  more
 than one day ago). The midnight boundary between days has no
 effect on the 24-hour calculation.
Rajeev Rumale