Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux管理 >> Linux維護

Linux touch 命令用法:修改時間戳與建立空檔

cd、ls 天天用,touch 卻常被忽略。它其實很有用:能改檔案的時間戳記,也能一鍵產生不存在的空檔案,在 make 流程中尤其常見。

語法與參數

touch [選項]... 檔案...
  • -a:只改存取時間(atime)。
  • -c:不建立任何檔案。
  • -d:使用指定的日期時間。
  • -f:忽略不處理,僅為相容 BSD 版 touch。
  • -m:只改修改時間(mtime)。
  • -r:把目標檔案時間設成與參考檔案相同。
  • -t:以指定日期時間取代現在時間。

實例一:建立不存在的檔案

[root@localhost test]# touch log2012.log log2013.log
[root@localhost test]# ll
-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 則不會建立:

[root@localhost test]# touch -c log2014.log

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

[root@localhost test]# touch -r log.log log2012.log
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log2012.log
-rw-r--r-- 1 root root 0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log

log2012.log 的時間被改成與 log.log 一致。

實例三:指定時間戳

[root@localhost test]# touch -t 201211142234.50 log.log
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 2012-11-14 log.log

-t 的時間格式為 [[CC]YY]MMDDhhmm[.SS]:CC 是世紀、YY 是年、MM 月、DD 日、hh 時、mm 分、SS 秒(允許 0–61 以處理閏秒)。省略 CC 時年份會落在 1969–2068 之間;早於 1970-01-01 的時間無效。範例中的 2012 年 11 月 14 日 22:34:50 就是這樣解讀的。

需要的時候,touch 修改的是你指定或參考的時間,而非當前時間,這點別搞混。

Copyright © Linux教程網 All Rights Reserved