Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 776
Abs Avatar asked Jul 20 '13 14:07

Abs


People also ask

How do I delete 7 days old file in Linux?

-mtime +7 : only consider the ones with modification time older than 7 days. -execdir ... \; : for each such result found, do the following command in ... . rm -- '{}' : remove the file; the {} part is where the find result gets substituted into from the previous part.


2 Answers

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 
like image 186
Barmar Avatar answered Oct 04 '22 08:10

Barmar


this should work for you

find /path -mmin +59 -type f -exec rm -fv {} \;

like image 28
matson kepson Avatar answered Oct 04 '22 06:10

matson kepson