详解Linux文件的三个时间

Linux文档的时间一般分三种:Access time-atime(访问时间)、Modification time-mtime(内容修改时间)、change-ctime(inode更改时间),下面为大家详细讲解一下Linux中文件的三种时间。

1 这三个time的含义

    1. 简名全名中文名含义
      atimeaccess time访问时间文件中的数据库最后被访问的时间
      mtimemodify time修改时间文件内容被修改的最后时间
      ctimechange time变化时间文件的元数据发生变化。比如权限,所有者等

2 如何查看这3个time

[root@centos7 time]# pwd
/app/time
[root@centos7 time]# ll
total 8
-rw-------. 1 root root 1933 Nov 11 08:14 anaconda-ks.cfg
-rw-r--r--. 1 root root   59 Nov 11 08:15 issue
[root@centos7 time]# stat issue
 File: ‘issue’
 Size: 59            Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d    Inode: 261123      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 08:15:05.650986739 +0800
Modify: 2017-11-11 08:15:05.650986739 +0800
Change: 2017-11-11 08:15:05.650986739 +0800
Birth: -[root@centos7 time]# ls -l                               #默认的ls -l显示的是mtime     total 8-rw-------. 1 root      root 1933 Nov 11 08:14 anaconda-ks.cfg-rw-r--r--. 1 zhaojiedi root   71 Nov 11 09:05 issue[root@centos7 time]# ls -l --time=atime                             #列出文件的atimetotal 8-rw-------. 1 root      root 1933 Nov 11 08:14 anaconda-ks.cfg-rw-r--r--. 1 zhaojiedi root   71 Nov 11 09:12 issue[root@centos7 time]# ls -l --time=ctime                #列出ctimetotal 8-rw-------. 1 root      root 1933 Nov 11 08:14 anaconda-ks.cfg-rw-r--r--. 1 zhaojiedi root   71 Nov 11 09:03 issue

3 三个time的测试

3.1 测试准备工作

测试前,我们需要先关闭文件系统的relatime特性。这个随后在说,具体操作如下。

[root@centos7 time]# mount -o remount,strictatime /app  # 重新挂载我们的/app,并修改文件系统工作在严格atime上,也就是不启用了默认的relatime支持。
[root@centos7 time]# mount |grep /app                   #查看我们的修改
/dev/sda5 on /app type ext4 (rw,seclabel,data=ordered)

3.2 读取文件测试

[root@centos7 time]# stat issue                             #先获取3个时间
 File: ‘issue’
 Size: 59            Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d    Inode: 261123      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 08:15:05.650986739 +0800
Modify: 2017-11-11 08:15:05.650986739 +0800
Change: 2017-11-11 08:15:05.650986739 +0800
Birth: -
[root@centos7 time]# cat issue                             #读取下
\S
Kernel \r on an \m
tty:   \l
hostname:   \n
time:    \t
[root@centos7 time]# stat issue                #再次查看3个时间
 File: ‘issue’
 Size: 59            Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d    Inode: 261123      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 08:57:40.858948780 +0800
Modify: 2017-11-11 08:15:05.650986739 +0800
Change: 2017-11-11 08:15:05.650986739 +0800
Birth: -

通过上面的分析,我们可以看出来,在使用cat读取文件后,文件的atime发生了改变。其他的没有改变。

3.3 修改文件测试

[root@centos7 time]# stat issue                           #先获取下3个time
 File: ‘issue’
 Size: 65            Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d    Inode: 261123      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 09:03:49.080931626 +0800
Modify: 2017-11-11 09:04:16.881930331 +0800
Change: 2017-11-11 09:04:16.881930331 +0800
Birth: -
[root@centos7 time]# echo "hello" >> issue                #修改文件
[root@centos7 time]# stat issue                 #再次查看三个time
 File: ‘issue’
 Size: 71            Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d    Inode: 261123      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 09:03:49.080931626 +0800
Modify: 2017-11-11 09:05:07.775927960 +0800
Change: 2017-11-11 09:05:07.775927960 +0800
Birth: -

通过上面的实验,我们可以看出来,写文件操作不会导致atime(访问时间)的修改,但是mtime和ctime会发生修改。mtime修改了我们可以理解的,毕竟我们修改了文件的,

那为何ctime也修改了呢, 仔细可以发现我们文件的大小发生了变化,也就是元数据发生了变化,所以ctime也是要变化的。

3.4 修改文件所有者测试

[root@centos7 time]# stat issue                                          #先查看下3个time
 File: ‘issue’
 Size: 71            Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d    Inode: 261123      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 09:03:49.080931626 +0800
Modify: 2017-11-11 09:05:07.775927960 +0800
Change: 2017-11-11 09:05:07.775927960 +0800
Birth: -
[root@centos7 time]# chown zhaojiedi issue                              #修改权限
[root@centos7 time]# stat issue                         #再次查看3个时间
 File: ‘issue’
 Size: 71            Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d    Inode: 261123      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/zhaojiedi)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 09:03:49.080931626 +0800
Modify: 2017-11-11 09:05:07.775927960 +0800
Change: 2017-11-11 09:12:42.076906795 +0800
Birth: -

通过上面的实验,我们可以看出来,修改了权限后,文件ctime发生了变化。

4 说说relatime

常用命令对三个time的修改情况我们上面的测试,可以看出来,每次访问文件都会更新atime,这是很耗时的,尤其在web服务器上,大量用户只是访问html页面,完全没有必要修改atime。

从kernel2.6.29开始,文件系统默认集成了一个relatime的属性。

那么啥时候更新atime呢? 有2种情况会更新atime,第一种是mtime比atime新,第二种是上次访问是1天前的了。

5 常用命令对三个time的修改情况

上面我们做了3个测试,我们也对atime,mtime,ctime有了一定的了解。网上有人已经做了好多测试如下表。

+-------------------------------------------------+
  |               |  timestamps marked for update   |
  |    syscall    |---------------------------------|
  |               |       file        | parent dir  |
  |---------------+-------------------+-------------|
  | [2]chdir      |                   |             |
  |---------------| -                 | -           |
  | [3]fchdir     |                   |             |
  |---------------+-------------------+-------------|
  | [4]chmod      |                   |             |
  |---------------| ctime             | -           |
  | [5]fchmod     |                   |             |
  |---------------+-------------------+-------------|
  | [6]chown      |                   |             |
  |---------------|                   |             |
  | [7]fchown     | ctime             | -           |
  |---------------|                   |             |
  | [8]lchown     |                   |             |
  |---------------+-------------------+-------------|
  | [9]close      | -                 | -           |
  |---------------+-------------------+-------------|
  | [10]creat     | atime,ctime,mtime | ctime,mtime |
  |---------------+-------------------+-------------|
  | [11]execve    | atime             | -           |
  |---------------+-------------------+-------------|
  | [12]fcntl     | -                 | -           |
  |---------------+-------------------+-------------|
  | [13]ftruncate |                   |             |
  |---------------| ctime,mtime       | -           |
  | [14]truncate  |                   |             |
  |---------------+-------------------+-------------|
  | [15]fstat     |                   |             |
  |---------------|                   |             |
  | [16]stat      | -                 | -           |
  |---------------|                   |             |
  | [17]lstat     |                   |             |
  |---------------+-------------------+-------------|
  | [18]fsync     |                   |             |
  |---------------| -                 | -           |
  | [19]fdatasync |                   |             |
  |---------------+-------------------+-------------|
  | [20]link      | ctime             | ctime,mtime |
  |---------------+-------------------+-------------|
  | [21]lseek     | -                 | -           |
  |---------------+-------------------+-------------|
  | [22]mknod     | atime,ctime,mtime | ctime,mtime |
  |---------------+-------------------+-------------|
  | [23]mkdir     | atime,ctime,mtime | ctime,mtime |
  |---------------+-------------------+-------------|
  | [24]mmap      | *                 | -           |
  |---------------+-------------------+-------------|
  | [25]munmap    | -                 | -           |
  |---------------+-------------------+-------------|
  | [26]msync     | *                 | -           |
  |---------------+-------------------+-------------|
  | [27]open      | *                 | *           |
  |---------------+-------------------+-------------|
  | [28]pread     |                   |             |
  |---------------|                   |             |
  | [29]read      | atime             | -           |
  |---------------|                   |             |
  | [30]readv     |                   |             |
  |---------------+-------------------+-------------|
  | [31]pwrite    |                   |             |
  |---------------|                   |             |
  | [32]write     | ctime,mtime       | -           |
  |---------------|                   |             |
  | [33]writev    |                   |             |
  |---------------+-------------------+-------------|
  | [34]rename    | implementation    | ctime,mtime |
  |---------------+-------------------+-------------|
  | [35]rmdir     | -                 | ctime,mtime |
  |---------------+-------------------+-------------|
  | [36]readlink  | *                 | -           |
  |---------------+-------------------+-------------|
  | [37]readdir   | atime             | -           |
  |---------------+-------------------+-------------|
  | readahead     | ?                 | ?           |
  |---------------+-------------------+-------------|
  | [38]symlink   | *                 | *           |
  |---------------+-------------------+-------------|
  | sendfile      | ?                 | ?           |
  |---------------+-------------------+-------------|
  | [39]unlink    | -                 | ctime,mtime |
  |---------------+-------------------+-------------|
  | [40]utime     | ctime             | -           |
  +-------------------------------------------------+

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/209450.html<

(0)
管理的头像管理
上一篇2025-04-09 11:39
下一篇 2025-04-09 11:40

相关推荐

  • 云服务器和云虚拟主机怎么选?云服务器和虚拟主机区别

    云服务器适合业务增长快、需弹性扩展的场景,而云虚拟主机适合预算有限、技术门槛低的小型静态网站或测试环境,二者核心区别在于资源独享性与运维复杂度,核心差异解析:从底层架构到使用体验很多人容易混淆这两者,觉得它们都是“买空间建站”,它们的底层逻辑完全不同,云服务器(ECS)就像是你租了一整栋别墅,水电网络独立,你想……

    2026-06-29
    0
  • 赣州智慧旅游招聘是真的吗?赣州旅游人才招聘信息

    中级岗位(3-5年经验)月薪范围通常在6000-10000元,这类岗位需要独立负责项目模块,如独立运营一个抖音账号,或维护一个景区小程序的功能迭代,具备成功案例的候选人议价能力较强,高级岗位(5年以上经验)月薪范围通常在10000-20000元,部分核心管理岗可达更高,这类人才需要具备战略规划能力,如制定整个景……

    2026-06-29
    0
  • 赣州智能物联网车位锁如何管理?智能车位锁管理系统多少钱

    赣州智能物联网车位锁管理的核心在于通过云端平台实现远程控锁、状态实时监控及自动计费,彻底解决传统车位“被占难管”与“找位难”的痛点,在赣州这样的城市,随着机动车保有量的持续增长,老旧小区、商业综合体以及私人固定车位的资源矛盾日益凸显,传统的机械地锁或简易遥控锁,不仅操作繁琐,更无法实现数据化管理,引入智能物联网……

    2026-06-29
    0
  • 赣州智能消防栓好用吗,智能消防栓多少钱一个

    赣州智能消防栓通过物联网技术实现实时监测与远程报警,能显著降低火灾响应时间并提升城市消防安全管理水平,是目前智慧城市建设中不可或缺的基础设施,赣州智能消防栓的核心价值与应用场景传统消防栓往往存在“看不见、摸不着、用不了”的痛点,在赣州这样地形复杂、老城区与新城区并存的区域,传统设施的管理难度极大,智能消防栓的出……

    2026-06-29
    0
  • 云服务器和物理机到底有啥区别?

    云服务器本质上是虚拟化资源池中的弹性实例,而传统物理服务器是独占的硬件实体,前者胜在弹性与运维便捷,后者强在物理隔离与性能稳定,具体选择取决于业务对成本、扩展性及安全合规的权衡,很多人初次接触服务器时,容易把“云服务器”和“传统物理服务器”混为一谈,觉得它们都是用来跑网站或存数据的盒子,这两者的底层逻辑完全不同……

    2026-06-29
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注