The find command is a versatile utility in Linux that allows you to search for files and directories based on various criteria, including modification time, size, ownership, and more. In this article, we will focus on using the find command to search for modified files in the last 30 days or within the last 30 days. This can be particularly useful when you need to find recently changed files for backup or archive purposes, or when you want to identify the source of a problem in your system.
The find command operates by recursively searching a specified directory and its subdirectories for files that match a specified set of criteria. By using the -mtime
option, you can search for files based on their modification time, and by using the -newermt
option, you can search for files based on a specific date range.
In this article, we will explain how to use these options to find modified files in the last 30 days or within the last 30 days, and provide examples of how to perform these searches. By the end of this article, you will have a good understanding of how to use the find command to search for modified files in Linux.
Find Modified Files Within Last X Days
To find files modified in the last 30 days, you can use the following command:
find /path/to/directory -mtime -30
In this command:
- “/path/to/directory” is the directory where you want to search for files.
-type f
L option specifies that you are searching for files and not directories.-mtime -30
option specifies that you want to search for files that were modified in the last 30 days.
The -
sign before 30 indicates that you want to search for files that were modified in the last 30 days. If you want to search for files modified more than 30 days ago, you can use the + sign instead.
You can also specify the file extension to search files with specific extension.
find /path/to/directory -type f -name "*.txt" -mtime -30
In above command, will search for all “.txt” files modified in within 30 days.
Find Modified Files Before X Days
The below command will search all files and directories modified before 30 days. Here dot (.) is used to search in current directory. And +30
defines to search files modified before 30 day. Change this number with your search preferences.
find /path/to/directory -type f -mtime +30
The -time +30
indicates that you want to search for files that were modified before last 30 days.
Find Modified Files Between Two Dates
To find files modified between two dates, you can use the following command:
find /path/to/directory -type f -newermt '2023-01-01' ! -newermt '2023-01-31'
In this command, /path/to/directory is the directory where you want to search for files. The -type f
option specifies that you are searching for files and not directories. The -newermt '2021-01-01'
option specifies the starting date, and the ! -newermt '2021-01-31'
option specifies the end date. The !
sign before the end date indicates that you want to search for files modified before the end date.
Conclusion
In conclusion, the find command is a powerful utility in Linux that can be used to search for files based on various criteria, including modification time. By using the options discussed in this article, you can find files modified in the last 30 days, within the last 30 days, or between two dates.
4 Comments
Need to change the + to a – in my case .
Hello
In the find manual, you can read:
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.
Go to know, isn’t it ?
Cheers
All of this was broken with systemd.
$ find . -mtime -30 days
find: paths must precede expression: days
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression]
Rahul, it was a great help. Thanks. Alex