mtime,ctime,atime 说明

本文详细解析了Linux系统文件的三个主要时间属性:ctime、mtime和atime,以及relatime特性的引入原因和使用方法。通过实际例子展示了不同命令对文件时间属性的影响,并提供了查看文件时间属性的常用命令。此外,文章还讨论了relatime如何解决atime的性能问题,以及其在文件系统挂载时的应用。
    Linux系统文件有三个主要的时间属性,分别是ctime(change time, 而不是create time), atime(access time), mtime(modify time).后来为了解决atime的性能问题,还引入了一个relatime的属性,下面一一解释.


    ctime, 很多朋友把它理解成create time,包括很多误导人的书籍也是这么写.实际上ctime是指change time.


    mtime, 就是modify time.


    mtime和ctime的区别在于,只有修改了文件的内容,才会更新文件的mtime,而对文件更名,修改文件的属主等操作,只会更新ctime.


    举例说明: 对文件进行mv操作,mtime不变,ctime更新;编辑文件内容,mtime和ctime同时修改.其他操作的影响,可以自己试验.但是我发现对文件执行touch操作,会同时修改mtime和ctime,所以具体修改哪个时间,还取决于不同命令自己的实现;atime, 这个就是每次查看文件内容的时候会更新.比如cat操作,而ls操作是不会更新的.


    你可以通过stat来查看文件的mtime,ctime,atime等属性,也可以通过ls命令来查看,具体如下:


    ls -lc filename 列出文件的 ctime


    ls -lu filename 列出文件的 atime


    ls -l filename 列出文件的 mtime


    从kernel2.6.29开始,还默认集成了一个relatime的属性.这个功能我个人理解是为了解决atime的兼容性问题.因为在文件读操作很频繁的系统中,atime更新所带来的开销很大,所以很多SA都在挂装文件系统的时候使用noatime属性来停止更新atime.但是有些程序需要根据atime进行一些判断和操作,所以Linux就推出了一个relatime特性.


    使用这个特性来挂装文件系统后,只有当mtime比atime更新的时候,才会更新atime.事实上,这个时候atime和mtime已经是同一个东西了.所以这个选项就是为了实现对atime的兼容才推出的.并不是一个新的时间属性.使用方法就是通过mount -o relatime /dir来挂装目录.


    在网上找到了一个表,列出了常见的系统调用对这几个时间的影响,包括对父目录的影响,很清晰.



On Thu, Oct 05, 2006 at 12:33:57PM -0700, Dmitri V. Ivanov wrote:


> *********************************************************************
> Due to deletion of content types excluded from this list by policy,
> this multipart message was reduced to a single part, and from there
> to a plain text message.
> *********************************************************************


It seems I've failed to send html version...


There is a my opinion about 'Why' and 'How' and next to it my script.
It seems to me too slow. It sorts all files with directory whereas to
sort directories is sufficient. But to sort all files seems to be
usefull to get list of deleted files like uniq.


Why and How:
=========


   First of all about file timestamps and their update [1]in common and table
   with details:


   +-------------------------------------------------+
   |               |  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             | -           |
   +-------------------------------------------------+


来源: http://blog.chinaunix.net/uid-24500107-id-2602881.html

### Linux `find` 命令中的 `atime`、`ctime` `mtime` 的用法及区别 #### 1. 时间戳的概念 在 Linux 中,文件有三种主要的时间戳: - **Access Time (atime)**:表示最后一次访问该文件内容的时间。 - **Modify Time (mtime)**:表示最后一次修改文件内容的时间。 - **Change Time (ctime)**:表示最后一次更改文件元数据(如权限、所有权等)的时间。 这些时间戳可以通过 `stat` 命令查看。例如: ```bash stat filename ``` --- #### 2. `find` 命令中 `atime` 的使用 `atime` 参数用于查找基于最后访问时间的文件。常见的选项包括: - `-atime n`:查找在过去 `n` 天内被访问过的文件[^3]。 - `-atime +n`:查找在 `n` 天前被访问过,但今天没有被访问过的文件。 - `-atime -n`:查找在过去不到 `n` 天内被访问过的文件,包括今天的文件。 **示例**: - 查找过去 7 天内被访问过的文件: ```bash find /path/to/directory -atime -7 ``` --- #### 3. `find` 命令中 `mtime` 的使用 `mtime` 参数用于查找基于最后修改时间的文件。常见选项与 `atime` 类似: - `-mtime n`:查找在过去 `n` 天内被修改过的文件。 - `-mtime +n`:查找在 `n` 天前被修改过,但今天没有被修改过的文件。 - `-mtime -n`:查找在过去小于 `n` 天内被修改过的文件。 **示例**: - 查找过去 30 天内被修改过的文件: ```bash find /path/to/directory -mtime -30 ``` --- #### 4. `find` 命令中 `ctime` 的使用 `ctime` 参数用于查找基于最后变更时间的文件。这里的“变更”指的是文件元数据的变化,而非内容变化。同样支持类似的选项: - `-ctime n`:查找在过去 `n` 天内发生元数据变更的文件。 - `-ctime +n`:查找在 `n` 天前发生元数据变更的文件。 - `-ctime -n`:查找在过去小于 `n` 天内发生元数据变更的文件。 **示例**: - 查找过去 5 天内元数据发生变化的文件: ```bash find /path/to/directory -ctime -5 ``` --- #### 5. 区别总结 | 属性 | 描述 | |------------|----------------------------------------------------------------------------------------| | **Atime** | 记录的是文件内容上次被读取或访问的时间 | | **Mtime** | 记录的是文件内容上次被修改的时间 | | **Ctime** | 记录的是文件元数据(如权限、所有者等)或内容本身上次被改变的时间 | 需要注意的是,在现代文件系统中,频繁更新 `atime` 可能会影响性能,因此许多发行版默认禁用了自动更新 `atime` 功能[^3]。 --- ### 示例代码汇总 1. **查找过去 10 天内被访问过的文件** ```bash find /path/to/directory -atime -10 ``` 2. **查找过去 3 天内被修改过的文件** ```bash find /path/to/directory -mtime -3 ``` 3. **查找过去 1 天内元数据发生变化的文件** ```bash find /path/to/directory -ctime -1 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值