8

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

Nishat
  • 353
  • 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
    http://unix.stackexchange.com/questions/92346/why-does-find-mtime-1-only-return-files-older-than-2-days – AReddy Jan 27 '16 at 10:14
  • @ 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
  • Please use the formatting tools to format your question properly. It's hard to understant what command produced what output. – terdon Jan 27 '16 at 11:09
  • I am a beginner with Stack exchange ...Next time i will use formatting tools format my question. Thanks for your advice. @ terdon – Nishat Jan 27 '16 at 11:34

1 Answers1

16

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.

Otheus
  • 6,138