Linux下檢視和修改檔案時間
Linux下檢視和修改檔案時間
一 、檢視檔案的時間及相關命令
1、stat 檢視檔案時間
1 2 3 4 5 6 7 8 |
[root@linux-node1 ~] # stat lnmp-install.log
File: `lnmp- install .log'
Size: 2228418 Blocks: 4368 IO Block: 4096 regular file
Device: fd00h /64768d Inode: 1970147 Links: 1
Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-04-18 16:03:58.903725603 +0800 Modify: 2016-05-15 15:02:15.836183263 +0800
Change: 2016-05-15 15:02:15.836183263 +0800
|
說明:Access:訪問時間 Modify:修改時間 Change:改變時間 ,可以使用stat * 檢視這個目錄所有檔案的狀態。
而我們想要檢視某檔案的三個時間中的具體某個時間,並以年月日時分秒的格式儲存。我們可以使用下面的命令:
1 2 3 4 5 6 |
[root@linux-node1 ~] # stat lnmp-install.log |grep -i Access | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}' |grep -v 'rwr' 20190418160358
[root@linux-node1 ~] # stat lnmp-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}'
20160515150215
[root@linux-node1 ~] # stat lnmp-install.log |grep -i Change | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}' 20160515150215
|
2、ls檢視檔案時間
相應的通過ls 檢視時也有三個時間:
1 2 3 |
• modification time (mtime,修改時間):當該檔案的“內容資料”更改時,就會更新這個時間。內容資料指的是檔案的內容,而不是檔案的屬性。
• status time (ctime,狀態時間):當該檔案的”狀態(status)”改變時,就會更新這個時間,舉例來說,更改了許可權與屬性,就會更新這個時間。
• access time (atime,存取時間):當“取用檔案內容”時,就會更新這個讀取時間。舉例來說,使用 cat 去讀取 ~/.bashrc,就會更新atime了。
|
1 2 3 4 |
[root@linux-node1 ~] # ls -l --time=ctime lnmp-install.log
-rw-r--r--. 1 root root 2228418 May 15 2016 lnmp- install .log
[root@linux-node1 ~] # ls -l --time=atime lnmp-install.log
-rw-r--r--. 1 root root 2228418 Apr 18 16:03 lnmp- install .log
|
注意:ls 引數裡沒有--time=mtime 這個引數,因為我們預設通過ls -l 檢視到的時間就是mtime .
二、修改檔案時間
建立檔案我們可以通過touch命令來建立,同樣的,我們也可以使用touch命令來修改檔案時間。touch 的相關引數如下:
-a : 僅修改access time
-c :僅修改時間,而不建立檔案
-d :後面可以接日期,也可以使用 --date="日期或時間"
-m :僅修改mtime
-t :後面可以接時間,格式為 [YYMMDDhhmm]
注意:如果touch後面接一個已經存在的檔案,則該檔案的3個時間(atime/ctime/mtime)都會更新為當前時間。若該檔案不存在,則會主動建立一個新的空檔案。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@linux-node1 ~] # ll
-rw-r--r--. 1 root root 9214 Apr 24 2016 install .log
-rw-r--r--. 1 root root 3091 Apr 24 2016 install .log.syslog
-rw-r--r--. 1 root root 2228418 May 15 2016 lnmp- install .log
[root@linux-node1 ~] # touch install.log
[root@linux-node1 ~] # stat install.log
File: ` install .log'
Size: 9214 Blocks: 24 IO Block: 4096 regular file
Device: fd00h /64768d Inode: 1962242 Links: 1
Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-04-18 16:20:22.729724898 +0800
Modify: 2019-04-18 16:20:22.729724898 +0800
Change: 2019-04-18 16:20:22.729724898 +0800
|
同樣的,使用ls,檢視到的結果也是一樣:
1 2 3 4 5 6 |
[root@linux-node1 ~] # ls -l --time=ctime install.log
-rw-r--r--. 1 root root 9214 Apr 18 16:20 install .log
[root@linux-node1 ~] # ls -l --time=atime install.log
-rw-r--r--. 1 root root 9214 Apr 18 16:20 install .log
[root@linux-node1 ~] # ls -l install.log
-rw-r--r--. 1 root root 9214 Apr 18 16:20 install .log
|
利用touch修改檔案時間:
1. 同時變更檔案的修改時間和訪問時間
1 2 3 4 5 6 7 8 9 |
[root@linux-node1 ~] # touch -d "2018-04-18 08:00:00" install.log
[root@linux-node1 ~] # stat install.log
File: ` install .log'
Size: 9214 Blocks: 24 IO Block: 4096 regular file
Device: fd00h /64768d Inode: 1962242 Links: 1
Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-04-18 08:00:00.000000000 +0800
Modify: 2018-04-18 08:00:00.000000000 +0800
Change: 2019-04-18 16:32:35.947725358 +0800
|
2. 只變更檔案的修改時間
1 2 3 4 5 6 7 8 9 |
[root@linux-node1 ~] # touch -m -d "2018-05-20 08:00:00" install.log
[root@linux-node1 ~] # stat install.log
File: ` install .log'
Size: 9214 Blocks: 24 IO Block: 4096 regular file
Device: fd00h /64768d Inode: 1962242 Links: 1
Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-04-18 08:00:00.000000000 +0800
Modify: 2018-05-20 08:00:00.000000000 +0800
Change: 2019-04-18 16:36:06.617725402 +0800
|
3. 只變更檔案的訪問時間
1 2 3 4 5 6 7 8 9 |
[root@linux-node1 ~] # touch -a -d "2017-05-10 09:00:00" install.log
[root@linux-node1 ~] # stat install.log
File: ` install .log'
Size: 9214 Blocks: 24 IO Block: 4096 regular file
Device: fd00h /64768d Inode: 1962242 Links: 1
Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-05-10 09:00:00.000000000 +0800
Modify: 2018-05-20 08:00:00.000000000 +0800
Change: 2019-04-18 16:38:09.851725390 +0800
|
將一個檔案的時間修改為同另一個檔案的時間相同
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@linux-node1 ~] # ls -l anaconda-ks.cfg
-rw-------. 1 root root 1097 Apr 24 2016 anaconda-ks.cfg
[root@linux-node1 ~] # touch -acmr anaconda-ks.cfg install.log
[root@linux-node1 ~] # stat install.log
File: ` install .log'
Size: 9214 Blocks: 24 IO Block: 4096 regular file
Device: fd00h /64768d Inode: 1962242 Links: 1
Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-04-24 22:21:38.363999825 +0800
Modify: 2016-04-24 22:21:39.114999827 +0800
Change: 2019-04-18 16:40:09.464725644 +0800
|
## 將install.log檔案的時間修改成和anaconda-ks.cfg ##
另外touch 還支援像date 命令一樣的引數修改檔案時間:
1 2 3 4 5 6 7 8 9 10 |
[root@linux-node1 ~] # stat install.log
File: ` install .log'
Size: 9214 Blocks: 24 IO Block: 4096 regular file
Device: fd00h /64768d Inode: 1962242 Links: 1
Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-04-24 22:21:38.363999825 +0800
Modify: 2016-04-24 22:21:39.114999827 +0800
Change: 2019-04-18 16:40:09.464725644 +0800
[root@linux-node1 ~] # touch -d "3 days ago" install.log;ls -l install.log
-rw-r--r--. 1 root root 9214 Apr 15 16:47 install .log
|
總結一下常用的檔案操作與時間的關係:
1、訪問時間,讀一次這個檔案的內容,這個時間就會更新。比如對這個檔案使用more命令。ls、stat命令都不會修改檔案的訪問時間。
2、修改時間,對檔案內容修改一次,這個時間就會更新。比如:vim後儲存檔案。ls -l列出的時間就是這個時間。
3、狀態改變時間。通過chmod命令更改一次檔案屬性,這個時間就會更新。檢視檔案的詳細的狀態、準確的修改時間等,可以通過stat命令+檔名。
改行去放羊