A detailed explanation of the differences between Atime,ctime and Mtime in Linux

Source: Internet
Author: User


Guessing the man handbook should have these three-time instructions, so try man atime, and man st_atime have no relevant documentation. After

To think of stat can see the file of these three time, use the man stat lookup, although there are documents, but not the relevant three time

Description Is helpless, suddenly think of C language stat function can also see the state of the file, immediately with Man 2 stat view hand

Book, sure enough to find, excerpt here:

The field st_atime is changed by file accesses, for example, by Execve (2), Mknod (2),

Pipe (2), Utime (2) and read (2) (of more than zero bytes). Other routines, like Mmap (2), May

or may not update st_atime.

The field st_mtime is changed by file modifications, for example, by Mknod (2), truncate (2),

Utime (2) and write (2) (of the more than zero bytes). Moreover, st_mtime of a directory is

Changed by
The creation or deletion of files in that directory. The St_mtime field isn't changed for

Changes in owner, group, hard link count, or mode.

The field st_ctime is changed by writing or by setting inode information (i.e., owner,

Group, link count, mode, etc.). With the English handbook, it is easier to understand, from the above analysis:

atime:access time file is accessed last.
mtime:modify time when the contents of the file have been modified.
ctime:change time file's inode content modification.


This is still not easy to understand, if you understand the function in English interpretation is easy to distinguish. For example, the atime mentioned Execve (2) and

The read (2) function, in fact, means that the time is modified when the file is executed and read, and write (2) in mtime indicates that when the content is written, the

The time has been modified; owner and group in CTime indicate that the time was modified when the user and user group of the file was modified.

Test under Atime

Write a simple function in C language, compile into a.out, and use the Stat command to view the a.out three time values:

$ stat a.out

......
Access:2013-01-15 10:49:50.032767384 +0800
Modify:2013-01-15 10:49:50.032767384 +0800
Change:2013-01-15 10:49:50.032767384 +0800
...... Then execute the file and see again:

$./a.out
$ stat a.out

......
Access:2013-01-15 10:52:31.912772564 +0800
Modify:2013-01-15 10:49:50.032767384 +0800
Change:2013-01-15 10:49:50.032767384 +0800
...... It can be found that atime has changed.

Test under Mtime

To create a new file:

$ Echo ' Hello ' > Test.txt
$ stat Test.txt

......
Access:2013-01-15 11:13:43.712813262 +0800
Modify:2013-01-15 11:13:43.712813262 +0800
Change:2013-01-15 11:13:43.712813262 +0800
...... To modify a file:

$ Echo ' World ' > Test.txt
$ stat Test.txt

......
Access:2013-01-15 11:13:43.712813262 +0800
Modify:2013-01-15 11:13:57.232813694 +0800
Change:2013-01-15 11:13:57.232813694 +0800
...... It can be found that mtime and CTime have found the change, why CTime will also change? Because the file's inode is stored in the

File of the number of bytes, file owner ID and other file-related properties, modified file content, related file properties will be updated, quite

Changed the content of the inode, so the CTime also changes.

There is also a doubt: the Inode also stores Atime, Mtime and CTime, then the example changes Atime, the content of the Inode

Had changed, why hasn't CTime changed? Very puzzled.

Test under CTime

Create a new file Ctime.txt

$ Echo ' Hello ' > Ctime.txt
$ stat Ctime.txt

......
Access:2013-01-15 11:22:02.908829236 +0800
Modify:2013-01-15 11:22:02.908829236 +0800
Change:2013-01-15 11:22:02.908829236 +0800
...... Modify permissions for Time.txt

$ chmod 777 Ctime.txt
$ stat Ctime.txt

......
Access:2013-01-15 11:22:02.908829236 +0800
Modify:2013-01-15 11:22:02.908829236 +0800
Change:2013-01-15 11:22:20.268829792 +0800
...... It can be found that CTime has changed.

Summary

This side is mainly for the normal file to do the test, about the directory problem, and ordinary files similar, but the contents of the content itself is stored

The file name of all files under the directory. If the files in the directory are new, renamed, Deleted, and so on, then the contents of the directory itself will

is modified, you can use VIM to see what the contents of the directory itself store.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.