天天看點

Linux下檔案的時間屬性

Linux下檔案的時間屬性

每個檔案都有三個主要的時間屬性,這三個時間屬性的意義是什麼呢?

  • modify time (mtime)

   當該檔案的“内容資料”更改時,就會更新這個時間。内容資料指的是檔案的内容,而不是檔案的屬性或權限。

  • change time (ctime)

   當該檔案的“狀态、中繼資料”更改時,就會更新這個時間。比如更改檔案權限、屬主、屬組、檔案名等中繼資料。

  • access time (atime)

   當該檔案的“内容被通路、讀取”時,就會更新這個讀取時間(access)。比如通過cat,more指令去讀取檔案,那麼就會更新該檔案的atime了。

如何【檢視】檔案的三個時間屬性?

stat指令, 可以顯示檔案或者檔案系統狀态。

文法:
stat [OPTION]... FILE...

[root@web ~]# stat strerror.c 
  File: `strerror.c'
  Size: 201           Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d    Inode: 143765      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-02-11 21:00:46.976652765 +0800
Modify: 2016-02-11 21:00:40.286651956 +0800
Change: 2016-02-11 21:00:40.342654314 +0800      

預設情況下, ls 顯示出來的是該檔案的 mtime, 也就是這個檔案的内容上次被更改的時間。

[root@web ~]# ls -l strerror.c 
-rw-r--r-- 1 root root 201 Feb 11  2016 strerror.c
[root@web ~]# ls -l --time=atime strerror.c 
-rw-r--r-- 1 root root 201 Feb 11  2016 strerror.c
[root@web ~]# ls -l --time=ctime strerror.c 
-rw-r--r-- 1 root root 201 Feb 11  2016 strerror.c      

如何【修改】檔案的三個時間屬性?

touch指令可以修改檔案的時間屬性。

touch [OPTION]... FILE...

-a     change only the access time
-m     change only the modification time 
-r, --reference=FILE    use this file’s times instead of current time      

針對 access time 沒有改變的問題

有時候,用 cat filename,或者 more filename 通路相應檔案時,并沒有重新整理檔案的 access time ,而是在修改了檔案内容之後,才重新整理了access time, 以及 modity time。

官方解釋:

在kernel版本 2.6.30之前, Linux的核心開發人員針對 ext3/ext4檔案系統的性能進行讨論,其中包括 atime。 在 kernel 2.6.30 之前,檔案系統中預設會及時的更新atime,這樣會帶來兩個問題。

(1)系統中大量的檔案通路,将atime寫入到磁盤中,消耗時間,進而降低系統性能

(2)頻繁的更新操作,也會消耗大量電能

其實在Linux上很少程式需要擷取精确的atime時間,并且Linux核心開發人員從ext3/ext4檔案系統的性能角度出發,決定在2.6.30版本的内容中修改atime的更新方式,隻有在以下三種情況之一才會更新atime:

(1)如果将分區mount挂載的時候指定采用非relatime方式(預設采用 relatime方式),如 strictatime。補充:在OS啟動的時候,将各個分區挂載到不同的目錄,在挂載(mount)的參數中采用 strictatime,表明及時更新atime。在2.6.30之後mount添加了 relatime 和 strictatime 兩個選項,詳細的可以通過 man mount 檢視。

(2)atime 小于 ctime 或者 小于 mtime 的時候

(3)本次的 access time 和 上次的 access time 超過24個小時

繼續閱讀