Linux 檔案擁有三種時間戳記:**atime(最後存取)、mtime(最後內容修改)、ctime(最後狀態變更)**。touch 不僅能建立空檔,更是精準操控 atime/mtime 的利器。本文詳細解析參數用法、日期格式、批次修改實戰、偽造時間戳的鑑識對策,讓你駕輕就熟掌握時間操作術。

| 短參數 | 長參數 | 功能 |
|---|---|---|
-a | --time=atime / --time=access / --time=use | 僅修改 atime(存取時間) |
-m | --time=mtime / --time=modify | 僅修改 mtime(修改時間) |
-c | --no-create | 不建立新檔案(僅修改既有檔案時間) |
-d | --date=STRING | 使用指定字串時間(支援相對/絕對格式) |
-t | — | 使用 [[CC]YY]MMDDhhmm[.ss] 格式時間 |
-r | --reference=FILE | 參考指定檔案的時間戳 |
| — | --help | 顯示說明 |
| — | --version | 顯示版本 |
GNU date 解析器極其靈活,支援:
"2023-10-13 18:30:00"、"13 Oct 2023"、"6:03pm""3 months ago"、"yesterday"、"last Friday"、"-2 weeks""2023-10-13 6:03pm"touch -d "3 months ago" *.sh
# 或指定具體日期
touch -d "2023-10-13" *.sh
執行前:
-rwxrwxr-x 1 oracle oinstall 8143 Jan 9 14:41 3.sh
-rw-r--r-- 1 root root 0 Jan 9 14:41 4.sh
執行後:
-rwxrwxr-x 1 oracle oinstall 8143 Oct 13 00:00 3.sh
-rw-r--r-- 1 root root 0 Oct 13 00:00 4.sh
touch -a -d "2023-01-01 00:00" important.log
touch -m -t 202310130000 report.pdf
touch -r reference.txt target.txt
touch -c newfile.txt # 若 newfile.txt 不存在,不建立,不報錯
# 格式:[[CC]YY]MMDDhhmm[.ss]
touch -t 202310131830.45 precise.txt # 2023-10-13 18:30:45
touch 無法單指令分別指定 atime/mtime,需兩步驟:
touch -m -d "2023-10-13 12:00" file.txt # 先改 mtime
touch -a -d "2023-10-13 18:00" file.txt # 再改 atime
| 偵測手段 | 原理 |
|---|---|
stat 檢視 ctime | ctime(inode 變更時間)無法被 touch 修改,若 mtime 早於 ctime 顯然異常 |
| 檔案系統日誌 | ext4/journal、XFS、ZFS 意圖日誌記錄實際寫入時間 |
| 備份/快照對比 | 對比歷史快照中的 mtime/atime |
| 應用層日誌 | 應用程式自身記錄的寫入時間(如資料庫 WAL、Web 伺服器 access log) |
警示: 偽造時間戳可能構成竄改電子證據、妨礙司法公正等法律風險,僅限測試、教學、合法授權範圍內使用。
stat file.txt
# 輸出範例
Access: 2023-10-13 18:30:00.000000000 +0800 # atime
Modify: 2023-10-13 12:00:00.000000000 +0800 # mtime
Change: 2023-10-13 18:35:00.000000000 +0800 # ctime
touch 看似簡單,實則是時間戳操作的瑞士刀。熟練掌握 -a/-m/-d/-t/-r/-c 組合,配合 GNU date 的豐富字串解析,即可應對批次修改、測試模擬、時區對齊等多元場景。但請記住:ctime 永遠記錄真相,誠實才是最好的系統管理策略。