1

This question already has an answer here:

I'm trying to remove all files more than 1 days old.Before I execute a script to remove the files,I try to find the files using mtime. however, I face same problem with my command-

My Current date is

Wed Jan 27 11:49:20 BDT 2016

My file lists are-

Jan 25 15:11 25-01-2016.txt            
Jan 26 13:05 26-01-2016.txt    
Jan 27 02:30 27-01-2016.txt    
Jan 25 15:11 dfk-25-01-2016.txt    
Jan 26 13:05 dfk-26-01-2016.txt      
Jan 27 02:30 dfk-27-01-2016.txt 

I thought -mtime +1 was supposed to list all files over a day old.

find /etc/output/*.txt -mtime +1
find /etc/output/*.txt -mtime +0

/output/25-01-2016.txt
/output/dfk-25-01-2016.txt

find /etc/output/*.txt -mtime -1

/output/26-01-2016.txt
/output/27-01-2016.txt
/output/dfk-26-01-2016.txt
/output/dfk-27-01-2016.txt

My desired out output is as followings,

find /etc/output/*.txt -mtime +1

/output/25-01-2016.txt
/output/dfk-25-01-2016.txt

find /etc/output/*.txt -mtime +0

/output/26-01-2016.txt
/output/dfk-26-01-2016.txt
/output/25-01-2016.txt
/output/dfk-25-01-2016.txt

marked as duplicate by Anthon, Raphael Ahrens, chaos, don_crissti, Stephen Kitt Jan 27 '16 at 13:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • mtime +0 should work – 123 Jan 27 '16 at 10:01
  • 1
    I'm confused. Maybe with formatting fixes, I'd be less confused. But why do you expect -mtime +0 to show only the files for the 26th? Maybe you mean -mtime +0 -mtime -1? – Otheus Jan 27 '16 at 10:05
  • But I want to find files old more than 1 days. Using -mtime +1 should get the desired result. Unfortunately not got that... – Nishat Jan 27 '16 at 10:07
  • 7
  • @ Otheus : I have edit my desired output. Now your confusion should be solved... and ofcourse -mtime +0 should give me files for 25th and 26th. – Nishat Jan 27 '16 at 10:15
8

Two points: find "ignores fractional parts". I guess it calculates the number of hours, divides by 24, and integerizes the result (discards the fraction). So -mtime 0 checks a file, compares the mtimes, converts to hours, divides by 24. If the integer part of that result is 0, it's a match. That means 0.99999 hours ago will match. Then -mtime +0 matches any file whose mtime difference is at least 24 hours.

Second, if you want mtime to count calendar days, and not n-24 hour periods from now, use -daystart. So -daystart -mtime 0 means today and -daystart -mtime +0 means before today.

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