Linux系統(tǒng)下touch命令的使用方法

1.命令格式:
touch [選項(xiàng)]... 文件...
2.命令參數(shù):
-a 或--time=atime或--time=access或--time=use 只更改存取時(shí)間。
-c 或--no-create 不建立任何文檔。
-d 使用指定的日期時(shí)間,而非現(xiàn)在的時(shí)間。
-f 此參數(shù)將忽略不予處理,僅負(fù)責(zé)解決BSD版本touch指令的兼容性問(wèn)題。
-m 或--time=mtime或--time=modify 只更改變動(dòng)時(shí)間。
-r 把指定文檔或目錄的日期時(shí)間,統(tǒng)統(tǒng)設(shè)成和參考文檔或目錄的日期時(shí)間相同。
-t 使用指定的日期時(shí)間,而非現(xiàn)在的時(shí)間。
3.命令功能:
touch命令參數(shù)可更改文檔或目錄的日期時(shí)間,包括存取時(shí)間和更改時(shí)間。
4.使用范例:
實(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的時(shí)間和log2012.log時(shí)間戳相同
命令:
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è)定文件的時(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
說(shuō)明:
-t time 使用指定的時(shí)間值 time 作為指定文件相應(yīng)時(shí)間戳記的新值.此處的 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í)數(shù)(幾點(diǎn)),mm為分鐘數(shù),SS為秒數(shù).此處秒的設(shè)定范圍是0--61,這樣可以處理閏秒.這些數(shù)字組成的時(shí)間是環(huán)境變量TZ指定的時(shí)區(qū)中的一個(gè)時(shí) 間.由于系統(tǒng)的限制,早于1970年1月1日的時(shí)間是錯(cuò)誤的。
指令名稱:
touch(1) - 更新檔案的時(shí)間紀(jì)錄
說(shuō)明:
藉由這個(gè)指令,我們可以同時(shí)或單獨(dú)變更檔案的兩種時(shí)間紀(jì)錄,讀取時(shí)間(access time)以及修改時(shí)間(modification time)。
選項(xiàng):
-a
更改文件的讀取時(shí)間(access time)
-c, --no-create
不建立任何新檔案
-m
更改文件的修改時(shí)間(modification time)
-t STAMP
設(shè)定"特定時(shí)間"來(lái)代替"目前時(shí)間",格式 [[CC]YY]MMDDhhmm[.ss]
--help
顯示程式用法資訊
--version
顯示程式本身的版本資訊
實(shí)例說(shuō)明:
空檔案
# touch file-touch1
# ls -l
total 0
-rw-r--r-- 1 root root 0 7月 9 13:53 file-touch1
#
一般狀況下使用 touch 指令變更檔案時(shí)間,如果檔案不存在,touch 會(huì)自動(dòng)建立指定的檔案(空檔案)。如果不想因使用 touch 而建立任何新檔案,必須加上選項(xiàng) -c
# touch -c file-touch2
touch: setting times of `file-touch2': 沒(méi)有此一檔案或目錄
#
指令的執(zhí)行結(jié)果是出現(xiàn)一段錯(cuò)誤訊息,因?yàn)橹付ǖ臋n案不存在。這正是加上選項(xiàng) -c 的目的。 更新檔案時(shí)間 touch 指令一般是以下指令當(dāng)時(shí)的電腦時(shí)間來(lái)作為更新檔案的時(shí)間。但也提供特殊的選項(xiàng) -t 來(lái)
指定"特定的時(shí)間"作為更新時(shí)間。為了舉例的方便,我原封不定地,拷貝一隻已建檔許久的程式 -- tcsh
# cp -a /bin/tcsh .
# ls --full-time
total 288
-rwxr-xr-x 1 root root 288604 週一 6月 25 03:45:26 2001 tcsh
#
這隻程式的最後修改時(shí)間是 -- 2001 年 6 月 25 日 3 點(diǎn) 45 分 26 秒?,F(xiàn)在,我因監(jiān)控上的需要,打算將檔案時(shí)間變更為 2002 年 7 月 1 日 8 點(diǎn) 00 分 01 秒。最簡(jiǎn)單有效的作法如下
# touch -t 200207010800.01 tcsh
# ls --full-time
total 288
-rwxr-xr-x 1 root root 288604 週一 7月 01 08:00:01 2002 tcsh
選項(xiàng)的語(yǔ)法很單純,就是直接鍵入西元的年月日時(shí)分?jǐn)?shù)字即可,其間不需任何分隔。如果要加上秒數(shù),做法比較特別,需使用小點(diǎn)"."來(lái)與前段區(qū)隔。當(dāng)然,在有需要時(shí),touch 指令也支援"讀取時(shí)間"或"修改時(shí)間"的單獨(dú)變更。
# touch -at 200207022300 tcsh
# ls -lu
total 288
-rwxr-xr-x 1 root root 288604 7月 2 23:00 tcsh
#
上例是單獨(dú)變更"讀取時(shí)間", 下例則是變更"修改時(shí)間"
# touch -mt 200206302300 tcsh
# ls -l
total 288
-rwxr-xr-x 1 root root 288604 6月 30 23:00 tcsh
# ls -lu
total 288
-rwxr-xr-x 1 root root 288604 7月 2 23:00 tcsh
#
以上這些例子,即是大體上 touch 的運(yùn)用。這篇就暫時(shí)先寫(xiě)到這裡好了。
相關(guān)文章
虛擬機(jī)安裝CentOS后沒(méi)有網(wǎng)絡(luò)怎么辦
虛擬機(jī)安裝CentOS后沒(méi)有網(wǎng)絡(luò)了不能上網(wǎng),該怎么設(shè)置網(wǎng)絡(luò)呢?下面我們就來(lái)看看這個(gè)問(wèn)題的解決辦法,詳細(xì)請(qǐng)看下文圖文介紹2024-01-18虛擬機(jī)怎么安裝CentOS? 安裝CentOS操作系統(tǒng)的保姆級(jí)圖文教程
虛擬機(jī)想要安裝CentOS操作系統(tǒng),但是CentOS操作系統(tǒng)已經(jīng)停止維護(hù)了,該怎么安裝呢?詳細(xì)請(qǐng)看下文圖文教程2024-01-18Centos7.8怎么更新openssh? Centos升級(jí)openssh的技巧
Centos7.8怎么更新openssh?Centos7.8系統(tǒng)想要安裝openssh,該怎么安裝呢?下面我們就來(lái)看看Centos升級(jí)openssh的技巧2023-09-02Centos7.8怎么更新openssl? CentOS升級(jí)OpenSSL的技巧
Centos7.8怎么更新openssl?Centos7.8系統(tǒng)想要升級(jí)OpenSSL,該怎么升級(jí)呢?詳細(xì)請(qǐng)看下文介紹2023-09-02- centos7沒(méi)有圖形化操作可能對(duì)很多人來(lái)說(shuō)都不太習(xí)慣,下面我們來(lái)為centos7安裝圖形化界面,本文以安裝 GNOME 圖形化為例,需要的朋友可以參考下2023-06-29
- 今天小編在安裝RHEL7的時(shí)候,一步留神沒(méi)有安裝圖形化桌面,下面分享一下安裝圖形化桌面的過(guò)程,需要的朋友可以參考下2023-06-29
CentOS7各個(gè)版本鏡像下載地址及版本說(shuō)明(包括Everything版)
下載CentOS-7.0-1406的時(shí)候,有很多可選則的版本,對(duì)于普通用戶來(lái)說(shuō),不知道選擇哪個(gè)好,下面做一下簡(jiǎn)單介紹,需要的朋友可以參考下2023-06-01Centos 7怎么手動(dòng)配置ip地址? Centos7配置IP地址的技巧
Centos 7怎么手動(dòng)配置ip地址?Centos 7系統(tǒng)想要自己配置ip地址,該怎么操作呢?下面我們就來(lái)看看Centos7配置IP地址的技巧2023-05-17- 這篇文章主要介紹了Centos 7 壓縮與解壓縮命令小結(jié),需要的朋友可以參考下2023-03-28
CentOS8使用阿里云yum源異常問(wèn)題及解決方法
由于centos8 在2022年停止服務(wù),后繼版本為8-steam。在使用阿里云的 centos8 的 yum 時(shí)報(bào)錯(cuò),這篇文章主要介紹了CentOS8使用阿里云yum源異常的解決方法,需要的朋友可以參考2022-04-19