Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux 檔案的三種時間:mtime、ctime、atime 與 touch

三個時間戳

mtime(修改時間)

檔案「內容」被改寫時更新,權限或屬性的變動不算在內。ls -l 預設顯示的就是它。

$ ls -l uconv.h
-rw-rw-r-- 1 work work 1808 Jul 23 2013 uconv.h

ctime(狀態變更時間)

inode 的 metadata 變動時更新,例如 chmod、chown、改名。注意 c 不是 create,這是最常見的誤解。

$ ls -l --time=ctime uconv.h

atime(存取時間)

$ ls -l --time=atime uconv.h

檔案內容被讀取時更新。正因為每次讀檔都要寫回 atime,效能導向的掛載常加上 noatime 或 relatime 以避免額外寫入。

用 touch 修改時間

touch --help
-a                    只改 atime
-c, --no-create       不建立新檔
-d, --date=STRING     指定時間
-m                    只改 mtime
-r, --reference=FILE  沿用另一檔案的時間
-t STAMP              [[CC]YY]MMDDhhmm[.ss]

不帶參數時,touch 會同時把 atime 與 mtime 設為現在;若檔案不存在則建立一個空檔。

$ touch -d "2 days ago" uconv.h
$ ls -l uconv.h ; ls -l --time=atime uconv.h ; ls -l --time=ctime uconv.h
-rw-rw-r-- 1 work work 1808 Jun 13 18:17 uconv.h
-rw-rw-r-- 1 work work 1808 Jun 13 18:17 uconv.h
-rw-rw-r-- 1 work work 1808 Jun 15 18:17 uconv.h

mtime 與 atime 被設為兩天前,ctime 仍是現在——因為 ctime 無法被使用者任意設定,任何 metadata 操作都會把它推回當前時刻。

$ touch -t 201406142020 uconv.h
$ ls -l uconv.h ; ls -l --time=atime uconv.h ; ls -l --time=ctime uconv.h
-rw-rw-r-- 1 work work 1808 Jun 14 20:20 uconv.h
-rw-rw-r-- 1 work work 1808 Jun 14 20:20 uconv.h
-rw-rw-r-- 1 work work 1808 Jun 15 18:23 uconv.h

cp -a 保留原樣

$ cp -a uconv.h uconv.h1
$ ls -l uconv.h1 ; ls -l --time=atime uconv.h1 ; ls -l --time=ctime uconv.h1
-rw-rw-r-- 1 work work 1808 Jun 14 20:20 uconv.h1
-rw-rw-r-- 1 work work 1808 Jun 15 18:25 uconv.h1
-rw-rw-r-- 1 work work 1808 Jun 15 18:27 uconv.h1

mtime 與 atime 完整保留,ctime 則變成複製當下的時間——再次印證 ctime 不可偽造。實務上這三個時間戳的組合很有用:以 mtime 找最近改動的檔案、以 ctime 判斷權限是否被人動過、以 atime 找長期未被存取的冷資料,都是常見的巡檢手法。

Copyright © Linux教程網 All Rights Reserved