Get Last Modified Date of a File in Linux with the command date
The first method to learn the last modification date of a file in Linux explained in this tutorial focuses on the date command. When the command date is followed by the -r flag, it shows us the last modification date of a file.
The following example shows the syntax to use the date command, where linuxhint is the file whose last modification date I want to be printed.
As you can see, the output is very user-friendly and includes the date and time of the last modification done on the file.
For more information on the date command, visit https://man7.org/linux/man-pages/man1/date.1.html.
Get Last Modified Date of Files in Linux using the ls command
The second method explained in this tutorial explains how to get the last modified date of a file using the ls command.
First, let’s use the ls command followed by the -l flag, as shown in the screenshot below. The -l flag instructs the ls command to print a long format list of files. The long format list includes the last modification date of a file.
As you can see, the first column shows the permissions, the second and third column shows file users and groups. The fourth column is the size. Then you can see the month, day, and time in which the file was modified. Contrary to the previous method with ls, you will get a list of all files in the directory, including their modification dates.
For an ordered output, you can add the -t flag. The -t flag will print the output ordered by modification date, showing the last modified files first, as shown in the following image.
As you can see now, the last modified files are now shown first.
The ls command also allows you to check the last modification date of a file individually; for this, just specify the file’s name as shown below.
As you can see, this output is more user-friendly if you only want to check a single file.
You can get additional information on the ls command at its mane page https://en.wikipedia.org/wiki/Ls.
Learn Last Modified Date of a File in Linux using stat
The third method to get the last modification date of a file in Linux explained in this article shows how to use the stat command for this purpose.
The syntax is pretty simple; just run the stat command followed by the file’s name whose last modification date you want to know, as shown in the example below.
As you can see, the output shows more information than previous commands. It is important to differentiate the modification and change dates.
The row named Modify shows us the last time the file was modified. The row named Change shows the last time the status of the file changed; the status includes permissions change, ownership change, etc.
You can run the command below to get in the output only the modification time, omitting the rest of the information.
As you see, the output is limited to the last modification date.
The stat command also allows us to check several files at once, as shown in the example below, in which files named linuxhint, and linuxhintfile are checked.
Now we get information on the two files we specified.
You can get more information on the stat command at https://man7.org/linux/man-pages/man1/stat.1.html.
Conclusion
This tutorial shows that getting the last modified date of a file in Linux can be done through different simple methods. There is no special advantage for any of the methods explained; all of them can be implemented easily and without installing additional packages. For this tutorial, I used Debian, but all commands described here are available in all Linux distributions. All the commands explained have additional features you can know by reading the man pages specified at the end of each section.
Thank you for following us; keep reading Linux Hint for additional tips and tutorials.