Chinaunix首页 | 论坛 | 博客
  • 博客访问: 330412
  • 博文数量: 127
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 333
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-27 14:44
个人简介

兴趣是最好的学习契机!

文章分类

全部博文(127)

文章存档

2017年(1)

2016年(3)

2015年(54)

2014年(58)

2013年(11)

我的朋友

分类: LINUX

2015-12-31 13:12:46

原文地址:atime、mtime、ctime 作者:bingqihan

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             | -           |
   +-------------------------------------------------+
阅读(684) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~