歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux 文件時間詳解 ctime mtime atime

Linux 文件時間詳解 ctime mtime atime

日期:2017/2/28 14:22:43   编辑:Linux教程

Linux系統文件有三個主要的時間屬性,分別是 ctime(change time), atime(access time), mtime(modify time)。這三個時間很容易混淆,准備深入了解Linux的童鞋請區分這三者的區別

atime:Access time,是在讀取文件或者執行文件時更改,即文件最後一次被讀取的時間。
說明: st_atime
Time when file data was last accessed. Changed by the
following functions: creat(), mknod(), pipe(),
utime(2), and read(2).

mtime:Modified time,是在寫入文件時隨文件內容的更改而更改,是指文件內容最後一次被修改的時間。
說明: st_mtime
Time when data was last modified. Changed by the fol-
lowing functions: creat(), mknod(), pipe(), utime(),
and write(2).

ctime:Change time,是在寫入文件、更改所有者、權限或鏈接設置時隨 Inode 的內容更改而更改,即文件狀態最後一次被改變的時間。
說明: st_ctime
Time when file status was last changed. Changed by the
following functions: chmod(), chown(), creat(),
link(2), mknod(), pipe(), unlink(2), utime(), and
write().
很多人把它理解成create time,包括很多誤導人的書籍也是這麼寫。實際上ctime是指change time。

注意:
1、修改是文本本身的內容發生了變化(mtime)
改變是文件的索引節點發生了改變(ctime)
2、如果修改了文件內容,則同時更新ctime和mtime
3、如果只改變了文件索引節點,比如修改權限,則只是改變了ctime

4、如果使用ext3文件系統的時候,在mount的時候使用了noatime參數則不會更新atime的信息,即訪問文件之後atime不會被修改,而這個不代表真實情況

小知識:這三個 time stamp 都放在 inode 中。若mtime,atime修改, inode 就一定會改,相應的inode改了,那ctime 也就跟著要改了,之所以在mount option中使用 noatime, 就是不想 file system 做太多的修改, 從而改善讀取性能.

查看文件的 atime、ctime 和 mtime。
# ls -lc filename 列出文件的 ctime
# ls -lu filename 列出文件的 atime
# ls -l filename 列出文件的 mtime

例子
1: # echo "Hello World" >> myfile atime不變,同時改變了ctime和mtime
2: # cat myfile ctime和mtime不變,只改變atime
# ls myfile
ctime和mtime以及atime都不改變
3: # chmod u+x myfile mtime和atime不變,只改變ctime
4: # mv myfile ../
mtime和atime不變,只改變ctime

其他擴展:
relatime屬性

從kernel2.6.29開始,默認集成了一個 relatime的屬性。使用這個特性來掛裝文件系統後,只有當mtime比atime更新的時候,才會更新atime。

使用場景:
在文件讀操作很頻繁的系統中,atime更新所帶來的開銷很大,所以在掛裝文件系統的時候使用noatime屬性來停止更新atime。但是有些程序需要根據atime進行一些判斷和操作,這個時候relatime特性就派上用場了。其實在事實上,這個時候atime和mtime已經是同一個time,所以可以理解這個選項就是為了實現對atime的兼容才推出的,並不是一個新的時間屬性。
使用方法: # mount -o relatime /dir##掛載目錄的時候加relatime參數

Copyright © Linux教程網 All Rights Reserved