通過touch這個命令,我們可以輕易修改檔案的日期與時間,並且也可以建立乙個空的檔案。不過,要注意的是,即使我們複製乙個檔案時,複製了所有的屬性,但也沒有辦法複製ctime這個屬性的。ctime可以記錄這個檔案最近的狀態(status)被改變的時間。無論如何,還是要告知大家,我們平時看的檔案屬性中,比較重要的還是屬於那個mtime。我們經常關心的是這個檔案的「內容」是什麼時候被改動過的。
無論如何,touch這個命令最常被使用的情況是:
建立乙個空的檔案;
將某個檔案日期修改為目前的日期(mtime與atime)。
命令格式:
touch [選項]... 檔案...
命令引數:
-a 僅修改訪問時間。
-c 僅修改檔案的時間,不建立任何檔案。
-d 後面可以接欲修改的日期而不用當前的日期,可以使用各種不同的格式。
-f 不使用,是為了與其他 unix 系統的相容性而保留。
-m 只更改變動時間mtime。
-r 把指定文件或目錄的日期時間,統統設成和參考文件或目錄的日期時間相同。
-t 使用指定的日期時間,而非現在的時間,格式與 date 指令相同。
命令示例:
1.將123.txt檔案,日期調整為前二天。當前日期2016.4.22
1
2
3
4
5
6
7
8
[root@w zdw]
# ll
total 4
-rw-r--r--. 1 root root 4 apr 22 16:49 123.txt 22日
[root@w zdw]
# touch -d "2 days ago" 123.txt
[root@w zdw]
# ll 123.txt; ll --time=atime 123.txt; ll --time=ctime 123.txt
-rw-r--r--. 1 root root 4 apr 20 16:49 123.txt
-rw-r--r--. 1 root root 4 apr 20 16:49 123.txt
-rw-r--r--. 1 root root 4 apr 22 16:49 123.txt
上面的示例,本來是22日的變成了20日(atime/mtime)
不過,ctime並沒有跟著改變。
2.將123.txt日期改為2016/4/10 02:031
2
3
4
5
[root@w zdw]
# touch -t 1604100203 123.txt
[root@w zdw]
# ll 123.txt; ll --time=atime 123.txt; ll --time=ctime 123.txt
-rw-r--r--. 1 root root 4 apr 10 02:03 123.txt
-rw-r--r--. 1 root root 4 apr 10 02:03 123.txt
-rw-r--r--. 1 root root 4 apr 22 16:57 123.txt
日期atime與mtime都改變了,但是ctime則是記錄目前的時間。
3.-c引數,如果檔案不存在,則不建立1
2
3
4
[root@w zdw]
# touch -c 456.txt
[root@w zdw]
# ll
total 4
-rw-r--r--. 1 root root 4 apr 10 02:03 123.txt
4.-r引數,更新123.txt時間和456.txt時間戳相同,以123.txt時間為準1
2
3
4
5
6
7
8
9
10
[root@w zdw]
# touch 456.txt
[root@w zdw]
# ll
total 4
-rw-r--r--. 1 root root 4 apr 10 02:03 123.txt
-rw-r--r--. 1 root root 0 apr 22 17:03 456.txt
[root@w zdw]
# touch -r 123.txt 456.txt
[root@w zdw]
# ll
total 4
-rw-r--r--. 1 root root 4 apr 10 02:03 123.txt
-rw-r--r--. 1 root root 0 apr 10 02:03 456.txt
linux命令 touch命令
touch命令將每個檔案的訪問時間和修改時間改為當前時間。touch 選項 檔案 1 在當前目錄下建立乙個空檔案a.log root vm 0 4 centos chenwei touch a.txt root vm 0 4 centos chenwei ll total 60 rw r r 1 r...
Linux命令分析 touch
用途 更改檔案的時間戳,常用來建立新的空檔案 用法 touch 選項.檔案.touch命令可用來更改檔案的atime和mtime到當前時間,如果touch命令後接的檔案不存在,則會建立乙個該檔名的空檔案 除非有 c或 h引數 引數 a 只更改atime c no create 不建立任何檔案 d d...
Linux基礎命令 touch
檔案處理 touch命令有兩個功能 一是用於把已存在檔案的時間標籤更新為系統當前的時間 預設方式 它們的資料將原封不動地保留下來 二是用來建立新的空檔案。touch 選項 引數 選項 a 或 time atime或 time access或 time use 只更改訪問時間 c 或 no creat...