howtouselinux

3 Ways to get file last modified time in Linux

Table of Contents

To check a file’s modified time in Linux, you can use the stat command, the ls command or the date command. These commands display detailed information about a file, including when it was last modified.

In this blog post, we will discuss each of these methods in detail. We will also provide examples of how to use each method. By the end of this blog post, you should be able to check a file’s modified time using any of these three methods!

Understanding file last modified time in Linux

The file’s last modified time is the time at which the file was last changed. This can be changed by various actions, such as editing the file.

The file’s modified time is important because it can help you troubleshoot problems. For example, if a file is not working as expected, checking the file’s modified time can help identify when the problem started.

Moving files and changing file permission won’t change the file’s modified time.

Related: 3 ways to check file creation time in Linux

 

Procedure to Get file’s last modified time in Linux

  1. Open the terminal and navigate to the directory where the file is located.
  2. Type in the command stat -c “%y” filename
  3. press enter to run the command
  4. The file’s last modified time will be listed on the command output

 

Get file’s last modified time with state command in Linux

The stat command is the most versatile way to get a file’s last modified time in Linux. This command can be used to check the modified time of any file, regardless of its type. To use the stat command, simply type “stat” followed by the path to the file you want to check.

For example, if we wanted to check the modified time of the file “example.txt”, we would type:

stat example.txt

The output of the stat command will show you the file’s modified time, as well as other information about the file such as inode number, file size, file owner. In the example below, we can see that the file “example.txt” was last modified on October 21, 2022 at 5:52 PM:

$ stat example.txt
File: `example.txt’
Size: 8 Blocks: 8 IO Block: 4096 bytes
Device: 2400 /dev/sda2
Inode: 12584693 Links: 1
Access: (0600/-rw-rw-rw-) Uid: ( 1000/ anon ) Gid: ( 1000/ anon )
Access: 2022-10-21 17:52:37.000000000 -0400
Modify: 2022-10-21 17:52:37.000000000 -0400
Change: 2022-10-21 17:52:37.000000000 -0400

The stat command with -c option allows you to use a particular or custom format instead of the default.

stat -c “%y” filename
stat -c “%Y” filename

%y   time of last data modification, human-readable
%Y   time of last data modification, seconds since Epoch

Example:

[root@howtouselinux ~]# stat -c “%y” example.txt
2022-10-21 17:52:37.000000000 -0400
[root@howtouselinux ~]# stat -c “%Y” nohup.out 
1663137045

If you want more information about the stat command and what it can do, you can type the following command: man stat. This will open up the man page for stat, which will provide you with more information about this command.

Find file’s last modified time with ls command in Linux

The ls command can also be used to find a file’s last modified time. Type “ls -l” followed by the name of the file you want to check. The output of the ls command will show you the file’s modified time and other information about the file, such as file size and file permissions.

When the long listing format is used, you can see the following file information:

  • The file type.
  • The file permissions.
  • Number of hard links to the file.
  • File owner.
  • File group.
  • File size.
  • Date and Time.
  • File name.

 

For example, if we wanted to check the modified time of the file “example.txt”, we would type:

#ls -l example.txt

-rw-r–r– 1 howtouselinux staff 2 9 20 22:10 example.txt

we can see that the file “example.txt” was last modified on 9/20 at 22:10.

We can also use ls -lrth to list files in reverse order of the last modified time.

  • -l (The lowercase letter “ell”.) List files in the long format. It will give detailed information of files in columnar format.
  • -t Sort by descending time modified (most recently modified first). If two files have the same modification timestamp, sort their names in ascending lexicographical order.
  • -r Reverse the order of the sort. This option will display files and directories in reverse order. 
  • -h option will show you the file sizes in human readable format. Size of the file is very difficult to read when displayed in terms of byte.

 

so ls -lrht command will sort the files in the current directory by modified time. The most recently modified files are last, and the output is in a long and human-readable format.

Example:

[root@howtouselinux ~]$ ls -lrt
total 1680
-rw——-.  17 howtouselinux howtouselinux   19k Oct 22 03:07 anaconda-ks.cfg
-rw——- 17 howtouselinux howtouselinux   2k Dec 10 04:07 dhcp.conf

The ls command has many other options that can be useful when working with files in Linux. To learn more about the ls command and what it can do, you can type the following command: man ls.

Get file’s last modified time with date command in Linux

The date command can be used to get the system’s current date and time in Linux by default. However, it can also be used to check the last modified time of a file.

To use the date command to check a file’s modified time, type “date” followed by the “-r” option and the path to the file you want to check.

For example, if we wanted to check the modified time of the file “example.txt”, we would type:

date -r example.txt

These are three ways to check a file’s modified time in Linux! Which method do you prefer? 

By now, you should be able to check a file’s modified time using any of these three methods! If you have any questions, feel free to ask us in the comments below. Thanks for reading!

Don’t forget to check out our other blog posts for more Linux tips and tricks!

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.