How to Find Last Modified Files in Linux?
This tutorial explains how to find last modified files in Linux using different commands and according to custom needs.
After reading this tutorial you’ll know how to execute the following tasks:
- How to find files modified in a specific day range
- How to find last modified specific file type (e.g mp4, png)
- Finding files modified before / after X minutes
- How to find files modified in a specific date
- Finding modified files recursively
- Search omitting files or directories
- Find files by access date
Finding last day modified files in Linux:
To start, let’s search files modified less than a day ago. To find files modified a day ago you can use the commands find and newermt used in the following example.
The find command is used to search files. The newermt command compares files timestamp with the argument passed, in this case “1 day ago”. Then, the ls command is passed to list the files.
$ find /home/linuxhint -newermt “1 day ago” -ls
To find last day modified files, you can also use the mtime command together with find. By specifying the option 0 as in the example below, mtime will return all files modified in the last 24 hours.
$ find /home/linuxhint -mtime 0
Find Last Modified Specific File Type in Linux:
You can use a wildcard to limit your search to a specific file type. In the following example, find and newermt are instructed to list all mp4 files modified a day ago.
cc lang=”bash” width=”100%” height=”100%” escaped=”true” theme=”blackboard”]$ find /home/linuxhint/*.mp4 -newermt “1 day ago” -ls[/cc
In the following example, find and newermt are used to find all .png images less than 15 days old.
$ find /home/linuxhint/Desktop/*.png -newermt “15 day ago” -ls
Finding Last Hour Modified Files in Linux:
The following example combines the find command with the mmin command. We can use the mmin command to specify minutes. In the example below, the find and mmin commands will print all files under the /root directory, whose modifications are less than 60 minutes old.
Contrary to the previous example in which files modified in the past 60 minutes were found. You can also use +mmin to search files modified after X minutes. For example, the following command will show files modified 60 minutes ago or more.
Finding Files Modified on a Specific Date in Linux:
You can use the ls command to list files including their modification date by adding the -lt flag as shown in the example below. The flag -l is used to format the output as a log. The flag -t is used to list last modified files, newer first.
Then you can combine ls -lt with grep to print all files which were modified on a specific date.
$ ls -lt /home/linuxhint | grep ‘Jun 11’
Find Last Modified Files Recursively:
Previous examples are useful to find last modified files
The command below can be used to print last modified files recursively.
$ find -printf “%TY-%Tm-%Td %TT %pn“ | sort -n
Search File by Date Omitting Files or Directories:
Contrary to the previous example, you can search files omitting directories. For this purpose, you need to implement the -type flag with the option f (file) as shown in the following example. As a result, you’ll see only final files and no directories.
$ find /root -type f -newermt “2021-06-10”
You can also search directories only and the output will omit files. For this, just replace the f with a d after the -type flag.
$ find /root -type d -newermt “2021-06-10”
Find Files by Access Date:
You also may want to find unmodified files by access date. For this purpose, you can use the atime command. It is similar to the mtime command explained before, but instead of identifying files by modification, it can display files by access. With this command you can learn the last accessed files and directories in the system.
The following command shows all files accessed in the past 10 days.
$ find /root -atime -10 -type f
Like the previous command, you can also use the d option to show only directories:
$ find /root -atime -10 -type d
If you don’t specify a type, atime will show all files and directories:
In the following example, find and atime are used to find files and directories with modification older than 20 days.
As with previous examples, you can also limit the listing to files or directories with the -type flag.
$ find /home -atime +20 -type f
Conclusion:
As you can see, Linux offers different methods to find files according to modification time. Any Linux user level can easily learn those methods to search files with a single command. Finding files by modification or access within a system is part of the basic knowledge a Linux user needs.
I hope this tutorial was useful. Keep following Linux Hint for more Linux tips and tutorials.