使用 touch 更新文件時間
$ll new.txt
保證輸出:ls: 無法訪問 new.txt: 沒有那個文件或目錄
$touch new.txt
$ll new.txt
-rw-r--r-- 1 root root 0 7月 12 16:56 new.txt
如果此文件已經(jīng)存在的情況下.更改文件時間為當前時間
$touch new.txt
-rw-r--r-- 1 root root 0 7月 12 16:57 new.txt
案例:更改文件時間為指定時間
$date
2015年 07月 12日 星期日 16:59:10 CST
$touch -t 11111111 new.txt
$ll new.txt
-rw-r--r-- 1 root root 0 11月 11 2015 new.txt
分析:此處指定文件的時間格式為:yyyy(年)MM(月)DD(日)hh(時)mm(分),省略在表示使用當前系統(tǒng)的時間.
案例:將文件改正與別的文件相同的時間
$ll new.txt
-rw-r--r-- 1 root root 0 7月 12 17:03 new.txt
$ll /etc/passwd
-rw-r--r-- 1 root root 1804 6月 10 23:27 /etc/passwd
$touch -r /etc/passwd new.txt
$ll new.txt
-rw-r--r-- 1 root root 0 6月 10 23:27 new.txt
總結(jié):linux 中 touch 命令參數(shù)不常用,一般在使用 make 的時候可能會用到,用來修改文件時間戳,或者新建一個不存在的文件.
語法:touch [-acdmt] 文件參數(shù)
$find /tmp -exec touch -t 11111111 {} \;
$ll /tmp
總用量 12
drwxr-xr-x 2 root root 4096 11月 11 2015 hidden
-rw-r--r-- 1 root root 0 11月 11 2015 new.txt
drwxr-xr-x 2 root root 4096 11月 11 2015 test
-rwxr-xr-x 1 root root 385 11月 11 2015 touch.sh
分析:可把/tmp 下的所有文件和目錄都改變修改時間。
主要選項和作用
| 參數(shù) | 作用 |
|---|---|
| -a | 僅修改文件的最后訪問時間 |
| -c | 僅修改時間,而不創(chuàng)建文件 |
| -d | 后面可以接日期,也可以使用-date=”如期或時間” |
| -m | 僅修改文件的修改時間 |
| -t | 后面可接時間,格式為[yyyyMMDDhhmm] |