面试题:Linux 中一个文件的 MAC 代表什么意思

查看文件状态 stat

ls 命令能够查看文件的类型、时间、属主、属组,大小以及最近的修改时间等信息,但是还有一些文件的扩展属性,是使用 ls 命令无法查看到的

stat 命令则用于显示文件的详细属性

面试题:linux操作系统中,一个文件的MAC time代表什么意思,如何查看一个文件的MAC time?

  • access time:表示我们最后一次访问(仅仅是访问,没有改动)文件的时间

  • modify time:表示我们最后一次修改文件的时间

  • change time:表示我们最后一次对文件属性改变的时间,包括权限,大小,属性等等。

Linux文档的时间一般分三种:

  • Access time(访问时间),atime 可以通过 ls -lu 命令显示,表示文档最后被访问的时间。
[root@centos /tmp]#ll a.log                    # 原文件是 24 号的
-rw-r--r--. 1 root root 0 Mar 24 18:59 a.log
[root@centos /tmp]#cat a.log # cat 之后 atime 会改变
[root@centos /tmp]#stat a.log # 通过 stat 命令查看
File: a.log
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 1866 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2021-03-26 10:13:04.011131156 +0800 # 查看之后会改变为当前时间 26 号,一天之内的访问不会多次改变
Modify: 2021-03-24 18:59:46.273586607 +0800 # 没有改变,还是 24 号
Change: 2021-03-24 18:59:46.273586607 +0800 # 没有改变,还是 24 号
Birth: -
[root@centos /tmp]#
  • Modification time(内容修改时间), mtime 可以通过 ls -l 命令显示,表示文档内容最后被修改的时间。
[root@centos /tmp]#ll d.log                 # 原文件是 24 号的
-rw-r--r--. 1 root root 0 Mar 24 18:59 d.log
[root@centos /tmp]#stat d.log
File: d.log
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 293401 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2021-03-24 18:59:46.273586607 +0800
Modify: 2021-03-24 18:59:46.273586607 +0800
Change: 2021-03-24 18:59:46.273586607 +0800
Birth: -
[root@centos /tmp]#vim d.log # 编辑文件
[root@centos /tmp]#stat d.log
File: d.log
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 293401 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2021-03-26 10:29:31.829155649 +0800 # 改变为当前时间 26 号
Modify: 2021-03-26 10:29:36.934155776 +0800 # 改变为当前时间 26 号
Change: 2021-03-26 10:29:36.936155776 +0800 # 改变为当前时间 26 号
Birth: -
[root@centos /tmp]#
  • change time(改变时间),ctime 可以通过 ls -lc 命令显示,表示文档属性最后被修改的时间
[root@centos /tmp]#ll c.log                   # 原文件是 24 号的
-rw-r--r--. 1 root root 0 Mar 24 18:59 c.log
[root@centos /tmp]#stat c.log
File: c.log
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 293397 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2021-03-24 18:59:46.273586607 +0800
Modify: 2021-03-24 18:59:46.273586607 +0800
Change: 2021-03-24 18:59:46.273586607 +0800
Birth: -
[root@centos /tmp]#chown song.root c.log # 改变文件的属主
[root@centos /tmp]#ll c.log # 确认属主已改为 song 所有
-rw-r--r--. 1 song root 0 Mar 24 18:59 c.log
[root@centos /tmp]#stat c.log
File: c.log
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 293397 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ song) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2021-03-24 18:59:46.273586607 +0800 # 没有改变,还是 24 号
Modify: 2021-03-24 18:59:46.273586607 +0800 # 没有改变,还是 24 号
Change: 2021-03-26 10:23:22.390146489 +0800 # 改变为当前时间 26 号了
Birth: -
[root@centos /tmp]#

总结

  • 当我们仅仅只是读取文件时,access time 改变,而modify,change time 不会改变

  • 当修改文件时,access,modify,change time 都会跟着改变

  • 当修改文件属性时,change time 改变,而access,modify time 不变。

上一篇:codevs 1171 潜伏者


下一篇:Redis 集群版