Skip to Content

2 ways to get file last modified time in Ubuntu

There are a few different ways to get the last modified time of a file in Ubuntu. In this blog post, we will discuss two of them. The first method is to use the stat command. The second method is to use the ls command. Let’s take a closer look at each of these methods!

what is the last modified time of file in Ubuntu?

The last modified time of a file is the time at which the file was last changed. This can be changed by various actions, such as editing the file or moving the file to a different location.

Get last modified time of file with stat command in Ubuntu

The best way to get the last modified time of a file in Ubuntu is using the stat command.

stat -c %y filename

This will give you the last modified time of the file in Year-Month-Day Hour:Minute:Second format.

$ stat -c %y howtouseubuntu 
2023-03-15 13:15:29.610613671 +0000

The stat command is a very versatile command that can be used to get a lot of information about a file.

$ stat howtouseubuntu 
  File: howtouseubuntu
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d	Inode: 258623      Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/  ubuntu)   Gid: ( 1000/  ubuntu)
Access: 2023-03-15 13:15:29.610613671 +0000
Modify: 2023-03-15 13:15:29.610613671 +0000
Change: 2023-03-15 13:15:29.610613671 +0000
 Birth: -

The -c option is used to format the output of the stat command according to a specified format string.

The format string can contain one or more conversion specifications, which begin with a percent sign (%) and are followed by a single character that specifies the type of information to display.

Let’s see another example.

To display the file name and file permissions in human-readable format for a file named howtouseubuntu, you can use the following command:

$ stat -c '%n %A'  howtouseubuntu 
howtouseubuntu -rw-------

You can get more info with the stat command on man page.

check last modified time of file with ls command in Ubuntu

The ls command is another versatile command that can be used to get a lot of information about a file. To get the last modified time of a file, you would use the following command: ls -l filename

This will give you the last modified time of the file in Month Day Hour:Minute:Second format.

$ ls -l howtouseubuntu 
-rw------- 1 ubuntu ubuntu 0 Mar 15 13:15 howtouseubuntu

The -l option in ls command displays information about the file “howtouseubuntu” in a long format. Here’s what each column of the output means:

  • -rw——-: the file permissions. The first character – indicates that it is a regular file. The next three characters rw- indicate that the file owner has read and write permissions, but not execute permission. The next three characters — indicate that the file group and others have no permissions.
  • 1: the number of hard links to the file. In this case, there is only one link to the file.
  • ubuntu: the file owner. This file is owned by the user “ubuntu”.
  • ubuntu: the file group. This file belongs to the group “ubuntu”.
  • 0: the file size in bytes. This file has a size of 0 bytes.
  • Mar 15 13:15: the date and time the file was last modified.
  • howtouseubuntu: the name of the file.

Let’s see another important option in ls command.

The -t option is used to sort the output of the ls command by modification time, with the most recently modified files appearing first.

For example, if you run the following command:

ls -t

It will list all the files and directories in the current directory, sorted by their modification time, with the most recently modified files appearing first.

This can be useful when you want to quickly see the most recently modified files in a directory.

If you want to reverse the sort order, you can use the -r option along with -t like this.

ls -rt

Do you have any tips or tricks for using the stat or ls commands? Let us know in the comments below! And if you found this blog post helpful, please share it with your friends and colleagues! Thank you for reading!