What is the difference between mtime, atime and ctime?

2018-10-18|Categories: Linux|

原文地址:https://www.quora.com/What-is-the-difference-between-mtime-atime-and-ctime ,转帖到这里主要是为了方便查看,并补充自己的学习笔记。

mtime, atime and ctime are timestamps associated with the modification, access and change of any file respectively.

  • mtime

    mtime (modification time) indicates the time the contents of the file has been changed. Mind you, only the contents. Not the attributes. For instance, if you open a file and change some (or all) of its content, its mtime gets updated. If you change a file's attribute (like read-write permissions, metadata), its mtime doesn't change, but ctime will.

  • atime

    atime (access time) is the timestamp that indicates the time that a file has been accessed. The file may have been opened by you, or may have been accessed by some other program or a remote machine. Anytime a file has been accessed, its access time changes.

  • ctime

    ctime (change time) is the timestamp of a file that indicates the time that it was changed. Now, the modification can be in terms of its content or in terms of its attributes. Whenever anything about a file changes (except its access time), its ctime changes.

Let me demonstrate it by the following example.

On a Unix-like system, lets create a file and check its atime, mtime and ctime.

We can see, all the timestamps are same.

Now, let us print its contents.

Since the text file was blank, it didn't print anything. But looking at its stats, we see the access time has changed, while the others remain unchanged.

Now, let us write something into the file.

Notice how all of its timestamps have changed! Since the file had to be accessed to modify it, the atime has changed. And since the contents of the file changed, its mtime and ctime both changed.

Now, instead of contents, what happens if we change its attributes? Say, file permissions?

We added the 'execute' permission to the file and see how its atime and ctime changes! As is obvious, to make any changes to the file, you have to access it. Hence the changes to atime. Also, since only the attributes were changed and not the contents, the ctime changed while mtime remained as it is.

In general, this is how the timestamps behave:

mtime <= ctime <= atime

My summary:

File operations atime changes? ctime changes? mtime changes?
Read (cat)
Write (vim)
Change attribute (chmod)

2 Comments

  1. LIHUI 2018-10-24 at 10:11 - Reply

    我是重庆一LINUX爱好者,有问题想请教你,没有你的联系方式。如果方便可以加我微信:13638388182,谢谢。

    • liyang 2018-11-10 at 18:25 - Reply

      不必客气,有时间大家可以一起学习、一起促进。

      最近比较忙,没有及时处理评论,还望见谅。

Leave A Comment