[Linux] 文件的atime、mtime和ctime

1.使用stat命令查看文件的三個時間
新創建的文件atime,mtime,ctime是一樣的,也就是創建時間,但是之後再想查創建時間沒有找到方法。

[root@xxx testtime]# ls -l
total 12
-rw-r--r--. 1 root root 4 Jan 16 11:22 aaa.txt
-rw-r--r--. 1 root root 4 Jan 16 11:23 bbb.txt
-rw-r--r--. 1 root root 4 Jan 16 11:23 ccc.txt
[root@xxx testtime]# stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796504   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:22:55.902450877 +0800
Modify: 2020-01-16 11:22:55.902450877 +0800
Change: 2020-01-16 11:22:55.902450877 +0800
 Birth: -
[root@xxx testtime]#

三種時間的介紹
atime(Access Time) -文件的最近訪問時間(cat、vim等)
mtime(Modify Time) -文件的內容最近修改的時間(vim編輯,重定向寫入等)
ctime(Change Time) -文件屬性最近修改的時間(內容、路徑、所有者、權限等變化)

(1)使用cat命令查看文件,僅atime會更新

[root@xxx testtime]# cat aaa.txt
aaa
[root@xxx testtime]# stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796504   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:26:10.860493954 +0800
Modify: 2020-01-16 11:22:55.902450877 +0800
Change: 2020-01-16 11:22:55.902450877 +0800
 Birth: -
[root@xxx testtime]#

(2)使用vim命令查看文件但不修改(:q),僅atime會更新;修改內容(:wq),三個時間都會更新

[root@xxx testtime]# vim aaa.txt
[root@xxx testtime]# stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796507   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:27:29.221913212 +0800
Modify: 2020-01-16 11:27:29.221913212 +0800
Change: 2020-01-16 11:27:29.291913584 +0800
 Birth: -
[root@xxx testtime]#

(3)使用重定向追加內容到文件,會更新mtime和ctime,由於並沒有讀原文件內容atime不更新

[root@xxx testtime]# stat bbb.txt
  File: ‘bbb.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:23:06.606508142 +0800
Modify: 2020-01-16 11:23:06.606508142 +0800
Change: 2020-01-16 11:23:06.606508142 +0800
 Birth: -
[root@xxx testtime]# cat aaa.txt >> bbb.txt
[root@xxx testtime]# stat bbb.txt
  File: ‘bbb.txt’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:23:06.606508142 +0800
Modify: 2020-01-16 11:28:23.973206143 +0800
Change: 2020-01-16 11:28:23.973206143 +0800
 Birth: -
[root@xxx testtime]#

(4)改變文件路徑,僅ctime會更新

[root@xxx testtime]# stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796507   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:28:23.973206143 +0800
Modify: 2020-01-16 11:27:29.221913212 +0800
Change: 2020-01-16 11:27:29.291913584 +0800
 Birth: -
[root@xxx testtime]#
[root@xxx testtime]# mv aaa.txt ../;cd ..;stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796507   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:28:23.973206143 +0800
Modify: 2020-01-16 11:27:29.221913212 +0800
Change: 2020-01-16 11:56:17.259158698 +0800
 Birth: -
[root@xxx test]#

2.使用touch指令修改時間
(1) touch -a更新atime(ctime也會更新)

[root@xxx testtime]# date;touch -a bbb.txt
Thu Jan 16 11:29:11 CST 2020
[root@xxx testtime]# stat bbb.txt
  File: ‘bbb.txt’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:29:11.802462045 +0800
Modify: 2020-01-16 11:28:23.973206143 +0800
Change: 2020-01-16 11:29:11.802462045 +0800
 Birth: -
[root@xxx testtime]#

使用touch不帶選項會更新三個時間

[root@xxx testtime]# date;touch bbb.txt;stat bbb.txt
Thu Jan 16 11:32:22 CST 2020
  File: ‘bbb.txt’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:32:22.494482300 +0800
Modify: 2020-01-16 11:32:22.494482300 +0800
Change: 2020-01-16 11:32:22.494482300 +0800
 Birth: -
[root@xxx testtime]#

(2) touch -d 使用指定的日期來修改

[root@xxx testtime]# touch -d "06:00:00" bbb.txt;stat bbb.txt
  File: ‘bbb.txt’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 06:00:00.000000000 +0800
Modify: 2020-01-16 06:00:00.000000000 +0800
Change: 2020-01-16 11:39:25.439745178 +0800
 Birth: -
[root@xxx testtime]#

(3)用某文件的時間來設置另一個文件的時間,會複製atime和mtime,ctime根據當前時間更新
例如用bbb.txt的時間來設置ccc.txt的時間

[root@xxx testtime]# date;touch -r bbb.txt ccc.txt;stat ccc.txt
Thu Jan 16 11:41:06 CST 2020
  File: ‘ccc.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796506   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 06:00:00.000000000 +0800
Modify: 2020-01-16 06:00:00.000000000 +0800
Change: 2020-01-16 11:41:06.349285070 +0800
 Birth: -
[root@xxx testtime]#

參考資料:
查看文件的創建、修改時間 https://www.cnblogs.com/yizhipanghu/p/9634325.html
Linux系統stat指令用法 https://www.cnblogs.com/klb561/p/9241228.html

發佈了92 篇原創文章 · 獲贊 2 · 訪問量 5699
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章