touch#

touch 是包含在 GNU Coreutils 內的檔案時間戳記修改與建立工具

touch 是 Linux 系統中用於修改檔案「存取時間 (atime)」與「修改時間 (mtime)」的指令。如果指定的檔案不存在,它會預設建立一個新的空檔案。在開發環境中,touch 常用於快速產生測試檔案,或透過更新時間戳記來觸發編譯工具(如 make)的重新建置流程。

Install#

$ sudo apt install coreutils

Setting in up#


Operate#

建立一個空檔案#

touch new_file.txt

同時建立多個空檔案#

touch file1 file2 file3

將檔案時間更新為與另一個檔案相同#

touch -r reference_file target_file

參數範例指令說明
-atouch -a file.txt僅修改存取時間。只更新 atime,保持 mtime 不變。
-mtouch -m file.txt僅修改變動時間。只更新 mtime,保持 atime 不變。
-ctouch -c non_exist不建立檔案。如果檔案不存在,則不建立新檔案(也稱為 --no-create)。
-ttouch -t 202501011200 file設定特定時間。格式為 [[CC]YY]MMDDhhmm[.ss],設定檔案至過去或未來時間。
-dtouch -d "2 days ago" file使用描述性時間。接受字串格式(如 “next Friday” 或 “10:00am”)來設定時間。
-rtouch -r ref_file target參考時間。將目標檔案的時間戳記設定成與參考檔案(Reference)完全一致。

Reference#

Official docs: