2 回答

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個贊
touch命令用來修改文件的訪問時間、修改時間。如果沒有指定時間,則將文件時間屬性改為當(dāng)前時間。當(dāng)指定文件不存在,則touch命令變?yōu)閯?chuàng)建該文件。
語法:
touch [-acm] [-d STRING-time] [-r reference-file] [-t [[CC]YY]MMDDhhmm[.ss]]
選項(xiàng)介紹:
-a: 只修改訪問時間;
-c: 如果指定文件不存在,則不創(chuàng)建文件;
-d STRING-time: 用字符串格式的時間來指定時間屬性的修改值;
-m: 只修改指定文件的修改時間;
-r refernce-file: 將指定文件的時間屬性改為與reference-file時間屬性相同的值;
-t [[CC]YY]MMDDhhmm[.ss]: 用[[CC]YY]MMDDhhmm[.ss]這種時間格式來指定時間屬性的修改值;
使用范例:
實(shí)例一:創(chuàng)建不存在的文件
命令:
touch log2012.log log2013.log
輸出:
[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
如果log2014.log不存在,則不創(chuàng)建文件
[root@localhost test]# touch -c log2014.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
實(shí)例二:更新log.log的時間和log2012.log時間戳相同
命令:
touch -r log.log log2012.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
-rw-r--r-- 1 root root 0 10-28 14:48 log.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
實(shí)例三:設(shè)定文件的時間戳
命令:
touch -t 201211142234.50 log.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
[root@localhost test]# touch -t 201211142234.50 log.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 2012-11-14 log.log
說明:
-t time 使用指定的時間值 time 作為指定文件相應(yīng)時間戳記的新值.此處的 time規(guī)定為如下形式的十進(jìn)制數(shù):
[[CC]YY]MMDDhhmm[.SS]
這里,CC為年數(shù)中的前兩位,即”世紀(jì)數(shù)”;YY為年數(shù)的后兩位,即某世紀(jì)中的年數(shù).如果不給出CC的值,則touch 將把年數(shù)CCYY限定在1969--2068之內(nèi).MM為月數(shù),DD為天將把年數(shù)CCYY限定在1969--2068之內(nèi).MM為月數(shù),DD為天數(shù),hh 為小時數(shù)(幾點(diǎn)),mm為分鐘數(shù),SS為秒數(shù).此處秒的設(shè)定范圍是0--61,這樣可以處理閏秒.這些數(shù)字組成的時間是環(huán)境變量TZ指定的時區(qū)中的一個時 間.由于系統(tǒng)的限制,早于1970年1月1日的時間是錯誤的。

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個贊
touch命令沒有創(chuàng)建多層目錄的功能的,建議還是用mkdir -p和touch命令的組合,可以考慮用管道實(shí)現(xiàn)在一條命令里創(chuàng)建。
添加回答
舉報