Meaning of GNU find options: -mtime, -ctime, -atime

VirginSnow at vfemail.net VirginSnow at vfemail.net
Thu Apr 2 19:35:54 EDT 2009


A question was raised recently on the list about the exact meaning of
arguments to GNU find's -mtime test.  While I've been able to get by
for years without understanding the exact semantics, I decided was for
me to "dig in" and learn how exactly this works.

Here's what I found (no pun intended).  If anyone finds any mistakes
in this description, please holler... loudly. :)

Without "-daystart", and without "+"/"-":

  Without "-daystart", the "-mtime" test compares a file's mtime
  (modification time) to the current system time.  The difference
  between the current time and mtime (in that order: time - mtime),
  expressed as a floating-point number of days, is floored to yield an
  integer value (using floor, not trunc).  If the argument to -mtime
  is exactly that integer (without a "+" or "-" prefix) the test
  returns true.

  For example:

  $ find /test -type f -mtime 0

  will find files modified from 24 hours ago to the present moment.

  $ find /test -type f -mtime 1

  will find files modified from 48 hours to 24 hours ago.  Etc.

With "+"/"-" prefix:

  When the argument to -mtime is prefixed with a "+" or "-" sign, an
  inequality comparison between the calculated and supplied integers
  is performed.  "+" indicates that the calculated value must be
  "greater than" the argument in order for the test to succeed, and
  "-" indicates that the value must be "less than" the argument.
  These comparisons are exclusive, not inclusive.  That is: "+" means
  "greater than", not "greater than or equal to".  And "-" means "less
  than", not "less than or equal to".  If the floored integer
  calculated above satisifies the inequality, the test returns true.

  For example, both:

  $ find /test -type f -mtime +0 -mtime -2

  $ find /test -type f -mtime 1

  will match the same set of files (those modified from 24 hours ago
  to the present moment) because 1 is the only integer greater than 0
  and less than 2.

  These inequality tests work even when the integer calculated is
  negative.  The calculated integer will be negative IF AND ONLY IF
  the file's mtime is in the future:

  $ find /test -type f -mtime -0 # match only negative values

  matches all files with mtimes in the future.

  Similarly,

  $ find /test -type f -mtime -1

  will match all files with mtimes from 24 hours ago to the present
  moment, as well as any mtimes in the future.

With "-daystart":

  When the "-daystart" option is supplied, the effective "current
  time" used for comparison is advanced to the first midnight in the
  future ("midnight tonight" as opposed to "midnight this morning").
  All file mtimes are then compared to this adjusted time, rather than
  to the actual current time.  All other option semantics remain the
  same.

  $ find /test -daystart -type f -mtime 0

  finds all files modified today.

  $ find /test -daystart -type f -mtime 1

  finds all files modified yesterday (the calendar day immediately
  prior to today).

  "+" and "-" inequalities can still be specified, and have the same
  meanings as they do without "-daystart".  With "-daystart", the
  integer for comparion is just computed with respect to the adjusted
  (midnight) time rather than the current time.

-ctime, -atime:

  The "-ctime" and "-atime" tests operate identically to the "-mtime"
  test, but test a file's ctime (inode change time) and atime (access
  time), respectively, rather than the file's mtime (modification
  time).  "+"/"-" comparisons can also be supplied, and "-daystart"
  has the same effect (advancing the effective current time to
  midnight) as it does with "-mtime".

In all other cases, the test returns false.

Whew!  I think that about covers it.

If anyone sees any obvious errors in this description... please DO
point them out.


More information about the gnhlug-discuss mailing list