This article shows you 3 easy ways to get the last modified date of a file in Linux. After reading this article, you will learn how to check the last modified date of a file using the ls, date and stat commands.
Get the last modified date of a file in Linux using date command
The first way to find out the last modified date of a file on Linux, described in this article, is based on the date command. When the command date is followed by the -r flag, it shows us the date the file was last modified.
The following example shows the syntax for using the date command, where andreyex is the file whose last modified date we want to print.
date -r andreyex
As you can see, the output is very handy and includes the date and time the file was last modified.
Get last modified date of files in Linux using ls command
The second method 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 tells the ls command to print a long list of file formats. The list of long formats includes the date the file was last modified.
As you can see, the first column shows permissions, the second and third columns show users and filegroups. The fourth column is size. Then you can see the month, day and time when the file was modified. Unlike the previous ls method, you will get a list of all files in the directory, including their modified dates.
For ordered output, you can add the -t flag. The -t flag will print the output sorted by modification date, showing the last modified files first, as shown in the following image.
As you can now see, the last modified files are now displayed first.
The ls command also allows you to check the last modified date of a file individually; to do this, just provide the filename as shown below.
ls -l andreyex
As you can see, this output is more user-friendly if you only want to check one file.
Find out the last modified date of a file in Linux using stat
The third Linux method for getting the last modified date of a file described in this article shows how to use the stat command for this purpose.
The syntax is pretty straightforward; just run the stat command followed by the name of the file whose last modified date you want to know as shown in the example below.
As you can see, the output shows more information than the previous commands. It is important to distinguish between modification and modification dates. The line named Modify shows us the last time the file was modified. The line titled “Modify” shows when the file’s status was last changed; status includes changing permissions, changing ownership, etc. You can run the command below to get only the modification time in the output, omitting the rest of the information.
stat -c '%y' andreyex
As you can see, the output is limited to the date of the last modification.
The stat command also allows us to check multiple files at the same time, as shown in the example below, which checks for files named andreyex and andreyexfile.
stat andreyex andreyexfile
Now we get information about the two specified files.
This article shows that you can get the last modified date of a file in Linux in a variety of simple ways. None of the methods described offer particular advantages; all of them can be easily implemented without installing additional packages. We’ve used Debian for this article, but all of the commands described here are available on all Linux distributions.