8

What is the difference with mtime's - and + switches as both are not bringing back the results I need?

I'm looking to delete all files older than 5 days:

 find /mnt/sdb1/tmp/ -type f -mtime +5 -exec ls {} \;
 find /mnt/sdb1/tmp/ -type f -mtime -5 -exec ls {} \;

I've changed the output to ls to compare the results.

2 Answers 2

14

From find's man page:

    Numeric arguments can be specified as

   +n     for greater than n,
   -n     for less than n,
    n     for exactly n.

  -mtime n
          File's data was last modified n*24 hours ago.  See the comments for 
          -atime to understand how rounding  affects  the  interpretation  of
          file  modification times.

   -atime n
          File was last accessed n*24 hours  ago.   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, -mtime +5 will find those files last modified more than 5*24h ago and -mtime -5 will find those files last modified less than 5*24h ago. To delete files that are older than 5 days1 you would do:

find /mnt/sdb1/tmp/ -type f -mtime +5 -exec rm {} \;

If this is not returning the result you want, there may be a problem with the timestamp. Is it correctly reported for the files in question? If this is an external USB drive, the files may have been created on another machine and have a different timstamp than what you expect.


1Note that the unit here is a day, 24 hours. So more than 5 days old means at least 6 days old since the value is always rounded and fractional parts ignored.

2
  • 1
    No, the statement about -mtime +5 contradicts Gilles's answer (cross site). It would be "more than 6*24h" (+1 effect for whatever reason (division and rounding down?)). Aug 9, 2018 at 7:53
  • @PeterMortensen first of all, please don't start statements with "No". It makes them needlessly confrontational, and makes your point harder to get across. That said, I don't see the contradiction. -mtime +5 will find files modified more than 5 days ago, so at least 6 days ago. That's what I say and what Gilles sais and, more importantly, what the manpage and a simple test will show: touch -d '6 days ago' file; find -mtime +5. Next time, test something instead of attacking. But OK, I have made the answer clearer.
    – terdon
    Aug 10, 2018 at 11:20
1

-mtime +5 should show you all files modified 5 days and before (6, 7, ...), while -5 should show the ones modified today up to 5 days ago.

1
  • No, the statement about -mtime +5 contradicts Gilles's answer (cross site). It would be "modified 6 days and before (7, 8, ...)" (+1 effect for whatever reason (division and rounding down?)). Aug 9, 2018 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .