Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 的 touch 指令怎麼用?時間戳操作完整整理

說到更新檔案或目錄的時間,Linux 下最直覺的工具就是 touch。它不只是「建立空檔」這麼單純,能做的事其實不少。

Linux 的 touch 指令怎麼用?時間戳操作完整整理

基本用法

名稱:touch
使用權限:所有使用者

touch [-acfm] [-r reference-file] [--file=reference-file]
[-t MMDDhhmm[[CC]YY][.ss]]
[-d time] [--date=time] [--time={atime,access,use,mtime,modify}]
[--no-create] [--help] [--version] file1 [file2 ...]

它主要用來變更檔案的時間記錄,而 ls -l 顯示的正是這些資訊。

參數速查

a 只改存取時間;m 只改修改時間;c 目標不存在時不建立新檔,等同 --no-create;f 保留相容性,實際不使用;r 沿用參考檔的時間;d 以自訂格式指定日期時間;t 用類似 date 的格式指定時間。

實例一:一次建立多個空檔

touch log2012.log log2013.log

-rw-r--r-- 1 root root 0 10-28 16:01 log2012.log
-rw-r--r-- 1 root root 0 10-28 16:01 log2013.log

若加上 -c,不存在的檔案就不會被建立:

touch -c log2014.log

實例二:把時間戳對齊另一個檔案

touch -r log.log log2012.log

執行後 log2012.log 的時間會被改成與 log.log 相同的 14:48,這在同步或觸發建置流程時很好用。

Copyright © Linux教程網 All Rights Reserved