Skip to content
Advertisement

Delete files older than X minutes

I would like to delete files that are older than 59 minutes. I have this so far:

find /tmp -daystart -maxdepth 1 -mmin +59 -type f -name "*.*" -exec rm -f {} ;

This doesn’t work and seems to delete all files. I’ve tested this several times and I think the issue is to do with daystart.

I’ve read the man page and it seems to base time on the beginning of the day rather than from 24 hours ago. If this is the case how can I accurately delete files that are older than 59 minutes? Do I need to account for daystart and add some more minutes?

Example:

ubuntu@ip-10-138-30-118:/tmp$ ls -la
total 8
drwxrwxrwt  2 root   root   4096 Jul 20 14:39 ./
drwxr-xr-x 23 root   root   4096 Jun 25 18:34 ../
-rw-rw-r--  1 ubuntu ubuntu    0 Jul 20 12:35 a.txt

Both the following commands, return the file:

ubuntu@ip-10-138-30-118:/tmp$ find /tmp -daystart -maxdepth 1 -mmin +59 -type f -name "*.*"
/tmp/a.txt

And:

ubuntu@ip-10-138-30-118:/tmp$ find /tmp -daystart -maxdepth 1 -mmin +359 -type f -name "*.*"
/tmp/a.txt

However, the file is not older than 659 minutes (10.9 hours)! But at 759 (12.65 hours), it doesn’t return the file anymore?

Advertisement

Answer

When used with -mmin, -daystart appears to make it calculate from the end of today, not the beginning.

If you just want to find files modified more than 59 minutes ago, you don’t need that option. -mmin calculates from the current time by default.

barmar@dev:~/testdir$ date
Sat Jul 20 10:02:20 CDT 2013
barmar@dev:~/testdir$ ls -l
total 0
-rw-r--r-- 1 barmar adm 0 Jul 20 09:57 a.txt
barmar@dev:~/testdir$ find . -maxdepth 1 -mmin +2 -type f
./a.txt
barmar@dev:~/testdir$ find . -maxdepth 1 -mmin +10 -type f
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement