Linux 檔案時間詳解 ctime mtime atime

來源:互聯網
上載者:User

標籤:atime   mtime   ctime   檔案時間   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參數


 

Linux 檔案時間詳解 ctime mtime atime

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.