Linux File Timestamps Explained With Examples

This tutorial explains what kind of file time stamps there are under Linux and uses examples to view and edit file time stamps using the touch screen.

Brief introduction to Linux time stamp files

Under Linux and Unix as a whole, each file has three types of timestamps, namely atime (access time), mtime (modification time) and ctime (modification time). As the name indicates, the timestamp is used to find out when a file was available and when it was changed.

The access time stamp (or short time stamp) is the last time the file was read. For example, you can open a file. The file may have been accessed by another program or a remote computer. You can read the contents of the file with a command (e.g. cat) or a program (e.g. gedit, vim, etc.). The file will not be changed or modified. It is only accessible to you or another user from an external system.

The modification date (or mtime) indicates the date on which the file (or folder) was last modified. In other words: mtime indicates the time at which the content of the file was changed. For example, you can add something to a file, delete something from a file, or change the content of a file.

The timestamp indicates the date of the last change to the content or metadata of the file (i.e. file attributes such as file ownership, rights or group).

In a nutshell,

  • atime – indicates the time of the last access to the file to read its contents
  • mtime – indicates the time of the last change in the content of the file.
  • ctime – indicates the time when the content of the file or the metadata (owner, group or access rights) was last changed.

Time stamps can be useful in different situations. Here are some examples of the use of time stamps:

Time stamp display of Linux files using the stat and ls commands

We can display the timestamp of files under Linux using the stat command. According to the manual pages, the stat command indicates the status of the file or file system under Linux. The statistics team is part of GNU Coreutils, so we don’t bother installing it.

We now want to check the timestamp of a text file named ostechnix.txt with the stat command :

$ ostechnix.txt stats

Taking samples:

File : ostechnix.txt Size
: Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access : (0664/-rw-r–) Uid : ( 1000/ sk) Hydraulic : ( 1000/ sk)Access : (0664/-rw-r-) Uid : ( 1000/ sk)Access: 2020-11 17:31:01.484504149 +0530
Amendment : 2020-11 17:31:01.484504149 +0530
Amendment : 2020-11 17:31:01.484504149 +0530
Birth: –

Viewing the timestamps of Linux files with the statistics command

The last three lines of the above output show the access time, the change time and the change time of the timestamp. If we just create a new file, all the time stamps are the same.

Did you notice the birth line at the end of the exit? Indicates the time when the file was created. The POSIX standard does not define a time stamp for creating a file. Some file systems (e.g. ext4, JFS, Btrfs) store this value, but there is currently no Linux kernel API to access it. So we see a hyphen – instead of a date stamp at birth: The line.

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

To display the change time stamp (mtime), use the ls -l command:

$ ls -l ostechnix.txt
-rw-r– 1 sk 21. November 11 17:31 ostechnix.txt

To display the timestamp of the change (ctime), execute the ls -lc command.

$ ls -lc ostechnix.txt
-rw-r– 1 sk 21. November 11 17:31 ostechnix.txt

Here the c-flag is used to indicate when the metadata or file attributes were last changed.

Display the access stamp (atime) with the command ls -lu :

$ ls -lu ostechnix.txt
-rw-r– 1 sk 21. November 11 17:31 ostechnix.txt

Here the u-flag indicates when the file was last accessed.

Display the timestamp of the Linux file with the ls command

Changing the timestamp of Linux files with thetouch command

The touch command is used to change the timestamp of files and to create new empty files under Linux. Like the stat command, the touch command is part of the GNU coreutils, so you don’t have to install it.

Before we change the timestamps, we remove the current timestamps from the file called ostechnix.txt to help us:

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access : (0664/-rw-r–) Uid : ( 1000/ sk) Hydraulic : ( 1000/ sk)Access : (0664/-rw-r-) Uid : ( 1000/ sk)Access: 2020-11 17:31:01.484504149 +0530
Amendment : 2020-11 17:31:01.484504149 +0530
Amendment : 2020-11 17:31:01.484504149 +0530
Birth: –

As can be seen from the above result, the ostechnix.txt file was consulted and amended on the same date and time, i.e. the eleventh day of the month. November 2020 at 17:31:01.

Let’s change the timestamp of this file with the touch command, as shown below:

$ sensory ostechnix.txt

The above command changes all timestamps (i.e. atime, mtime and ctime) to the current time on your computer.

Now let’s look at the timestamps in the stat command file:

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access : (0664/-rw-r–) Uid : ( 1000/ sk) Hydraulic : ( 1000/ sk)Access : (0664/-rw-r-) Uid : ( 1000/ sk)Access: 2020-11 17:39:59.430610787 +0530
Amendment : 2020-11 17:39:59.430610787 +0530
Amendment : 2020-11 17:39:59.430610787 +0530
Birth: –

Change the timestamp of a Linux file with a touch command

Do you see that? All timestamps have been changed for the current time in my system, i.e. 2020-11-10 17:51:02.

It is also possible to change the timestamps individually. For example, you can only change the time stamp (one hour) of the access with the -a indicator:

$ touch -a ostechnix.txt

The above command sets the time stamp for access for the current time.

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

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access : (0664/-rw-r–) Uid : ( 1000/ sk) Hydraulic : ( 1000/ sk)Access : (0664/-rw-r-) Uid : ( 1000/ sk)Access: 2020-11 17:43:46.555428706 +0530
Amendment : 2020-11 17:39:59.430610787 +0530
Amendment : 2020-11-11 17:43:46,555428706 +0530
Birth: –

Changing the access time stamp using touch control only

As you can see in the result above, the access time is currently changed. The changeover time is also adjusted.

Use the -m flag to change only the change time (mtime):

$ touch -m ostechnix.txt

Make sure the mtime has changed with the stat command:

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access : (0664/-rw-r–) Uid : ( 1000/ sk) Hydraulic : ( 1000/ sk)Access : (0664/-rw-r-) Uid : ( 1000/ sk)Access: 2020-11 17:43:46.555428706 +0530
Amendment : 2020-11 17:47:17.577722187 +0530
Amendment : 2020-11 17:47:17.577722187 +0530
Birth: –

Use the touch controls to change the change-only time stamp Use the touch controls to change the change-only time stamp Use the touch controls to change the change-only time stamp Use the touch controls to change the change-only time stamp of the change

Today the mtime and c-time-stamps have changed, while time has remained the same.

As you may have noticed, when changing the access time or changing the time, the c-time is also updated.

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

$ sensory -d 2020-11 17:50:01 ostechnix.txt

Make sure that the time and m time stamps have been changed with or without the stat command:

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access : (0664/-rw-r–) Uid : ( 1000/ sk) Hydraulic : ( 1000/ sk)Access : (0664/-rw-r-) Uid : ( 1000/ sk)Access: 2020-11 17:50:01.000000000 +0530
Amendment : 2020-11 17:50:01.000000000 +0530
Amendment : 2020-11-11 17:51:17.024213698 +0530
Birth: –

Simultaneous change of time and timestamp m by touch control

We have now set the access time and the changeover time to a certain time, namely 2020-11 17:50:01. And also the timestamp is currently updated.

You can also use special timestamps instead of the current time with the -t flag:

$ touch -t 2011111754 ostechnix.txt

This team will set the hours a and m on 2020-11 17:54:00. Check this with the stat command, as shown below:

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access : (0664/-rw-r–) Uid : ( 1000/ sk) Hydraulic : ( 1000/ sk)Access : (0664/-rw-r-) Uid : ( 1000/ sk)Access: 2020-11 17:54:000000000 +0530
Amendment : 2020-11 17:54:000000000 +0530
Amendment : 2020-11 17:56:20.844405239 +0530
Birth: –

As already mentioned, ctime is updated when atime and mtime are adjusted. If you only want to change the time stamp, there is no special flag in the touch command. You therefore need to manually adjust the ctime by changing the metadata or attributes of the file. For example, I will assign a right to run ostchnix.txt with the chmod command :

$ chmod +x ostechnix.txt

Check that ctime receives the update with the command stat :

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access: (0775/-rwxrxr-x) Uid: (1000/ sk) Hydraulic: (1000/ sk)Access: (0775/-rwxr-x) Uid: (1000/ sk)Access: 2020-11 17:54:000000000 +0530
Amendment : 2020-11 17:54:000000000 +0530
Amendment : 2020-11 17:57:21.022720776 +0530
Birth: –

Change only the timestamp of the change using the touch control

Do you see that? The timbre of time has changed, but the timbre of time and m-time have not changed. Because the access to the file has not changed, not changed. I just changed the permissions on the files.

Changing the timestamp of files by viewing or openingfiles or modifying them

As mentioned above, the timestamp is changed when you open a file to read it. Try to open the file to read it with the chat command and see what happens.

cat ostechnix.txt
This is a text file

Now check if the time stamp for access has been updated:

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 21: I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access: (0775/-rwxrxr-x) Uid: (1000/ sk) Hydraulic: (1000/ sk)Access: (0775/-rwxr-x) Uid: (1000/ sk)Access: 2020-11-11 18:00:16:005323521 +0530
Amendment : 2020-11 17:54:000000000 +0530
Amendment : 2020-11 17:57:21.022720776 +0530
Birth: –

Do you see that? Today, the value of time has changed.

The mtime is updated when we add data to a file or delete data from a file.

Let me add a line to ostechnix.txt:

$ echo change this file >> ostechnix.txt

Check the time stamp on the folder :

$ statistics ostechnix.txt
File : ostechnix.txt
Size : Block 38 : I/O block : 4096 Single file
Device: 801h/2049d Inode: 4351679 Communication : 1A-access: (0775/-rwxrxr-x) Uid: (1000/ sk) Hydraulic: (1000/ sk)Access: (0775/-rwxr-x) Uid: (1000/ sk)Access: 2020-11-11 18:00:16:005323521 +0530
Amendment : 2020-11-18:01:49.072771458 +0530
Amendment : 2020-11-11 18:01:49.072771458 +0530
Birth: –

As the content of the file has changed, the mtime and ctime of the file have changed.

Symbolic link Time stamp change

When you apply the touch command to a symbolic link (or sim-link), the timestamps in the reference file are also changed by default.

If you only want to change the timestamp of a SIM connection, use the -h, (–no-o-dereference) option:

$ adorable. -h

[Bonus Tip] Copy timestamp from another file.

The touch command has a -r, (–reference=) option that allows you to copy timestamps from one file to another.

The following command copies the timestamps from file1 to file2.

$ touch -r File1.txt File2.txt

For more information, refer to the pages of the statistics and touch control manual :

man stat
man touch

Conclusion

In this tutorial we have seen three types of Linux file time stamps, atime, mtime and ctime, how file time stamps are displayed using the stat and ls commands, and finally how these time stamps can be changed by touching them. I hope this helps.

Related Tags:

find ctime example,unix command atime,linux file creation time,linux change ctime,file times in unix,locating files in linux,how to check timestamp in linux server,file creation time linux,cache timestamp file for the linux user,linux file modification time,find by atime,linux find files accessed last 24 hours,mtime, ctime <= atime,how to find file creation time in linux,unix timestamp,touch: invalid date format,touch -t unix timestamp,touch recursive,echo welcome /dev/tty,touch stamp linux,touch future date,unix change date modified,touch command windows,touch command in unix tutorials point,touch command mac,touch examples,unix touch yesterday,touch command in unix to change timestamp,how to get timestamp of a file in unix,how to get timestamp of a file in unix shell script\,how to check the last modified time of a file in linux,linux file creation date,linux ctime example,find file timestamp linux,how to change timestamp of a directory in linux