touch#
touch 是包含在 GNU Coreutils 內的檔案時間戳記修改與建立工具
touch 是 Linux 系統中用於修改檔案「存取時間 (atime)」與「修改時間 (mtime)」的指令。如果指定的檔案不存在,它會預設建立一個新的空檔案。在開發環境中,
touch常用於快速產生測試檔案,或透過更新時間戳記來觸發編譯工具(如 make)的重新建置流程。
Install#
$ sudo apt install coreutilsSetting in up#
Operate#
建立一個空檔案#
touch new_file.txt
同時建立多個空檔案#
touch file1 file2 file3
將檔案時間更新為與另一個檔案相同#
touch -r reference_file target_file
| 參數 | 範例指令 | 說明 |
|---|---|---|
-a | touch -a file.txt | 僅修改存取時間。只更新 atime,保持 mtime 不變。 |
-m | touch -m file.txt | 僅修改變動時間。只更新 mtime,保持 atime 不變。 |
-c | touch -c non_exist | 不建立檔案。如果檔案不存在,則不建立新檔案(也稱為 --no-create)。 |
-t | touch -t 202501011200 file | 設定特定時間。格式為 [[CC]YY]MMDDhhmm[.ss],設定檔案至過去或未來時間。 |
-d | touch -d "2 days ago" file | 使用描述性時間。接受字串格式(如 “next Friday” 或 “10:00am”)來設定時間。 |
-r | touch -r ref_file target | 參考時間。將目標檔案的時間戳記設定成與參考檔案(Reference)完全一致。 |
Reference#
Official docs: