Pages

Wednesday, 25 March 2020

Unix file timestamps all in one place: atime, mtime, ctime

In the Unix world, a file or directory has three timestamps as metadata, atime/AccessTime, mtime/ModifyTime, and ctime/ChangeTime.


  • atime is for the timestamp when the file's content read.
  • mtime is for the time when the file's content was modified.
  • ctime is for the time when the file's inode(metadata, including atime/mtime, permission, ownership ...)  was changed.

1 How to display timestamps? 

1.1 ls can only show one of them

By default, "ls -l" shows the mtime. To show others, use "ls -lu" for atime, "ls -lc" for ctime.

1.2 stat can show all of them at the same time


$ stat hello
  File: hello
  Size: 6               Blocks: 8          IO Block: 4096   regular file
Device: fd02h/64770d    Inode: 7620190     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ smstong)   Gid: ( 1000/ smstong)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-03-25 17:31:32.405671903 -0400
Modify: 2020-03-25 19:51:36.319163363 -0400
Change: 2020-03-25 19:51:36.319163363 -0400
 Birth: -

2 How to update timestamps directly

Normally, users don't have to change timestamps manually as they are maintained by the file system automatically. But Linux does provide a tool for users to directly write timestamps. The tool is called "touch".

"touch" can be used to change atime and mtime separately or together.

# without any options, touch update atime and mtime to the current time. 
$ touch hello
$ stat hello
...
Access: 2020-03-25 22:17:19.724106900 -0400
Modify: 2020-03-25 22:17:19.724106900 -0400
Change: 2020-03-25 22:17:19.724106900 -0400
...

The ctime is updated too, but rather than being updated directly by "touch", it's updated by the filesystem automatically in respond to the metadata changing (here metadata are atime and ctime).

# "touch -a" updates atime only
$ touch -a hello
$ stat hello
...
Access: 2020-03-25 22:22:33.734591194 -0400
Modify: 2020-03-25 22:17:19.724106900 -0400
Change: 2020-03-25 22:22:33.734591194 -0400
...

Here we can see that ctime was changed automatically again.

# "touch -m" updates mtime only
$ touch -m hello
$ stat hello
...
Access: 2020-03-25 22:22:33.734591194 -0400
Modify: 2020-03-25 22:25:19.161899978 -0400
Change: 2020-03-25 22:25:19.161899978 -0400
...

Here we can see that ctime was changed automatically again.

"touch" can also set atime and/or mtime to any denoted time instead of the current time.

# "touch -d" can set timestamps to any value you want
$ touch -d '1981-11-26' hello
$ stat hello
...
Access: 1981-11-26 00:00:00.000000000 -0500
Modify: 1981-11-26 00:00:00.000000000 -0500
Change: 2020-03-25 22:28:29.187403314 -0400
...

3 When File Systems update them automatically

These timestamps are supposed to be maintained by the OS automatically.

3.1 atime is not always updated automatically

If a file's atime is changed every time it's read, the file system may suffer from writing inode metadata and become inefficient. As a workaround, Linux provides some options when mounting a file system so that atime could be updated less frequently.


  • strictatime (strict atime): This option updates the access timestamp of files every time they’re accessed. There’s an overhead attached with this approach, but some servers can benefit from this scheme. It has little merit on a desktop or laptop computer.
  • noatime (no atime): This option fully disables the access timestamps for files and directories from updating. The modified timestamps, however, will still update.
  • nodiratime (no dir atime): This option enables access timestamps for files to update, but disables it for directories.
  • relatime (relative atime): This option updates the access timestamp only if it was more than 24-hours old, or the previous one was older than the current modified or changed timestamps. This strikes a good balance between access timestamps updating too frequently or not updating at all.
The default is "relatime".

$ cat /proc/mounts  | grep ext4
/dev/sda2 /boot ext4 rw,seclabel,relatime 0 0
/dev/sdb3 /disk2 ext4 rw,seclabel,relatime 0 0

The following example confirms that the atime didn't change after it was read.

$ date
Wed Mar 25 22:34:55 EDT 2020
$ cat hello
hello
$ stat hello
Access: 2020-03-25 22:33:55.961988509 -0400
Modify: 2020-03-25 20:00:00.000000000 -0400
Change: 2020-03-25 22:32:51.045474935 -0400


Generally, don't rely too much on atime, unless the filesystem is mounted with 'strictatime' option.

3.2  ctime is always updated to the time when it's updated

ctime represent the time when the inode (metadata) was changed. It is always updated automatically by the filesystem. Some common metadata examples:
  • permission bits
  • ownership
  • timestamps (atime and mtime)
As editing a file updates its mtime, ctime is also updated accordingly.

3.3 mtime is updated when editing and creating

File creation in any way would update its all timestamps, be it copying or touching.
But "cp -p" preserves the timestamps.

4. Where is creation time?

There is NO creation time at all!
It exists only in the Microsoft Windows world!

No comments:

Post a Comment