歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux下的三個時間屬性

Linux下的三個時間屬性

日期:2017/3/3 12:48:51   编辑:Linux技術

Linux下的文件有三個時間屬性。分別為atime、ctime、mtime。atime:access time,即文件的最近一次訪問時間。ctime:change time,即文件的最近一次改變時間(這裡並非create time創建時間)。改變是指文件的屬性發生改變,一般為修改文件權限或者文件名。mtime:modify time,即文件的最近一次修改時間。修改是指文件的內容發生改變。 其中,當mtime發生改變時,ctime一定改變,而atime不一定改變。當修改文件內容時,文件大小等屬性發生改變,此時文件ctime會一起發生改變。但是修改文件的方式有多種,如果是直接echo ‘hello’ > file,此時我們已經修改文件內容,但並未查看文件,所以atime不變。如果是vi file,此時我們已經查看到文件內容,不管你保存與否,atime一定發生改變。 我們可以通過ls命令查看文件的三個屬性,查看方式如下:ls -ul 查看文件的atimels -cl 查看文件的ctimels -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文件後,文件的atime、ctime、mtime都是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時的時間,ctime、mtime依然為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
可以發現,修改文件內容之後,文件的ctime、mtime變為修改時的時間,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、ctime、mtime都變為修改時的時間。

本文出自 “一年後回頭看看” 博客,請務必保留此出處http://cubix.blog.51cto.com/7251166/1773467

Copyright © Linux教程網 All Rights Reserved