How to use find to find files modified in the last N days or minutes

This article uses examples to introduce how to find files that have changed in the last N minutes or days, or find files that are older than N minutes or days. This is done using the find command.

To find files that have been changed in the last N days (the modification time of the file data is earlier than) from the directories and subdirectories, use:

find /directory/path/ -mtime -N -ls

where:

  • find Is a Unix command line tool for finding files (and more)
  • /directory/path/ Is the directory path to find the modified file.Replace it with the path of the directory where you want to find the last modified file N day
  • -mtime -N Used to match files with recently modified data N day.replace N With numbers (integer)
  • -ls List the generated files (the last modified file N day) ls -dils Format on standard output. You can skip this operation, but using it you will get more information, such as file size, permissions, modification date, etc.

example:

  • Find all files modified in the last day (24 hours; from now to one day ago) in the directories and subdirectories:
find /directory/path/ -mtime -1 -ls

-mtime -1 Are the same -mtime 0.

  • Find all files modified in the last 30 days:
find /directory/path/ -mtime -30 -ls

You might also like: Bash history: how to display the timestamp (date/time) when executing each command

However, if you need to find the modification date is earlier than N, For example, more than 30 days?In this case, you need to use +N instead -N, like this:

find /directory/path/ -mtime +N -ls

example:

  • Find all files whose modification date is older than 7 days:
find /directory/path/ -mtime +7 -ls

  • Find all files modified more than 48 hours ago (at least 2 days ago):
find /directory/path/ -mtime +1 -ls

  • Find all files modified 24 to 48 hours ago (1 to 2 days ago):
find /directory/path/ -mtime 1 -ls

Then why 1 one day ago +1 Earlier than 2 days / 48 hours ago?That is because according to man find, All decimal parts will be ignored, so if the file was last modified 1 day and 23 hours ago, -mtime +1 Will not match it, and treat it as the last modification of the file 1 day, 0 hours, 0 minutes, and 0 seconds; see This explanation Why is this

In this case, how to modify all files at least 1 day ago?use +0:

find /directory/path/ -mtime +0 -ls

Replace days with minutes

Find modified files N Minutes ago, or modified date earlier than N, Just replace -mtime versus -mmin.

Therefore, if you want to find the last changed file (the modification time of the file data is earlier than) N Minutes from directories and subdirectories, use:

find /directory/path/ -mmin N -ls

example:

  • Find all files modified in the last 5 minutes in directories and subdirectories:
find /directory/path/ -mmin -5 -ls

  • Find all files whose modification date is earlier than 5 minutes:
find /directory/path/ -mmin +5 -ls

You may also like: Starship is the smallest and fast Shell prompt written in Rust

Source

Sidebar