4

I want to search files whose creation date is prior than today.
I do :

find $HOME -type f -name "myFile_*.sql.zip" -mtime +1

but it doesn't return anything, it should at least return one file.

Where am I wrong?

6
  • Try this command: find $HOME -iname '*.sql.zip' -atime +1 Dec 11, 2015 at 8:10
  • IMO, your command should have worked if you had 1 file modified 1 day before. Check modified date of that again to make sure.
    – Tung Tran
    Dec 11, 2015 at 8:53
  • it doesn't work, is there a way to tell 'find' to look for the date that is shown when doing ls -al ? (i don't know if it is the creation/modification date) Dec 11, 2015 at 9:05
  • Well, any chance your file was created more than 24 hours before? According to man find: -mtime -n File’s data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times. I assume your file created at 1 am yesterday but now it's 3 am today, so the output your command would be correct. In this case, use -mtime +2 instead.
    – Tung Tran
    Dec 11, 2015 at 9:46
  • when id do ls -al, I am getting the date for this file : 'Dec 9'. I assume that -mtime +1 means : files that have been created at least one day ago. Is that right? If so, I should see this file with the find command. But I can only see a file with the date : 'Nov 20' Dec 11, 2015 at 10:03

1 Answer 1

7

find with -mtime +1 returns files that were modified two or more days ago.

It is a documented behavior i.e. -mtime ignores any fractional portion. From man find (Time calculation is identical in -atime, -mtime and -ctime):

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

Now if you can use couple of solutions:

  1. You can do:

    find . -type f -mtime +0
    

    This would get you the files modified one day or more ago.

    In your case:

    find $HOME -type f -name "myFile_*.sql.zip" -mtime +0
    
  2. To get precision like this case, you should use -mmin option which strictly matches minutes.

    So lets say you want to get the files that were modified more that 7 hours and 10 minutes i.e. 430 minutes ago, you can do:

    find . -type f -mmin +430
    

    In you case, do the calculation and replace 430 with that value:

    find $HOME -type f -name "myFile_*.sql.zip" -mtime +430
    
  3. Another option is to do it the reverse way.

    You can create a reference file e.g. ref.txt:

     touch -d 'today + 2 days ago' ref.txt
    

    Now you can find files that were modified more recently than ref.txt:

    find . -type f -newer ref.txt
    

    In your case:

    find $HOME -type f -name "myFile_*.sql.zip" -newer ref.txt
    

Once you are satisfied with the results, you can remove the file(s) e.g.:

find . -type f -mmin +430 -delete
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .