0

I've tried multiple ways of writing this and I'm executing the script myself so it isn't the crontab that isn't working. These are examples of what I've tried :

find /home/backups -mtime +1 -exec rm {} \;
find /home/backups/* -mtime +1 -exec rm {} \;
find /home/backups/ -mtime +1 -exec rm {} \;

I need the files to get deleted every day yet it just seems to not work. Running ls -ld /home/backups returns a long file list - These are the first two files

-rw-r--r-- 1 root root 8284346 Jan 12 13:00 arksave-2016-01-12--01-00-01.tar.gz
-rw-r--r-- 1 root root 8295428 Jan 12 13:15 arksave-2016-01-12--01-15-01.tar.gz

Note that is more than one day old, and these are the last two files

-rw-r--r-- 1 root root 38016124 Jan 13 12:30 arksave-2016-01-13--12-30-01.tar.gz
-rw-r--r-- 1 root root 38016163 Jan 13 12:45 arksave-2016-01-13--12-45-01.tar.gz

8
  • whats the problem though ?
    – 123
    Jan 13 '16 at 13:35
  • The files just dont get deleted
    – Batzz
    Jan 13 '16 at 13:36
  • 1
    What output do you get if you just run the finds, and drop the exec's for now. Also, show us the outputs of ls -l /home/backups and ls -ld /home/backups, and tell us which user is running the command. Jan 13 '16 at 13:38
  • 1
    When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago. - this also applies to mtime. Maybe that's your problem?
    – TNW
    Jan 13 '16 at 13:48
  • 1
    Yeah, that was my problem. I just found this link a second ago, i fixed it by using the code example the second answer gave $ find . -mmin +$((60*24))
    – Batzz
    Jan 13 '16 at 13:50
2

Quoting 'TNW'

When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

So to find a file that is only a day old, you can use either of the snippets below

find /home/backups/* -mtime +0

or

find . -mmin +$((60*24))

Not the answer you're looking for? Browse other questions tagged or ask your own question.