Linux file last modification time

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

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:

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.

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.

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:

Output

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.

1 thought on “How to Get Last Modified Date of File in Linux”. add one

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.

Читайте также:  Виджет часов с погодой для windows

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»

Источник

File Timestamps — mtime, ctime and atime in Linux

When you are working with directory and files, you may need to know about Linux file timestamps such as change time (ctime), access time (atime), and modification time (mtime). Linux files, directories, sockets have three different timestamps – mtime, ctime and atime.

Probably when working in Linux you have get answers to following questions:

When was the last date of file content modified? When was the file last opened/accessed ? When the properties of the file such as ownership, permissions last changed?

Here, we are going to explain each file timestamps in Linux in detail.

mtime – Last modification time

Mtime or modification time is the time of the last change to the file contents. ‘Modification’ means something inside the file was amended or deleted, or new data was added.

Use the -l (long listing) option with ls, you can see the modified timestamp.

ctime – last change time

Ctime is the changed timestamp referring to changes made to attribute of a file such as ownership, access permission. It’s the time at which the metadata related to the file changed.

To see the change timestamp, use the -lc option:-

atime – last access time

Atime or access timestamp is the last time a file was read, read by one of the processes directly or through commands and scripts.

Use the -lu (access time) option with command ls to see access time. You can see the modification time and access time for the same file are different.

Show mtime, atime and ctime with stat command

Most of the linux distribution come with stat command which can be used to show all of the time stamp in a more convenient way.

To see modification time, access time and change time of a particular file use as follow:-

The timestamp are first generated in number of seconds since the Unix epoch, it translates the number of seconds into a date and time from the system time zone.

Conclusion

Unfortunately, we won’t be able to find file creation time using ctime, atime or mtime orelse we have to use debugfs command.

In this article, we learned about Linux file timestamps and about access time, modification time, and change time. Your feedback is much welcome.

Источник

Linux File Timestamps Explained With Examples

This tutorial explains the types of Linux file timestamps and how to view and change a file’s timestamps using touch command with examples.

A brief introduction to Linux file timestamps

In Linux and Unix in general, every file has three types of timestamps namely atime (access time), mtime (modification time) and ctime (change time). As the name implies, the timestamps are used to find out when a file was accessed, modified and changed.

The access time (or atime in short) timestamp is the last time a file was read. For instance, you might have opened the file. The file may have been accessed by some other program or a remote machine. You might have read the contents of a file using a command (e.g. cat command), or a program (e.g. gedit, vim etc.). The file is neither edited nor modified. It has only been accessed by you or some other user from a remote system.

Читайте также:  Структура корневого каталога windows

The modification time (or mtime) timestamp indicates the last time a file (or directory) was modified. In other words, mtime indicates the time the contents of a file has been changed. For instance, you might have added something in the file or deleted something from the file, or might have amended the contents of the file.

A change time (or ctime) timestamp shows the last time the file contents or the file metadata (i.e. file attributes, such as file ownership, file permissions, or group) was changed.

  • atime — indicates the the time of last access to file for reading contents.
  • mtime — indicates the time of last modification to file contents.
  • ctime — indicates the time of last change to file contents or file metadata (owner, group, or permissions).

The timestamps can be helpful in various situations. Here are a few use-cases for timestamps:

  • Find and sort files based on access or modification time.
  • Find and delete files older than X days. Useful to clean up old files in your hard drive.
  • Check when was a configuration file was changed.
  • Verify if a file was accessed.
  • Verify if a directory was updated.
  • Find files older or newer than X days.

View Linux file timestamps with stat and ls commands

We can view the file timestamps in Linux using stat command. According to man pages, the stat command displays file or file system status in Linux. The stat command is part of the GNU Coreutils , so let’s not bother installing it.

Now, let us check the timestamps of a text file named ostechnix.txt using stat command:

Sample output:

View Linux file timestamps with stat command

The last three lines in the above output shows the access time, modification time and change time timestamps respectively. When we just create a new file, all timestamps are same.

Did you notice line » Birth: » at the end of the output? It indicates the file creation time timestamp. The POSIX standard does not define a timestamp for file creation. Some filesystems (e.g. ext4, JFS, Btrfs) store this value, but currently there is no Linux kernel API to access it. So, we see a hyphen «-» instead of a timestamp in «Birth:» line.

You can also get the atime, mtime, and ctime timestamps individually using ls command.

To view the modification time (mtime) timestamp, use ls -l command:

To view the change time (ctime) timestamp, run ls -lc command:

Here, the c flag is used to display the time of last change to file metadata or file attributes.

View access time (atime) timestamp with ls -lu command:

Here, the u flag displays the time of last access to the file.

View Linux file timestamps with ls command

Change Linux file timestamps with touch command

The touch command is used to change the file timestamps as well as create new, empty files in Linux. Just like stat command, the touch command is also part of GNU coreutils , so you don’t need to install it either.

Before changing the timestamps, let us get the current timestamps of the file named ostechnix.txt for reference:

As you can see in the above output, the file ostechnix.txt was accessed. modified and changed on the same date and time i.e. November 11, 2020 at 17:31:01.

Let us change the timestamps of this file using touch command like below:

Читайте также:  Драйвер для oklick gp 315m для windows 10

The above command will change all timestamps (i.e. atime, mtime and ctime) to your computer’s current time.

Now let us take a look at the timestamps of the file with stat command:

Change Linux file timestamps using touch command

See? All timestamps have been changed to my system’s current time i.e. 2020-11-10 17:51:02 .

It is also possible to change individual timestamps separately. For instance, you can change only the access time (atime) timestamp with -a flag:

The above command will set the access time timestamp to the current time.

Now, check the timestamp of ostechnix.txt file with stat command:

Change only the access time timestamp with touch command

As you see in the above output, the access time is changed to current time. The change time is also updated.

To change only the modification time timestamp (mtime), use -m flag:

Verify if the mtime has changed with stat command:

Change only the modification time timestamp with touch command

Now the mtime and ctime timestamps changed while atime remained as it is.

As you might have noticed when we change the access time or modification time, the ctime also gets updated.

If you want to change both atime and mtime timestamps simultaneously, use -d option.

Verify if the atime and mtime timestamps have been changed or not with stat command:

Change both atime and mtime timestamps simultaneously with touch command

We have now set the access time and modification time to a specific time i.e. 2020-11-11 17:50:01 . And the ctime timestamp was also updated to the current time.

It is also possible to use specific timestamp instead of current time with -t flag as well:

This command will set the atime and mtime timestamps to 2020-11-11 17:54:00 . Verify it with stat command like below:

As stated already, the ctime gets updated when the atime and mtime are changed. If you want to change only the ctime timestamp, there is no dedicated flag in touch command. So you need to manually change the ctime by modifying the file metadata or file attributes. For example, I am going to assign executable permission to ostchnix.txt file with chmod command:

Check if ctime gets update with stat command:

Change only the change time timestamp with touch command

See? The ctime timestamp changed but the atime and mtime times didn’t. Because the file is neither accessed not changed. I changed the file permission only.

Change file timestamps by viewing or accessing or modifying files

As stated already, the atime timestamp gets changed when we access the file for read. Try to access the file for read using cat command and see what happens.

Now check if the access time timestamp is updated:

See? The atime value has changed now.

The mtime gets updated when we add or remove data from a file.

Let me add a line in the ostechnix.txt file:

Check the file timestamps:

Since the contents of the file changed, its mtime and ctime both changed.

By default, if you use touch command on a symbolic link (or symlink), it will change the timestamps of the referenced file as well.

If you want to only change the timestamp of a symlink, use -h , ( —no-dereference ) option:

[Bonus Tip] Copy timestamps from another file

The touch command has an option called -r, (—reference=) which allows you to copy timestamps from one file to another.

The following command will copy the timestamps of file1 to file2.

For more details, refer man pages of stat and touch commands:

Источник

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