溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

touch 修改文件時間或者創(chuàng)建文件

發(fā)布時間:2020-07-05 04:27:03 來源:網(wǎng)絡(luò) 閱讀:6732 作者:陳國成 欄目:編程語言

功能:對已經(jīng)存在文件的時間進行修改,存取時間(access time)、修改時間(modification time)。對不存在的文件,進行創(chuàng)建新的空白文件。

短選項長選項含義
-a–time=atime
或–time=access
或–time=use
只更改存取時間
-m–time=mtime只更改變動時間
-d TIME-date=字符串設(shè)定時間與日期,可以使用各種不同的格式
-t STAMP
設(shè)定時間戳。STAMP是十進制數(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ù)(幾點),mm為分鐘數(shù)SS為秒數(shù).此處秒的設(shè)定范圍是0–61,這樣可以處理閏秒。
這些數(shù)字組成的時間是環(huán)境變量TZ指定的時區(qū)中的一個時間。由于系統(tǒng)的限制,早于1970年1月1日的時間是錯誤的
-r FILE
把指定文檔或目錄的日期時間,統(tǒng)統(tǒng)設(shè)成和參考文檔或目錄的日期時間相同
-c–no-create不建立任何文檔

Linux的多個time屬性
access time是文檔最后一次被讀取的時間。因此閱讀一個文檔會更新它的access時間,但它的modify時間和change時間并沒有變化。cat、more 、less、grep、sed、tail、head這些命令都會修改文件的access時間。 
change time是文檔的索引節(jié)點(inode)發(fā)生了改變(比如位置、用戶屬性、組屬性等)。chmod, chown,create,mv等動作會將Linux文件的change time修改為系統(tǒng)當(dāng)前時間。
modify time是文本本身的內(nèi)容發(fā)生了變化。[文檔的modify時間也叫時間戳(timestamp).] ls命令看到的是modify time。用vi等工具編輯一個文件保存后,modify time會被修改。

創(chuàng)建新文件 
[root@GChan ~]# ls -l new.txt 
ls: new.txt: 沒有那個文件或目錄 
[root@GChan ~]# touch new.txt 
[root@GChan ~]# ls -l new.txt 
-rw-r--r-- 1 root root 0 10-11 22:40 new.txt 
[root@GChan ~]# 
 
 
更改文件時間為當(dāng)前時間 
[root@GChan ~]# ls -l new.txt 
-rw-r--r-- 1 root root 0 10-11 22:40 new.txt 
[root@GChan ~]# touch new.txt 
[root@GChan ~]# ls -l new.txt 
-rw-r--r-- 1 root root 0 10-11 22:41 new.txt 
 
 
更改文件時間為指定時間 
[root@GChan ~]# date 
2010年 10月 11日 星期一 22:42:54 CST 
[root@GChan ~]# touch -t 10112200 new.txt   <=== 格式 MMDDhhmm 
[root@GChan ~]# ls -l new.txt 
-rw-r--r-- 1 root root 0 10-11 22:00 new.txt 
[root@GChan ~]# touch -t 200910112200 new.txt   <=== 格式 yyyyMMDDhhmm 
[root@GChan ~]# ls -l new.txt 
-rw-r--r-- 1 root root 0 2009-10-11 new.txt 
[root@GChan ~]# 
 
[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   201211142234.50時間戳  
[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 
 
將 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 
 
 
將文件時間改成與別的文件相同 
[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


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI