linux中查看和修改檔案時間的命令

來源:互聯網
上載者:User

linux下檔案時間主要有下面三種:

1.1 modification time(mtime)

檔案修改時間,即檔案內容的修改時,更新這個時間,不包括檔案許可權和屬性的修改。使用ls -l查看,預設時間顯示為mtime

$ ls -l uconv.h
-rw-rw-r--  1 work work 1808 Jul 23  2013 uconv.h

1.2 status time(ctime)

檔案狀態status的修改時間,如檔案的許可權和屬性修改時更新這個時間。使用 ls --time=ctime 查看

$ ls -l --time=ctime uconv.h
-rw-rw-r--  1 work work 1808 Jul 23  2013 uconv.h


1、stat查看檔案時間

[root@web10 ~]# stat install.log
  File: “install.log”
  Size: 33386           Blocks: 80         IO Block: 4096   一般檔案
Device: fd00h/64768d    Inode: 7692962     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-07-13 16:02:34.000000000 +0800
Modify: 2011-11-29 16:03:06.000000000 +0800
Change: 2011-11-29 16:03:08.000000000 +0800
說明:Access訪問時間。Modify修改時間。Change狀態改變時間。可以stat *查看這個目錄所有檔案的狀態。

而我們想要查看某檔案的三個時間中的具體某個時間,並以年月日時分秒的格式儲存。我們可以使用下面的命令:

[root@web10 ~]# stat install.log|grep -i Modify | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}'
20111129160306

1.3 access time(atime)

檔案訪問時間,當檔案內容被擷取時,更新這個時間。使用 ls --time=actime 查看

$ ls -l --time=atime uconv.h
-rw-rw-r--  1 work work 1808 Dec 12  2013 uconv.h

相應的通過ls 查看時也有三個時間:

• modification time(mtime,修改時間):當該檔案的“內容資料”更改時,就會更新這個時間。內容資料指的是檔案的內容,而不是檔案的屬性。
• status time(ctime,狀態時間):當該檔案的”狀態(status)”改變時,就會更新這個時間,舉例來說,更改了許可權與屬性,就會更新這個時間。
• access time(atime,存取時間):當“取用檔案內容”時,就會更新這個讀取時間。舉例來說,使用cat去讀取 ~/.bashrc,就會更新atime了。

[root@web10 ~]# ls -l --time=ctime install.log
-rw-r--r-- 1 root root 33386 2011-11-29 install.log
[root@web10 ~]# ls -l --time=atime install.log
-rw-r--r-- 1 root root 33386 07-13 16:02 install.log

注意:ls參數裡沒有--mtime這個參數,因為我們預設通過ls -l查看到的時間就是mtime 。

2. 修改檔案的時間

如果需要修改上述三個時間,使用touch命令來修改。 touch filename ,如果檔案不存在,則建立一個檔案。

$ touch --help

Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

  -a                     change only the access time
                         修改訪問時間
  -c, --no-create        do not create any files
                         修改檔案三個時間,不存在則不建立
  -d, --date=STRING      parse STRING and use it instead of current time
                         指定時間代替目前時間
  -f                     (ignored)
  -m                     change only the modification time
                         修改mtime
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
                         指定修改時間
例如:

$ touch -d "2 days ago" uconv.h
$ ll uconv.h ; ll --time=atime uconv.h ; ll --time=ctime uconv.h ;
-rw-rw-r--  1 work work 1808 Jun 13 18:17 uconv.h
-rw-rw-r--  1 work work 1808 Jun 13 18:17 uconv.h
-rw-rw-r--  1 work work 1808 Jun 15 18:17 uconv.h

將mtime和atime修改為兩天前,ctime沒變。

$ touch -t 201406142020 uconv.h  

$ ll uconv.h ; ll --time=atime uconv.h ; ll --time=ctime uconv.h ;
-rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h
-rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h
-rw-rw-r--  1 work work 1808 Jun 15 18:23 uconv.h
atime和mtime都變了,但是ctime變成了目前時間。

使用cp命令,-a保持原屬性。

$ cp -a uconv.h uconv.h1

$ ll uconv.h1 ; ll --time=atime uconv.h1 ; ll --time=ctime uconv.h1 ;
-rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h1
-rw-rw-r--  1 work work 1808 Jun 15 18:25 uconv.h1
-rw-rw-r--  1 work work 1808 Jun 15 18:27 uconv.h1

mtime和atime都保持原檔案不變,但是ctime變成目前時間

注:如果touch後面接一個已經存在的檔案,則該檔案的3個時間(atime/ctime/mtime)都會更新為目前時間。若該檔案不存在,則會主動建立一個新的空檔案。

[root@web10 ~]# touch install.log
[root@web10 ~]# stat install.log
  File: “install.log”
  Size: 33386           Blocks: 80         IO Block: 4096   一般檔案
Device: fd00h/64768d    Inode: 7692962     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-07-13 16:21:50.000000000 +0800
Modify: 2012-07-13 16:21:50.000000000 +0800
Change: 2012-07-13 16:21:50.000000000 +0800

同樣,使用ls ,查看到的結果也一樣。

[root@web10 ~]# ls -l --time=ctime install.log
-rw-r--r-- 1 root root 33386 07-13 16:21 install.log
[root@web10 ~]# ls -l --time=atime install.log
-rw-r--r-- 1 root root 33386 07-13 16:21 install.log
[root@web10 ~]# ls -l install.log
-rw-r--r-- 1 root root 33386 07-13 16:21 install.log

下面再看一個和touch不相關的例子:

[root@web10 ~]# cp /etc/profile .;ll --time=atime profile ;ll --time=ctime profile
cp:是否覆蓋“./profile”? y
-rw-r--r-- 1 root root 1344 07-13 16:24 profile
-rw-r--r-- 1 root root 1344 07-13 16:25 profile
因為我之前運行過這個命令一次,所以會出現覆蓋,不過這個覆蓋出的好,剛才讓我們看到了atime和ctime的時間的差別。

我們再回到touch利用touch修改檔案時間:

1. 同時修改檔案的修改時間和訪問時間
touch -d "2010-05-31 08:10:30" install.log
2. 只修改檔案的修改時間
touch -m -d "2010-05-31 08:10:30" install.log
3. 只修改檔案的訪問時間
touch -a -d "2010-05-31 08:10:30" install.log

下面再給一個rootkit木馬常用的伎倆。就是把後一個檔案的時間修改成和前一個相同。

touch -acmr /bin/ls /etc/sh.conf
另外touch還支援像date命令一樣參數修改檔案時間:

[root@web10 ~]# touch -d "2 days ago" install.log ; ll install.log
-rw-r--r-- 1 root root 33386 07-11 16:35 install.log
最後總結下常用的檔案操作與時間的關係:

1、訪問時間,讀一次這個檔案的內容,這個時間就會更新。比如對這個檔案使用more命令。ls、stat命令都不會修改檔案的訪問時間。

2、修改時間,對檔案內容修改一次,這個時間就會更新。比如:vim後儲存檔案。ls -l列出的時間就是這個時間。

3、狀態改變時間。通過chmod命令更改一次檔案屬性,這個時間就會更新。查看檔案的詳細的狀態、準確的修改時間等,可以通過stat命令 檔案名稱。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.