Linux下的文件有三个时间属性。分别为atimectimemtime

atimeaccess time,即文件的最近一次访问时间。

ctimechange time,即文件的最近一次改变时间(这里并非create time创建时间)。改变是指文件的属性发生改变,一般为修改文件权限或者文件名。

mtimemodify time,即文件的最近一次修改时间。修改是指文件的内容发生改变。

 

其中,当mtime发生改变时,ctime一定改变,而atime不一定改变。当修改文件内容时,文件大小等属性发生改变,此时文件ctime会一起发生改变。但是修改文件的方式有多种,如果是直接echo  ‘hello’ > file,此时我们已经修改文件内容,但并未查看文件,所以atime不变。如果是vi file,此时我们已经查看到文件内容,不管你保存与否,atime一定发生改变。

 

我们可以通过ls命令查看文件的三个属性,查看方式如下:

ls  -ul 查看文件的atime

ls  -cl 查看文件的ctime

ls  -l 查看文件的mtime

 

接下来通过几个试验测试三个时间在什么情况下会发生改变。

1.touch一个文件,之后查看文件的三个时间属性。

[root@TestServercubix]# date
2016年 05月 15日 星期日 01:29:10 CST
[root@TestServercubix]# touch file
[root@TestServercubix]# ls -u file  
-rw-r--r--. 1root root 0 5月  15 01:29 file
 [root@TestServer cubix]# ls -c file
-rw-r--r--. 1root root 0 5月  15 01:29 file
[root@TestServercubix]# ls -l file
-rw-r--r--. 1root root 0 5月  15 01:29 file

可以发现touch文件后,文件的atimectimemtime都是touch时的时间。

 

2.查看文件的内容,之后查看文件的三个时间属性。

[root@TestServercubix]# date

2016年 05月 15日 星期日 01:30:33 CST
[root@TestServercubix]# cat file
[root@TestServercubix]# ls -u file
-rw-r--r--. 1root root 0 5月  15 01:30 file
[root@TestServercubix]# ls -c file
-rw-r--r--. 1root root 0 5月  15 01:29 file
[root@TestServercubix]# ls -l file
-rw-r--r--. 1root root 0 5月  15 01:29 file

可以发现,cat文件之后,文件的atime变为cat时的时间,ctimemtime依然为touch的时间。

 

3.修改文件内容(直接修改,不查看文件内容),之后查看文件的三个时间属性。

[root@TestServercubix]# date
2016年 05月 15日 星期日 01:32:11 CST
[root@TestServercubix]# echo 'Hello!' > file
[root@TestServercubix]# ls -u file
-rw-r--r--. 1root root 7 5月  15 01:30 file
[root@TestServercubix]# ls -c file
-rw-r--r--. 1root root 7 5月  15 01:32 file
[root@TestServercubix]# ls -l file
-rw-r--r--. 1root root 7 5月  15 01:32 file

可以发现,修改文件内容之后,文件的ctimemtime变为修改时的时间,atime为上次查看时的时间。

 

4.改变文件属性,之后查看文件的三个时间属性。

[root@TestServercubix]# date       
2016年 05月 15日 星期日 01:50:40 CST
[root@TestServercubix]# chmod 700 file          
[root@TestServercubix]# ls -lu file          
-rwx------. 1root root 7 5月  15 01:30 file
[root@TestServercubix]# ls -lc file          
-rwx------. 1root root 7 5月  15 01:50 file
[root@TestServercubix]# ls -l file          
-rwx------. 1root root 7 5月  15 01:32 file

可以发现,改变文件内容之后,文件的ctime变为改变时的时间,atime为上次查看时的时间、mtime为上次改变时的时间。

 

5.修改文件内容(修改时看到文件内容),之后查看文件的三个时间属性。

[root@TestServer cubix]# date
2016年 05月 15日 星期日 02:18:24 CST
[root@TestServer cubix]# vi file 
[root@TestServer cubix]# ls -ul file 
-rwx------. 1 root root 11 5月  15 02:18 file
[root@TestServer cubix]# ls -cl file  
-rwx------. 1 root root 11 5月  15 02:18 file
[root@TestServer cubix]# ls -l file  
-rwx------. 1 root root 11 5月  15 02:18 file

可以发现,修改文件内容之后,文件的atime、ctimemtime都变为修改时的时间