"find command -mtime 0" does not receive the file that I expect

I am trying to find a file with 0 days. Below are the steps that I followed to verify this.

$ ls $ ls -ltr total 0 $ touch tmp.txt $ ls -ltr total 0 -rw-r----- 1 tstUser tstUser 0 Feb 28 20:02 tmp.txt $ find * -mtime 0 $ $ find * -mtime -1 tmp.txt $ 

Why doesn't '-mtime 0' get me a file?

What is the exact difference between '-mtime 0' and '-mtime -1'?

I'm sure there should be other ways on unix to find files that are 0 days old, but they are interested to know how -mtime works.

+7
source share
3 answers
  -mtime n File data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times. 

So, -mtime 0 will be: "File data was last modified 0 hours ago. While -mtime 1 will be:" File data was last changed 24 hours ago "

Edit:

  Numeric arguments can be specified as +n for greater than n, -n for less than n, n for exactly n. 

So, I think -1 will be changed in the last 24 hours, while 1 will be exactly one day.

+5
source

This is not a user-friendly aspect of find - you need to understand how matching really works in order to correctly define search criteria. The following explanation is based on GNU search (findutils) 4.4.2.

find tests -atime , -ctime , -mtime run for 24-hour periods, so set the "file age" to

 floor (current_timestamp - file_modification_timestamp / 86400) 

Given the three files modified 1 hour ago, 25 hours ago, and 49 hours ago

 $ touch -t $(date -d "1 hour ago" +"%m%d%H%M") a.txt $ touch -t $(date -d "25 hours ago" +"%m%d%H%M") b.txt $ touch -t $(date -d "49 hours ago" +"%m%d%H%M") c.txt 

file ages (as defined above)

 $ echo "($(date +"%s") - $(stat -c %Y a.txt)) / 86400" | bc 0 $ echo "($(date +"%s") - $(stat -c %Y b.txt)) / 86400" | bc 1 $ echo "($(date +"%s") - $(stat -c %Y c.txt)) / 86400" | bc 2 

Given the above, here's what it finds

 $ find -type f -mtime 0 # find files with file age == 0, ie files modified less than 24 hours ago ./a.txt $ find -type f -mtime -1 # find files with file age < 1, ie files modified less than 24 hours ago ./a.txt $ find -mtime 1 # find files with file age == 1, ie files modified more than (or equal to) 24 hours ago, but less than 48 hours ago ./b.txt $ find -mtime +1 # find files with file age > 1, ie files modified more than 48 hours ago ./c.txt 

This shows that -mtime 0 and -mtime -1 give equivalent results.

-mmin gives the same test with finer granularity - the argument is minutes instead of 24-hour periods.

I cannot reproduce your problem using the above version of find

 $ touch tmp.txt $ find * -mtime 0 tmp.txt $ find * -mtime -1 tmp.txt 
+6
source

The meaning of these three possibilities is as follows:
n: exactly n 24-hour periods (days) ago, 0 means today.
+ n: "more than n 24-hour periods (days) ago", or older than n,
-n: less than n 24-hour periods (days) ago (-n), or less than n. Obviously, -1 and 0 are the same, and both mean today .

NOTE If you use parameters with the find command in scripts, be careful when the -mtime parameter is zero. Some (earlier) versions of GNU misinterpret the following Sourece expression: http://www.softpanorama.org/Tools/Find/index.shtml

0
source

All Articles