Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 檔案時間屬性深度解析:atime、mtime、ctime 與「建立時間」迷思

編輯:Linux 教學

Linux 沒有「檔案建立時間」 (Birth Time)

傳統 Unix/Linux 檔案系統 (ext2/3/4、XFS、Btrfs 早期版本) **不記錄檔案建立時間**。僅保留三個時間戳:

  • atime (Access Time): 最後一次**讀取**檔案內容時間 (如 cat, more, grep)。ls, stat **不更新** atime。
  • mtime (Modify Time): 最後一次**修改檔案內容**時間 (如 vi 存檔、echo >> file)。ls -l 顯示的即為 mtime。
  • ctime (Change Time): 最後一次**改變 inode 中繼資料**時間 (權限 chmod、擁有者 chown、連結數變更、移動檔案)。**非內容變更**。

何時可推測建立時間?

  • 檔案建立後**從未修改內容** → mtime ≈ 建立時間。
  • 檔案建立後**從未變更屬性** → ctime ≈ 建立時間。
  • 檔案建立後**從未被讀取** → atime ≈ 建立時間 (極罕見)。

以上皆為推測,**無法保證**。

查看完整時間資訊:stat 指令

[user@host ~]$ stat test.txt
  File: 'test.txt'
  Size: 66            Blocks: 8          IO Block: 4096   regular file
Device: 807h/2055d    Inode: 1191481     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 500/user)   Gid: ( 500/user)
Access: 2024-05-20 10:30:00.000000000 +0800   # atime
Modify: 2024-05-20 10:25:00.000000000 +0800   # mtime
Change: 2024-05-20 10:25:00.000000000 +0800   # ctime
 Birth: -                                       # ext4 未啟用 birth time 顯示 '-'

現代檔案系統支援 Birth Time (建立時間)

檔案系統核心版本支援狀態查看方式
ext44.11+支援 (需 debugfs 或 statx 系統呼叫)debugfs -R 'stat /path/file' /dev/sdX 觀察 crtime
Btrfs4.1+原生支援stat (glibc 2.28+)、btrfs inspect-internal rootid
XFS5.10+支援 (v5 超級區塊)stat、xfs_db
ZFS (OpenZFS)-原生支援zfs get creation

實務建議

  1. 若應用需精確建立時間 (如審計、同步、備份),**採用支援 Birth Time 的檔案系統 (ext4 4.11+/Btrfs/XFS v5/ZFS) 並使用相容工具 (coreutils 8.32+、glibc 2.28+)**。
  2. 傳統 ext4/ext3 環境下,勿依賴 mtime/ctime 作為建立時間依據。
  3. 掛載選項 noatime、relatime 可減少 atime 更新 I/O,提升效能。
  4. touch -a 僅更新 atime,touch -m 僅更新 mtime,touch 同時更新 atime/mtime (ctime 亦會更新)。
Copyright © Windows教程網 All Rights Reserved