歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> Linux指令篇:檔案目錄管理--touch

Linux指令篇:檔案目錄管理--touch

日期:2017/3/6 15:40:36   编辑:關於Unix
名稱:touch 使用權限:所有使用者 使用方式: touch[-acfm] [-rreference-file][--file=reference-file] [-tMMDDhhmm[[ CC ]YY][.ss]] [-dtime][--date=time][--time=atime,access,use,mtime,modify] [--no-create][--help][--version] file1[file2...] 說 名稱:touch

使用權限:所有使用者

使用方式:
touch [-acfm]
[-r reference-file] [--file=reference-file]
[-t MMDDhhmm[[CC]YY][.ss]]
[-d time] [--date=time] [--time={atime,aclearcase/" target="_blank" >ccess,use,mtime,modify}]
[--no-create] [--help] [--version]
file1 [file2 ...]


說明:
touch 指令改變檔案的時間記錄。 ls -l 可以顯示檔案的時間記錄。


參數:
a 改變檔案的讀取時間記錄。
m 改變檔案的修改時間記錄。
c 假如目的檔案不存在,不會建立新的檔案。與 --no-create 的效果一樣。
f 不使用,是為了與其他 unix 系統的相容性而保留。
r 使用參考檔的時間記錄,與 --file 的效果一樣。
d 設定時間與日期,可以使用各種不同的格式。
t 設定檔案的時間記錄,格式與 date 指令相同。
--no-create 不會建立新檔案。
--help 列出指令格式。
--version 列出版本訊息。


范例:


最簡單的使用方式,將檔案的時候記錄改為現在的時間。若檔案不存在,系統會建立一個新的檔案。



touch file
touch file1 file2



將 file 的時間記錄改為 5 月 6 日 18 點 3 分,公元兩千年。時間的格式可以參考 date 指令,至少需輸入 MMDDHHmm ,就是月日時與分。



touch -c -t 05061803 file
touch -c -t 050618032000 file



將 file 的時間記錄改變成與 referencefile 一樣。



touch -r referencefile file



將 file 的時間記錄改成 5 月 6 日 18 點 3 分,公元兩千年。時間可以使用 am, pm 或是 24 小時的格式,日期可以使用其他格式如 6 May 2000 。


touch -d "6:03pm" file
touch -d "05/06/2000" file
touch -d "6:03pm 05/06/2000" file




touch 也可以制造一個空檔(0 byte).例如DHCP Server所需的/etc/dhcpd.leases,dhcpd 必須要有這個檔案才能運作正常.[root@/root]#touch /etc/dhcpd.leases[root@/root]#ls -l /etc/dhcpd.leases-rw-r--r-- 1 root root 0 Jul 3 05:50 /etc/dhcpd.leases


記得上一次重灌前把/etc下的設定檔tar起來,重灌好之後把原有設定還原,卻發現系統檢查設定檔的時間有問題,這個時候用
find /etc -name * -exec touch {};

就可以把設定檔的時間更新到與現在一致了。

Copyright © Linux教程網 All Rights Reserved