Linux file time resolution

Source: Internet
Author: User

Linux file time resolution

the reason for writing this small article is that you have done a project before, you need to periodically transfer the document, perhaps a professional noun called rollover. Then it is necessary to determine whether a file has been transferred to the time, so you need to know the creation time of the file. Linux files are divided into state change time ctime, modification time mtime and access time atime, then how do they change? Note that this ctime is not a creation time, but an abbreviation for change time, which is the time that the file was modified and modify time, which is the time that the file was read by Access. In fact, there is a lot of mystery. Create a new file , touch test, to write the content "ABC". We can use the stat Test command to view these three times of a file. Just created, you can find three time is the same. :



1. State Change Time

In fact, the so-called state change, is actually the inode information changes the time. So what is the specific operation that will cause the inode information to change? We know that the Linux file Inode contains information such as file permissions, access time, creation time information, file size, number of links, the user of the file, and so on, so changing this information will cause CTime to change.

    • Our ability to change the file test will cause ctime changes. In the previous example, we performed chmod-w test, and then we performed the Stat test again to see the results as shown, the state change time CTime changed, and the modification time and access time were the same. Similarly, we use the Chown command to modify information such as the user and group that the file belongs to, and also cause CTime to change. In addition to creating a hard link such as ln test test1, modifying the contents of the file such as echo ' hehe ' >> test will cause CTime to change ( note that creating a symbolic connection LN- The S test test2 does not cause CTime to change because the soft connection is equivalent to another file and does not affect itself on this file .


    • So, to summarize, modify the state change time CTime The situation is as follows:
      A. Modify file permissions or attributes such as owner, number of links, etc. such as CHMOD,CHOWN,LN (excluding ln-s).
    • B. Modify the contents of the file. (It will also change the mtime, if the use of tools such as vim to open the file and then modify, but also modify the atime.) If you just append to the command with Echo ' hehe ' >> test, only the ctime,mtime will be modified, not atime.

2. Modification time

Modification time we can easily think of the document has changed only change, that is actually the case, but this modification refers to the modification of the operation, not to say that the contents of the file unchanged it does not change, but requires the file has not been written operation. For example, we use the command VIM test to open the file, add a character ' d ' in test, and then delete the character ' d ', this time with : w Save the file, we will find that although the test file content has not changed, However, its modification time Mtime has been updated. Of course, this operation itself is to open the file and then modify, so Atime will also be updated, CTime must also be updated.


3. Access time

As long as we are reading the file, we will modify the access time atime. such as more, cat commands, etc. To address the overhead of atime updates in a file-read system, many SAS use the Noatime property to stop updating atime when mounting the file system, which allows atime to be used as a file creation time. However, some programs need to be based on atime to make some judgments and operations, so Linux in the 2.6 new version of the kernel has introduced a relatime feature. Using this feature to mount the file system, the Atime is updated only when Mtime is newer than atime. This means that the simple more,cat command does not update the atime, only if you modify the file and then read it again to update the atime.

After using the Relatime feature, such as you use the echo ' hehe ' >> Test command to modify the test file content, just finished writing, you can find CTime and Mtime are updated, but Atime has not been updated. At this point, you open the test file with a command such as Cat test or more, and the Atime is updated. As shown




4. References
    • Linux c ext2 file system for one-stop programming
    • Linux files three kinds of time
Related Article

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.