Linux system CTime, mtime and Atime commands detailed

Source: Internet
Author: User
Tags chmod touch command

Atime:access time when the file (read or executed) or directory was last accessed
Mtime:modofy time when the file (content) or directory (content) was last modified
Ctime:change time when the file (property) or directory (property) was last changed

How to view the Atime/mtime/ctime of a file or directory
1, stat filename
2,
Atime:ls-lu filename
mtime:ls-l filename
CTIME:LS-LC filename
Example demonstrates
Under/tmp Create a new directory Tm,tm a new file a.txt

The code is as follows Copy Code
# cd/tmp
# mkdir TM
# CD TM
# Stat. /tm
File: '. /tm '
size:4096 blocks:8 IO block:4096 Directory
device:ca01h/51713d inode:1050207 Links:2
Access: (0755/drwxr-xr-x) Uid: (0/root) Gid: (0/root)
ACCESS:2014-03-17 10:57:32.993350411 +0800
MODIFY:2014-03-17 10:57:32.993350411 +0800
CHANGE:2014-03-17 10:57:32.993350411 +0800
Birth:-
# Touch A.txt
# stat A.txt
File: ' A.txt '
size:0 blocks:0 IO block:4096 Regular empty file
device:ca01h/51713d inode:1050208 links:1
Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)
ACCESS:2014-03-17 10:58:01.328805868 +0800
MODIFY:2014-03-17 10:58:01.328805868 +0800
CHANGE:2014-03-17 10:58:01.328805868 +0800
Birth:-

The atime/mtime/ctime of the newly created directories and files are the same.

Open the A.txt, enter the contents of the point and save it.

  code is as follows copy code
# vi a.txt
# stat a.txt
  File: ' a.txt '
  size:3           Blocks:8          IO block:4096   regular file
device:ca01h/51713d inode:1050208     links:1
Access: (0644/-rw-r--r--)   Uid: (    0/    root)    Gid: (     0/    root)
access:2014-03-17 11:02:19.619836157 +0800
MODIFY:2014-03-17 11:02:22.399782635 +0800
change:2014-03-17 11:02:22.423782143 +0800
 birth:-

VI a.txt will modify the file atime, the input content will be modified mtime, and because the file's attributes changed (such as file size changed), so CTime will also change.

Modify the properties of the file, CTime changes, Mtime and atime unchanged.

The code is as follows Copy Code
# chmod G+w a.txt
# stat A.txt
File: ' A.txt '
Size:9 blocks:8 IO block:4096 Regular file
device:ca01h/51713d inode:1050208 links:1
Access: (0664/-rw-rw-r--) Uid: (0/root) Gid: (0/root)
ACCESS:2014-03-17 11:02:19.619836157 +0800
MODIFY:2014-03-17 11:02:22.399782635 +0800
CHANGE:2014-03-17 11:07:10.114234176 +0800
Birth:-

Will changes in content change atime? The answer is!

  code is as follows copy code

# echo ' World ' >> a.txt
# stat a.txt
  File: ' A.txt '
&NB Sp size:9          Blocks:8           IO block:4096   Regular file
device:ca01h/51713d inode:1050208      Links:1
Access: (0644/-rw-r--r--)   Uid: (    0/    root)    Gid : (    0/    root)
access:2014-03-17 11:02:19.619836157 +0800
MODIFY:2014-03-17 11:09:07.980591047 +0800
change:2014-03-17 11:09:07.980591047 +0800
 birth:-


Touch instruction is used to change the timestamp of the file, the syntax format of the Touch command is as follows:

touch [options] ... filename ...

Options and Parameters:
-A: revise atime only;
-C: Only the time to modify the file, if the file does not exist will not create a new file;
-D: You can follow the date you want to revise without the current date, or you can use the--date= "date or time"
-M: Modify Mtime only;
-T: Later you can take the time you want to revise instead of the current time, in the form [YYMMDDHHMM]
-R: Sets the date and time of the specified document or directory to the same date and time as the reference document or directory
...
Example demonstrates

  code is as follows copy code
# touch a.txt
# stat a.txt
  File: ' a.txt '
  SIZE:0 &NBS p;        Blocks:0          IO block:4096   Regular Empty file
device:ca01h/51713d inode:1050217     Links: 1
Access: (0644/-rw-r--r--)   Uid: (    0/    root)    Gid: (     0/    root)
access:2014-03-18 09:30:49.965240705 +0800
modify:2014-03-18 09:30:49.965240705 +0800
change:2014-03-18 09:30:49.965240705 +0800
 birth:-

The

First creates a.txt by touch, and modifies the atime/mtime/ctime of the file if it already exists.

  code is as follows copy code
# touch a.txt
# stat a.txt
  File: ' a.txt '
  SIZE:0 &NB sp;        Blocks:0          IO block:4096   Regular Empty file
device:ca01h/51713d inode:1050217     Links: 1
Access: (0644/-rw-r--r--)   Uid: (    0/    root)    Gid: (     0/    root)
access:2014-03-18 09:31:43.584234285 +0800
modify:2014-03-18 09:31:43.584234285 +0800
change:2014-03-18 09:31:43.584234285 +0800
 birth:-

Touch A.txt again, found that atime/mtime/ctime have changed.

  code is as follows copy code
# touch-a a.txt
# stat a.txt
  File: ' a.txt '
  size:0           Blocks:0          IO block:4096   Regular empty file
device:ca01h/51713d inode:1050217     Links:1
Access: (0644/-rw-r--r--)   Uid: (    0/    root)    Gid: (     0/    root)
access:2014-03-18 09:32:01.459898755 +0800
modify:2014-03-18 09:31:43.584234285 +0800
change:2014-03-18 09:32:01.459898755 +0800
 birth:-

Using Touch-a a.txt, only Atime and CTime have changed, mtime save unchanged.

  code is as follows copy code
# touch-m a.txt
# stat a.txt
  File: ' a.txt '
  size:0           Blocks:0          IO block:4096   Regular empty file
device:ca01h/51713d inode:1050217     Links:1
Access: (0644/-rw-r--r--)   Uid: (    0/    root)    Gid: (     0/    root)
access:2014-03-18 09:32:01.459898755 +0800
modify:2014-03-18 09:32:22.591502109 +0800
change:2014-03-18 09:32:22.591502109 +0800
 birth:-

Using Touch-m a.txt, only Mtime and CTime have changed, and the atime has not changed.

In any case, using the Touch command, the CTime of the file will change. After the touch instructions above, the value of the atime/mtime/ctime is the value of the current point in time, and you can specify the time stamp at will.

  code is as follows copy code
# touch-d" 2 days Ago "B.txt && stat b.txt
  File: ' B.txt '
&nbs P size:0          Blocks:0           IO block:4096   Regular empty file
device:ca01h/51713d inode:1050218      links:1
Access: (0644/-rw-r--r--)   Uid: (    0/    root)     Gid: (    0/    root)
access:2014-03-16 09:38:49.261034132 +0800
Modify: 2014-03-16 09:38:49.261034132 +0800
change:2014-03-18 09:38:49.256243241 +0800
 birth:-

Touch-d by specifying a string representing the time as the current time, this format is relatively free, as long as it can represent time, such as "2014-02-28 16:21:42″," "Next Thursday", "2 days ago" and so on.

The code is as follows Copy Code
# touch-t 201401011212.30 c.txt && stat c.txt
File: ' C.txt '
size:0 blocks:0 IO block:4096 Regular empty file
device:ca01h/51713d inode:1050219 links:1
Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)
access:2014-01-01 12:12:30.000000000 +0800
modify:2014-01-01 12:12:30.000000000 +0800
change:2014-03-18 09:39:45.983178168 +0800
Birth:-
# touch-t 01011212 d.txt && stat d.txt
File: ' D.txt '
size:0 blocks:0 IO block:4096 Regular empty file
device:ca01h/51713d inode:1050220 links:1
Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)
access:2014-01-01 12:12:00.000000000 +0800
modify:2014-01-01 12:12:00.000000000 +0800
change:2014-03-18 09:41:59.092678768 +0800
Birth:-

The timestamp format after the TOUCH-T directive is: [[CC]YY]MMDDHHMM[.SS]

Similarly, CTime is always the current time

The difference between the Ctime,mtime,atime

When you talk to a skilled UNIX user, you often hear them arrogantly speaking of the term "Change time" and "modification Time". For many people (and many dictionaries), changes and modifications are the same. What's the difference here?

The difference between change and modification is to change the label of a component or change its contents. If someone says Chmod a-w myfile, then this is a change; if someone says Echo foo >> myfile, then
This is a modification. The change is that the index node of the file changes, and the change is the content of the text itself. [The file's modification time is also called the time sign (timestamp).]

As long as the discussion changes time and changes time, it is impossible not to mention "access time". The access time is the last time the file was read. So reading a file updates its access time, and when it changes time does not change (the information about the file is not changed), so does its modification time (the file itself has not been changed)

Sometimes, changing time in many places or "CTime" is wrongly written as "creation time," including some UNIX reference manuals. Don't trust them.
Here is my man out of the content, for reference only!

The code is as follows Copy Code

St_atime
Time is file data is last accessed. Changed by the
Following Functions:creat (), Mknod (), pipe (),
Utime (2), and read (2).

St_mtime
Time when data is last modified. Changed by the fol-
Lowing functions:creat (), Mknod (), pipe (), utime (),
and write (2).

St_ctime
time when file status is last changed. Changed by the
Following Functions:chmod (), Chown (), creat (),
Link (2), Mknod (), pipe (), unlink (2), Utime (), and
Write ().

The time of LS display should be mtime.

After the touch, the file three time should be changed, you can try.

Problem description
What is the difference between the CTime, Mtime, and atime of a file?
Configuration information
Solving method
The Access time,atime for the file is changed while reading the file or executing the file.
The Modified time,mtime of the file is changed as the file content changes when it is written to the file.
The Create time,ctime of a file is changed as the content of the Inode changes when the file is written, the owner, permissions, or link settings are changed.

As a result, changing the contents of the file changes Mtime and CTime, but the CTime of the file may change when the mtime has not changed-the permissions change, but the contents of the file do not change.
The LS (1) command can be used to list the atime, CTime, and Mtime of the file.
LS-LC filename Lists the CTime of the file
Ls-lu filename Lists the atime of the file
ls-l filename Lists the mtime of the file

Atime is not necessarily modified after accessing the file because: when using the Ext3 file system, the Atime information is not updated if the Noatime parameter is used when the mount is used. And this is added Noatime canceled, does not mean true?? What's the situation? Time stamp are all placed in the inode. If mtime, atime modification, the inode must be??? Now that the inode has changed, the CTime will have to change. The reason for using Noatime in Mount option is that it does not want the file system to make too many changes to improve performance.

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.