0

I am using this command to list all files that have been modified in the last day (or created)

find ./ -mtime 1 -ls

However it keeps returning files from 30th April when I am running the command just now (2nd May at 19:38)

Can anyone advise why this is occurring and / or give me a better command to use to find files modified or create in the last 24 hours period

  • find . -mtime -1 -ls or preferably find . -mmin -1440 -ls – heemayl May 02 '15 at 18:47
  • That second one is much better for me.. and these both seem to work. Mysterious.

    Post that second one as the answer so I can accept it as it worked for me

    – MOLEDesign May 02 '15 at 18:50
  • 1
    The way that find interprets the digits is not intuitive. From man find (slightly modified): When find figures out how many 24-hour periods ago the file was last modified, any fractional part is ignored, so to match -mtime +1, a file has to have been modified *at least* two days ago. – gogoud May 02 '15 at 18:55

2 Answers2

3

To find files that have been modified a certain days ago, it is better to use -mmin instead of -mtime as the latter will ignore any fractional part. So, 1 day 23 hours is also treated as 1 day.

From man find:

-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.

In your case, the following will show the files that were modified in the last 24 hours i.e. 1440 mins:

find . -mmin -1440 -ls 
heemayl
  • 56,300
2

-mtime N means files whose age A in days satisfies NA < N+1. In other words, -mtime N selects files that were last modified between N and N+1 days ago. For example, -mtime 1 selects files that were modified between 1 and 2 days ago. To select files that were modified in the last day (as in, the last 24 hour period), use -mtime 0.

-mtime -N means files whose age A satisfies A < N, i.e. files modified less than N days ago. Less intuitively, -mtime +N means files whose age A satisfies N+1 ≤ A, i.e. files modified at least N+1 days ago.

If you find these rules hard to remember, use a reference file instead.

touch -d '1 day ago' cutoff
find . -newer cutoff

(The syntax “1 day ago” requires GNU touch.)