一 、查看文件的時間及相關命令
1、stat 查看文件時間
[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 * 查看這個目錄所有文件的狀態。
而我們想要查看某文件的三個時間中的具體某個時間,並以年月日時分秒的格式保存。我們可以使用下面的命令:
[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 查看時也有三個時間:
• modification time(mtime,修改時間):當該文件的“內容數據”更改時,就會更新這個時間。內容數據指的是文件的內容,而不是文件的屬性。 • status time(ctime,狀態時間):當該文件的”狀態(status)”改變時,就會更新這個時間,舉例來說,更改了權限與屬性,就會更新這個時間。 • access time(atime,存取時間):當“取用文件內容”時,就會更新這個讀取時間。舉例來說,使用cat去讀取 ~/.bashrc,就會更新atime了。
[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)都會更新為當前時間。若該文件不存在,則會主動建立一個新的空文件。
[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,查看到的結果也是一樣:
[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. 同時變更文件的修改時間和訪問時間
[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. 只變更文件的修改時間
[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. 只變更文件的訪問時間
[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
將一個文件的時間修改為同另一個文件的時間相同
[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 命令一樣的參數修改文件時間:
[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命令+文件名。
