“find” is the Unix/Linux command-line utility used for searching files across the file system. Sometimes we need to search files modified in the last few days. Assume you have modified multiple files in your application and forgot to keep track of the modified files. In that case, the find command provides you an option to search files based on their modification. You can also search the files modified before X days.
-mtime option with the find command to search files based on modification time followed by the number of days. The number of days can be used in two formats.
- Use + with number of days to search file modified older than X days
- Use – with number of days to search file modified in last X days
The below examples will help you to understand the search for files based on modification time.
Find Files Modified in the Last X Days
Use the below command to search all files and directories modified in the last 30 days. Here dot (.) is used to search in the current directory. And
-30 defines to search files modified in the last 30 days. Change this number with your search requirements.
find . -mtime -30
You can also customize the search based on file type. Use -type followed with -f (file) or -d (directory). The below command will search for files only.
find . -type f -mtime -30
Find Files Modified Before X Days
The below command will search all files and directories modified before 30 days. Here dot (.) is used to search in the current directory. And
+30 defines to search files modified before 30 days. Change this number with your search preferences.
find . -mtime +30
Customize search pattern to search for files only using
-type f. Or use
-type d to search for directories.
find . -type f -mtime +30
For “find” command help page use the below command.
For “find” command manual page use the below command.