How to delete files older than X hours


Question

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
180
10/30/2008 8:31:20 AM

Accepted Answer

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.

281
11/11/2018 8:09:42 AM

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon