Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 檔案三大時間屬性詳解:atime、mtime、ctime 差異與修改方法

編輯:Linux 系統常見問題解答

三大時間定義

縮寫全名中文觸發情境查看指令
atimeAccess Time存取時間讀取檔案內容、執行腳本、cat, less, grep 等ls -lu
mtimeModify Time修改時間檔案資料內容變更(echo > file, vim 存檔)ls -l (預設)
ctimeChange Time狀態改變時間inode 屬性變更:權限、擁有者、連結數、移動、重命名ls -lc

關鍵差異口訣

  • 內容變 → mtime + ctime 同步更新(因大小、區塊指標屬於 inode 屬性)。
  • 屬性變 → ctime 更新,mtime 不變(chmod, chown, mv, ln)。
  • 單純讀取 → atime 更新,mtime/ctime 不變。
  • 目錄操作:ls 目錄 → 目錄 atime 更新;在目錄建檔 → 目錄 mtime+ctime 更新,atime 不一定變。

查看完整時間資訊

stat filename
# 輸出包含 Access, Modify, Change 三欄完整時間戳(含奈秒)

修改時間戳記

touch 指令

touch -a -t 202401010000 file   # 只改 atime
touch -m -t 202401010000 file   # 只改 mtime (ctime 會跟著變)
touch -d "2024-01-01 00:00" file # 同時改 atime/mtime (ctime 變)

注意:touch 無法單獨修改 ctime(ctime 代表 inode 狀態改變時間,系統自動維護)。若需偽造 ctime,需修改系統時間後操作,或使用 debugfs 直接操作檔案系統(極不建議)。

實務應用場景

  • find /path -mtime +30:找出 30 天前修改的檔案(清理舊日誌)。
  • find /path -atime +90:找出 90 天未存取的檔案(候選歸檔)。
  • dump 備份工具依據 ctime 判斷增量備份。
  • 檔案同步工具(rsync)預設比對 mtime+size。

小結

搞懂 atime/mtime/ctime 語意,能精準設計備份、清理、同步、稽核策略。善用 stat 觀察、touch -a/-m 微調、find -newer 比對,掌握檔案時間全貌。

Copyright © Windows教程網 All Rights Reserved