Linux File time Parsing

Source: Internet
Author: User

Linux File time Parsing

The reason for writing this small article is that I have worked on a project and need to transfer files regularly. Maybe rolover is a professional term. In this case, you need to determine whether a file has been transferred to another time, So that you need to know the file creation time. Linux Files are divided into the state change time ctime, the modification time mtime, And the access time atime. How do they change? Note that this ctime is not the creation Time, it is the abbreviation of Change Time, the modification Time is the file modification Time Modify Time, the Access Time is the file read Time Access Time. In fact, it is quite mysterious. Create a fileTouch test, And the written content is "abc ". We can useStat testCommand to view the time of the file. Just created, we can find that the three times are the same. :



1. Status Change Time

In fact, the so-called state change is actually the time when the inode information is changed. Which operations will change inode information? We know that inode in linux Files contains information such as file permissions, access time, creation time, file size, number of links, and owner of files, therefore, changing the information will cause ctime to change.

  • We change the test permission of the file, resulting in ctime changes. In the preceding example, run chmod-w test and execute stat test again. The result is shown in. The status change time ctime is changed, while the modification time and access time remain unchanged. Similarly, we use the chown command to modify the user and group information of the file, which may also cause ctime changes. In addition, creating a hard link such as ln test test1 and modifying the file content such as echo 'hei'> test will change the ctime (Note that you must create a symbolic connection.Ln-s test test2It does not cause ctime changes, because the soft connection is equivalent to another file and does not affect this file.).


  • To sum up, there are several situations where ctime is modified:
    A. Modify the file permissions or attributes such as the owner and number of links, such as chmod, chown, and ln (excluding ln-s ).
  • B. Modify the file content. (Of course, mtime will also be changed. If you use tools such as vim to open the file and modify it, atime will also be modified. If you only use the echo 'hehes'> test command to append the data, only ctime and mtime will be modified, without affecting atime ).

2. modification time

The modification time can easily be changed if the file is modified. This is true, but this modification refers to the modification operation. It does not mean that the file content remains unchanged, instead, the file has not been written. For example, run the command vim test to open the file, add a character 'D' to test, and then delete the character 'D'. In this case, use w to save the file, we will find that although the content of the test file has not changed, its modification time, mtime, has been updated. Of course, this operation first opens the file and then modifies it, So atime will also be updated, and ctime must also be updated.


3. Access time

The access time atime will be modified as long as the file is read. Such as more and cat commands. To solve the overhead of atime updates in systems with frequent file reads, many SAS use the noatime attribute to stop updating atime when mounting the file system, in this way, you can use atime as the file creation time. However, some programs need to make some judgments and Operations Based on atime, so Linux has launched a relatime feature in the kernel of the new version 2.6. After you use this feature to mount a file system, atime is updated only when mtime is more than atime. That is to say, the simple more and cat commands do not update atime. atime will be updated only when you read the file again after modification.

After the relatime feature is used, for example, you can use the echo 'hehe '> test command to modify the content of the test file. After the test file is written, you can find that both ctime and mtime are updated, however, atime has not been updated. At this point, you can use commands such as cat test or more to open the test file, which will update atime. As shown in




4. References
  • Ext2 File System for linux C All-in-One Programming
  • Linux Files

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.