Linux(11)RedHat7 基本命令九-touch命令詳解
阿新 • • 發佈:2018-12-14
導言
前一篇博文介紹了目錄,並且其實開篇導言,介紹了linux將一切看成是檔案,所以對於本次博文,講解的touch命令。本命令用於建立檔案而準備的。【感覺換個字型會不會看得更好點?】
touch命令
作用
用於修改檔案或者目錄的時間屬性,包括存取時間和更改時間。若檔案不存在,系統會建立一個新的檔案。 該命令會將每個檔案的訪問時間和修改時間改為當前時間。 不存在的檔案將會被建立為空檔案,除非使用-c 或-h 選項。 如果檔名為"-"則特殊處理,更改與標準輸出相關的檔案的訪問時間。
用法
touch [選項]... 檔案...
詳細全選項引數解釋
短選項 | 長選項 | 含義(作用) |
---|---|---|
-a |
只更改訪問時間 | |
-c |
--no-create |
不建立任何檔案 |
-d |
--date=字串 |
使用指定字串表示時間而非當前時間 |
-f |
此引數將忽略不予處理,僅負責解決BSD版本touch指令的相容性問題。 | |
-h |
--no-dereference |
會影響符號連結本身,而非符號連結所指示的目的地(當系統支援更改符號連結的所有者時,此選項才有用) |
-m |
只更改修改時間 | |
-r |
--reference=檔案 |
使用指定檔案的時間屬性而非當前時間 |
-t STAMP |
使用[[CC]YY]MMDDhhmm[.ss] 格式的時間而非當前時間 | |
--time=WORD |
使用WORD 指定的時間:access、atime、use 都等於-a選項的效果,而modify、mtime 等於-m 選項的效果 | |
--help |
顯示此幫助資訊並退出 | |
--version |
顯示版本資訊並退出 |
請注意,-d
和-t
選項可接受不同的時間/日期格式。
常見命令使用
實踐一
同時建立一個或者多個空檔案
[[email protected] test]$ touch a.txt
[[email protected] test]$ ls
a.txt
[[email protected] test]$ touch b.txt c.txt d.txt
[ [email protected] test]$ ls
a.txt b.txt c.txt d.txt
[[email protected] test]$ touch {1..5}.txt
[[email protected] test]$ ls
1.txt 2.txt 3.txt 4.txt 5.txt a.txt b.txt c.txt d.txt
實踐二
檢視檔案的修改時間mtime,訪問時間atime,屬性或狀態改變時間ctime。
[[email protected] test]$ cp -a ~/.bashrc bashrc
[[email protected] test]$ ls
1.txt 2.txt 3.txt 4.txt 5.txt a.txt bashrc b.txt c.txt d.txt
[[email protected] test]$ date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
2018年 10月 12日 星期五 00:11:03 CST <=== 這是目前的時間
-rw-r--r--. 1 tqw tqw 124 7月 9 2013 bashrc <=== mtime
-rw-r--r--. 1 tqw tqw 124 10月 11 00:17 bashrc <=== atime
-rw-r--r--. 1 tqw tqw 124 10月 12 00:10 bashrc <=== ctime
實踐三
修改實踐二的 bashrc 檔案,將日期調整為兩天前。
[[email protected] test]$ touch -d "2 days ago" bashrc
[[email protected] test]$ date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
2018年 10月 12日 星期五 00:18:25 CST
-rw-r--r--. 1 tqw tqw 124 10月 10 00:18 bashrc
-rw-r--r--. 1 tqw tqw 124 10月 10 00:18 bashrc
-rw-r--r--. 1 tqw tqw 124 10月 12 00:18 bashrc
與實踐二的 bashrc 檔案相比,mtime和atime改變成時間為10月10日,而ctime日期並未改變。
實踐四
將案例三的bashrc檔案,改變時間為2018年10月9日14點25分,以時間戳形式。
[[email protected] test]$ touch -t 201810091425 bashrc
[[email protected] test]$ date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
2018年 10月 12日 星期五 00:25:31 CST
-rw-r--r--. 1 tqw tqw 124 10月 9 14:25 bashrc
-rw-r--r--. 1 tqw tqw 124 10月 9 14:25 bashrc
-rw-r--r--. 1 tqw tqw 124 10月 12 00:25 bashrc
bashrc 檔案的mtime和atime改變成時間為設定時間了,而ctime日期並未改變。
總結
本文介紹了touch命令,該命令主要是用於建立新檔案和修改檔案相關時間屬性。