How to Get Last Modified Date of File in Linux

get last modified date of file

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 article, 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://linoxide.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

This wraps up this article. 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.

Winnie Ondara 3:36 am

Comments

Your email address will not be published. Required fields are marked *

1 Comment

  1. I prefer using 'ls' over all the others because ls allows you to control precisely how the date and time are displayed. I believe stat only gives the choice between seconds-since-epoch and human-readable, with no control over the human-readable format.

    For ls, the relevant option is '--time-style' and its format specifiers are fairly straightforward, using the same specifiers used by /bin/date. See 'man date' for all the available specifiers. My personal favorite is --time-style="+%Y-%m-%d %H:%M:%S". I use this alias for my day-to-day ls needs...

    alias l="/bin/ls --time-style=\"+%Y-%m-%d %H:%M:%S\" --group-directories-first -lLFAGv"