Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Find difference with mtime - and +

Writer Andrew Henderson

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

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

-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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy