How to Get Last Modified Date of File in Linux

Written by: Winnie Ondara   |   Last updated: June 25, 2022

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.

stat filename
stat command
stat of 'file1.txt'

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
Use stat command to only check modified date
Use stat command to only check modified date

The -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

For example,

date -r file1.txt
date command to display last modified date
date command to display last modified date

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
check last modified file uing ls -lt
check last modified file uing ls -lt

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'

For example:

http -h https://example.com/wp-content/uploads/2020/09/Fedora-32-desktop.png | grep -i 'Last-Modified'
Output
Last-Modified: Fri, 18 Sep 2020 22:38:48 GMT

Conclusion

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.

About The Author

Winnie Ondara

Winnie Ondara

Winnie is a Linux technical writer with over 3 years of experience with various Linux distributions and writing technical guides in Linux. She is passionate about FOSS technologies and always endeavor to learn new technologies. During my free time, I watch movies, listen to music, and catch up with tech news.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment