linux 文件時間查看及修改

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

2, 查看文件時間, 使用ls

相應的通過ls 查看時也有三個時間:
* 指的是文件的內容,而不是文件的屬性。
* status time(ctime,狀態時間):當該文件的”狀態(status)”改變時,就會更新這個時間,舉例來說,更改了權限與屬性,就會更新這個時間。
* access time(atime,存取時間):當“取用文件內容”時,就會更新這個讀取時間。舉例來說,使用cat去讀取 ~/.bashrc,就會更新atime了。
* 注意: modify-文件的屬性被修改; change-文件的屬性(權限,擁護者,時間..)被修改

[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 。

3, 文件時間修改, 基本

  • linux
    touch -acmd “2017-11-20 09:20:23” abc.java
    touch -acmt “20171120092023” abc.java
  • Aix
    aix下,沒有-d參數
    touch -acmt “20171120092023” abc.java

4, 文件時間修改, 具體

創建文件我們可以通過touch來創建。同樣,我們也可以使用touch來修改文件時間。touch的相關參數如下:
-a : 僅修改access time。
-c : 僅修改時間,而不建立文件。
-d : 後面可以接日期,也可以使用 –date=”日期或時間”
-m : 僅修改mtime。
-t : 後面可以接時間,格式爲 [YYMMDDhhmm]
注:如果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

5, 總結三者關係

三者關係文:件的訪問時間,修改時間,及狀態變化時間:

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

修改時間,對文件內容修改一次,這個時間就會更新。比如:vim後保存文件。ls -l列出的時間就是這個時間。

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章