您的当前位置:首页正文

关于文件的三个时间

2024-11-11 来源:个人技术集锦

1. 场景引入—stat命令

stat命令可以获取文件的详细信息

[root@localhost ~]# stat anaconda-ks.cfg 
  File: ‘anaconda-ks.cfg’
  Size: 1662      	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 68279761    Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-06-10 02:45:24.959342867 +0800
Modify: 2019-06-10 02:43:18.884898228 +0800
Change: 2019-06-10 02:43:18.884898228 +0800
 Birth: -

从这里可以看到三个时间
Access : 文件最近一次被访问的时间 
Modify: 文件内容最近一次被修改的时间 
Change: 文件属性最近一次被改变的时间(属主、属组、权限、大小等)

2. 时间改变的情况分类

1) 使用cat或者more 只有access会变

[root@localhost test]# stat a.txt 
  File: ‘a.txt’
  Size: 2         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:12:20.149589732 +0800
Modify: 2019-07-01 21:12:20.149589732 +0800
Change: 2019-07-01 21:12:20.149589732 +0800
 Birth: -
[root@localhost test]# cat a.txt 
a
[root@localhost test]# stat a.txt 
  File: ‘a.txt’
  Size: 2         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:14:21.096339337 +0800
Modify: 2019-07-01 21:12:20.149589732 +0800
Change: 2019-07-01 21:12:20.149589732 +0800
 Birth: -

2)使用echo增加文件内容,modify和change会变

[root@localhost test]# stat a.txt 
  File: ‘a.txt’
  Size: 2         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:14:21.096339337 +0800
Modify: 2019-07-01 21:12:20.149589732 +0800
Change: 2019-07-01 21:12:20.149589732 +0800
 Birth: -
[root@localhost test]# echo 2 >> a.txt 
[root@localhost test]# stat a.txt 
  File: ‘a.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:14:21.096339337 +0800
Modify: 2019-07-01 21:15:29.012607988 +0800
Change: 2019-07-01 21:15:29.012607988 +0800
 Birth: -

3)mv chmod等操作只会改变文件的change

[root@localhost test]# stat a.txt 
  File: ‘a.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:14:21.096339337 +0800
Modify: 2019-07-01 21:15:29.012607988 +0800
Change: 2019-07-01 21:15:29.012607988 +0800
 Birth: -
[root@localhost test]# mv a.txt b.txt
[root@localhost test]# stat b.txt 
  File: ‘b.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:14:21.096339337 +0800
Modify: 2019-07-01 21:15:29.012607988 +0800
Change: 2019-07-01 21:16:40.556443329 +0800
 Birth: -

4)vim对三个时间的影响

只是看看,不做修改,并且通过q退出,只有access变化
[root@localhost test]# stat b.txt 
  File: ‘b.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:14:21.096339337 +0800
Modify: 2019-07-01 21:15:29.012607988 +0800
Change: 2019-07-01 21:16:40.556443329 +0800
 Birth: -
[root@localhost test]# vim b.txt 
[root@localhost test]# stat b.txt 
  File: ‘b.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:18:20.695226659 +0800
Modify: 2019-07-01 21:15:29.012607988 +0800
Change: 2019-07-01 21:16:40.556443329 +0800
 Birth: -

如果wq保存退出,不管有没有修改文件内容,三个时间都变
[root@localhost test]# stat b.txt 
  File: ‘b.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720750   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:18:20.695226659 +0800
Modify: 2019-07-01 21:15:29.012607988 +0800
Change: 2019-07-01 21:16:40.556443329 +0800
 Birth: -
[root@localhost test]# vim b.txt 
[root@localhost test]# stat b.txt 
  File: ‘b.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720752   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:18:52.200187770 +0800
Modify: 2019-07-01 21:18:52.200187770 +0800
Change: 2019-07-01 21:18:52.201187831 +0800
 Birth: -

5)touch命令直接修改文件的时间属性

  • touch -a b.txt 将access,change更改为系统时间
  • touch -m b.txt 将modify,change更改为系统时间
  • touch b.txt 将同时刷新access,modify,change
touch -a 操作示范

[root@localhost test]# touch -a b.txt 
[root@localhost test]# stat b.txt 
  File: ‘b.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720752   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:21:53.060012162 +0800
Modify: 2019-07-01 21:18:52.200187770 +0800
Change: 2019-07-01 21:21:53.060012162 +0800
 Birth: -

touch -m 操作示范

[root@localhost test]# touch -m b.txt 
[root@localhost test]# stat b.txt 
  File: ‘b.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720752   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:21:53.060012162 +0800
Modify: 2019-07-01 21:23:51.953952473 +0800
Change: 2019-07-01 21:23:51.953952473 +0800
 Birth: -

touch 操作示范
[root@localhost test]# touch b.txt 
[root@localhost test]# stat b.txt 
  File: ‘b.txt’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 203720752   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-01 21:24:19.196538615 +0800
Modify: 2019-07-01 21:24:19.196538615 +0800
Change: 2019-07-01 21:24:19.196538615 +0800
 Birth: -

Top