Sometimes, you may be required to check detailed information about a file (timestamp) such as its last modified date. This can come in handy when you want to check when the file was last edited. Additionally, it ensures that you have the latest version of the file.
In this guide, you will learn 4 ways to get the last modified date of file in Linux.
1. Using stat command
The ls -l command is just okay in giving you basic information about a file such as file ownership and permissions, file size, and creation date. The stat command returns detailed information file attributes such as the last time the file was accessed and modified.
The syntax is quite simple. stat is followed by the file name or the full path to the file.
From the above output, we can clearly see when the file was last accessed ( Access date ), Modify date, Change date among other parameters.
If you wish to view the modified date only and leave out all the other information, run the following command:
stat -c ‘%y’ file1.txt
-c option is used to return the date in a custom format, while the
'%y' flag displays the last modification time. For directories, the syntax remains the same. Simply replace the file name with that of the directory.
2. Using date command
The date command in its basic syntax displays the current date. However, when used with the
-r option, you can display the last modification date of a file as shown.
date -r filename
date -r file1.txt
3. Using ls -l command
The ls -l command is usually used for long listing - display additional information about a file such as file ownership and permissions, size and creation date. To list and display the last modified times, use the
lt option as shown.
ls -lt filename
4. Using httpie
Another way you can check the last modified date is by using the httpie HTTP command-line client tool. The tool is usually used for interacting with HTTP servers and APIs and can also check when a file residing on a web server was last modified.
But first, you need to install it using the command:
pip3 install httpie --user
On Ubuntu / Debian / Mint, run the command:
sudo apt install httpie
To check when a file on a web server was last modified, use the syntax:
http -h [url] | grep 'Last-Modified'
http -h https://example.com/wp-content/uploads/2020/09/Fedora-32-desktop.png | grep -i 'Last-Modified'
Last-Modified: Fri, 18 Sep 2020 22:38:48 GMT
In this guide, we have featured various ways that you can use to list the last modified date of a file on a Linux system, and even a file hosted on a web server using the httpie tool. Hopefully, you won't have an issue viewing when files were last modified.