Linux精講——atime、mtime、ctime

flyingfishzxf發表於2016-02-27

概念

通過 stat filename 命令檢視一個檔案的三個時間屬性

[root@centos01 ~]# stat 1.txt
  File: `1.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 931337      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-02-27 22:20:10.803907698 +0800
Modify: 2016-02-27 22:20:10.803907698 +0800
Change: 2016-02-27 22:20:10.803907698 +0800

Access 後面的時間即為atime,檔案被讀會更改此屬性
Modify 後面的時間即為mtime,檔案內容改變會更改此屬性
Change 後面的時間即為ctime,inode資訊改變會更改此屬性

如何更改atime、mtime、ctime

如上,1.txt 是新建的一個檔案,初始狀態其三個時間都一樣的

  • cat 1.txt 後,atime 改變,mtime 和 ctime 不變
[root@centos01 ~]# cat 1.txt
[root@centos01 ~]# stat 1.txt
  File: `1.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 931337      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-02-27 22:24:24.653927639 +0800
Modify: 2016-02-27 22:20:10.803907698 +0800
Change: 2016-02-27 22:20:10.803907698 +0800
  • echo "123" >> 1.txt 後,mtime 改變,ctime 改變,atime 不變
[root@centos01 ~]# echo "123" >> 1.txt
[root@centos01 ~]# stat 1.txt
  File: `1.txt'
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 931337      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-02-27 22:24:24.653927639 +0800
Modify: 2016-02-27 22:29:36.153926096 +0800
Change: 2016-02-27 22:29:36.153926096 +0800
  • mv 1.txt 2.txt / chmod 666 1.txt 等會變更inode資訊的操作會使ctime改變,atime 和 mtime不變
[root@centos01 ~]# chmod 666 1.txt
[root@centos01 ~]# stat 1.txt
  File: `1.txt'
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 931337      Links: 1
Access: (0666/-rw-rw-rw-)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-02-27 22:24:24.653927639 +0800
Modify: 2016-02-27 22:29:36.153926096 +0800
Change: 2016-02-27 22:30:44.079913921 +0800
  • mtime 改變一定會導致 ctime 改變

相關文章