When you are working on Linux, whether for development or system administration, it is important to be able to locate files that have been recently modified. It could be to identify when specific changes were made, or to quickly find files that are part of a code update or bug fix. The good news is, there are a few simple commands that can help you find recently modified files in Linux. In this blog, we will explore some of these commands and how to use them effectively.
1. Using the find command
One way to quickly find files modified in the last 24 hours is by using the find command. The command syntax is as follows:
find /path/to/search -mtime -1
This will search for files in
/path/to/search that were modified in the last 24 hours. The
-mtime flag specifies the time period that you want to search, and
0 means “within the last 24 hours”. You can replace
1 with a different number of days if you want to search for files modified within a different time period.
You can further filter the search by file type. For instance, if you only want to search for PHP files, you can use:
find /path/to/search -type f -name "*.php" -mtime -1
This command will search for all files in
/path/to/search (that end with php) that were modified in the last 24 hours.
2. Using the locate command
locate command is another useful tool to quickly search for recently modified files. However, it requires that you have a pre-built index of files on your system. To update the index, run:
Once the index is updated, you can search for recently modified files using:
locate -b "\*pattern\*" | xargs ls -altu
This command will search for all files that match the
pattern and were modified recently. The
| xargs ls -altu option sorts the search results by modification time, with the most recently modified files listed first.
3. Using the stat command
If you want to find specific information about a recently modified file, you can use the
stat command. The command syntax is:
This will display information about the file, including the last time it was modified. If you want to see a more human-readable format of the modification time, you can use:
stat -c '%y' /path/to/file
This will display the modification time in the format
4. Using the ls command
Finally, you can also use the
ls command to find recently modified files. The command syntax is:
ls -altu /path/to/search
This will list all files in
/path/to/search, sorted by the last time they were modified. You can add additional filters, such as file type (
-type f) or file size (
-size +10M) to narrow down the search.
As you can see, there are several ways to find recently modified files in Linux, depending on your needs. Whether you prefer using the
ls command, you now have the tools you need to quickly and efficiently locate the files you need. These commands can save you a lot of time and effort, especially when working on larger code projects or system updates. So the next time you need to find a recently modified file in Linux, give one of these commands a try and see how much easier it can make your workflow.