Linux file last modification time

3 Ways to get file last modified time in Linux

To check the last modified time of the file 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 last 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.

Procedure to Get file 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 last modified time will be listed on the command output

Get file 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:

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

Читайте также:  Astra linux postgresql узнать версию

[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 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:

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

[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 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:

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!

Читайте также:  Настройка сети linux утилита ip

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

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

How to display modification time of a file?

I’d like a method to find and print the modified time of a file, for use within a bash script. I have come up with:

ls -l $filename | cut -d ' ' -f '6-8' 

Though I’d like to avoid parsing ls , also it’d be useful to have the year in there. Ideally I’d like to see an output similar to the default output of the date command.

Tue Jul 26 15:20:59 BST 2016 

2 Answers 2

Don’t use ls , this is a job for stat :

-c lets us to get specific output, here %y will get us the last modified time of the file in human readable format. To get time in seconds since Epoch use %Y :

If you want the file name too, use %n :

stat -c '%y : %n' filename stat -c '%Y : %n' filename 

Set the format specifiers to suit your need. Check man stat .

% stat -c '%y' foobar.txt 2016-07-26 12:15:16.897284828 +0600 % stat -c '%Y' foobar.txt 1469513716 % stat -c '%y : %n' foobar.txt 2016-07-26 12:15:16.897284828 +0600 : foobar.txt % stat -c '%Y : %n' foobar.txt 1469513716 : foobar.txt 

If you want the output like Tue Jul 26 15:20:59 BST 2016 , use the Epoch time as input to date :

% date -d "@$(stat -c '%Y' a.out)" '+%a %b %d %T %Z %Y' Tue Jul 26 12:15:21 BDT 2016 % date -d "@$(stat -c '%Y' a.out)" '+%c' Tue 26 Jul 2016 12:15:21 PM BDT % date -d "@$(stat -c '%Y' a.out)" Tue Jul 26 12:15:21 BDT 2016 

Check date ‘s format specifiers to meet your need. See man date too.

Further to this answer, bear in mind that ls is inconsistently implemented across systems, and so should never be used for any sort of automation. Instead, use commands such as stat and find when writing scripts.

excellent. I had not known about this little treasure. So many of them in the *nix/linux environment yet to be discovered.

ls -l $filename | cut -d ' ' -f '6-8' 

but if the date is less than 10 it misses the time. This because of the extra space before the date if less than 10. Try this:

filename="test.txt" ls -l $filename | awk -F ' ' '' 

The awk command prints the fields separated by all spaces (-F ‘ ‘). Hope it works. I know this doesn’t answer the original question but just a clarification on the ls command for just date and time. When you Google «ubuntu get date and time of file» it lists this question at the top, which is what I was looking for, since I don’t need the year also. For year, date and time, you could try one of the commands below. %m prints month number. %b prints month abbreviation: Drop the %H:%M if you don’t need the hour and minute. %-d doesn’t print leading zero for the day of the month.

date -r $filename +"%Y %m %-d %H:%M" date -r $filename +"%y %m %-d %H:%M" date -r $filename +"%Y %b %-d %H:%M" date -r $filename +"%y %b %-d %H:%M" 

Источник

Читайте также:  Запуск графического интерфейса astra linux

How to Get Last Modified Date of File in Linux

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 command

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:

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

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:

On Ubuntu / Debian / Mint, run the command:

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'
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.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector