linux中atime,ctime和mtime的區別詳解

來源:互聯網
上載者:User


猜測man手冊中應當有這三個時間的相關說明,於是嘗試man atime、man st_atime都沒有相關文檔。後

來想到stat可以查看檔案的這三個時間,就用man stat尋找,雖然有文檔,但是沒有這三個時間的相關

說明。正一籌莫展之際,突然想到c語言的stat函數也可以查看檔案的狀態,立馬就用man 2 stat查看手

冊,果然找到了,摘抄在這裡:

The  field  st_atime  is  changed  by file accesses, for example, by execve(2), mknod(2),

pipe(2), utime(2) and read(2) (of more than zero bytes).  Other routines, like mmap(2), may

or may not update st_atime.

The field st_mtime is changed by file modifications, for example, by mknod(2), truncate(2),

utime(2) and write(2) (of more than zero bytes).  Moreover, st_mtime of a directory  is 

changed  by
the creation or deletion of files in that directory.  The st_mtime field is not changed for

changes in owner, group, hard link count, or mode.

The field st_ctime is changed by writing or by setting inode information (i.e., owner,

group, link count, mode, etc.).有了英文手冊,理解起來就簡單些了,從上面分析:

•atime:access time檔案被訪問的最後時間。
•mtime:modify time檔案內容被修改的時間。
•ctime:change time檔案的inode內容修改的時間。


這樣還是不容易理解,如果從英文解釋中的函數來理解就容易分清了。例如atime中有提到execve(2)和

read(2)函數,其實就是表示檔案執行和讀取時該時間被修改;mtime中的write(2)就表示內容寫入時該

時間被修改;ctime中的owner和group表示修改檔案所屬使用者和使用者組時該時間被修改。

測試下atime

用c語言編寫一個簡單的功能,編譯成a.out,使用stat命令查看a.out的三個時間值:

$ stat a.out

......
Access: 2013-01-15 10:49:50.032767384 +0800
Modify: 2013-01-15 10:49:50.032767384 +0800
Change: 2013-01-15 10:49:50.032767384 +0800
......然後執行一下這個檔案,再次查看:

$ ./a.out
$ stat a.out

......
Access: 2013-01-15 10:52:31.912772564 +0800
Modify: 2013-01-15 10:49:50.032767384 +0800
Change: 2013-01-15 10:49:50.032767384 +0800
......可以發現atime發生了改變。

測試下mtime

建立一個新檔案:

$ echo 'hello' > test.txt
$ stat test.txt

......
Access: 2013-01-15 11:13:43.712813262 +0800
Modify: 2013-01-15 11:13:43.712813262 +0800
Change: 2013-01-15 11:13:43.712813262 +0800
......修改檔案:

$ echo 'world' > test.txt
$ stat test.txt

......
Access: 2013-01-15 11:13:43.712813262 +0800
Modify: 2013-01-15 11:13:57.232813694 +0800
Change: 2013-01-15 11:13:57.232813694 +0800
......可以發現mtime和ctime都發現了改變,為什麼ctime也會發生改變呢?因為檔案的inode記憶體儲了

檔案的位元組數、檔案擁有者的id等檔案相關屬性,修改了檔案內容後,相關檔案屬性也會被更新,相當

於修改了inode的內容,所以ctime也發生了變化。

另外還有一個疑問:inode也儲存了atime、mtime和ctime,那麼上例改變atime的時候,inode的內容發

生了變化,為什麼ctime沒有發生變化呢?非常不解。

測試下ctime

建立新檔案ctime.txt

$ echo 'hello' > ctime.txt
$ stat ctime.txt

......
Access: 2013-01-15 11:22:02.908829236 +0800
Modify: 2013-01-15 11:22:02.908829236 +0800
Change: 2013-01-15 11:22:02.908829236 +0800
......修改time.txt的許可權

$ chmod 777 ctime.txt
$ stat ctime.txt

......
Access: 2013-01-15 11:22:02.908829236 +0800
Modify: 2013-01-15 11:22:02.908829236 +0800
Change: 2013-01-15 11:22:20.268829792 +0800
......可以發現ctime發生了改變。

小結

這邊主要是針對普通檔案做了測試,關於目錄的問題,和普通檔案類似,只是目錄本身的內容儲存的是

目錄下的所有檔案的檔案名稱。如果目錄內的檔案有建立、改名、刪除等操作,那麼目錄本身的內容就會

被修改,可以使用vim查看目錄本身儲存的內容。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.