[linux系统下载]Linux系统ctime、mtime和atime命令详解

更新时间:2019-06-03    来源:Access    手机版     字体:

【www.bbyears.com--Access】

atime:Access Time 最后一次访问文件(读取或执行)或目录的时间
mtime:Modofy Time 最后一次修改文件(内容)或目录(内容)的时间
ctime:Change Time 最后一次改变文件(属性)或目录(属性)的时间

如何查看文件或目录的atime/mtime/ctime
1、stat filename
2、
atime: ls -lu filename
mtime: ls -l filename
ctime: ls -lc filename
示例演示
在/tmp下新建一个目录tm,tm下新建一个文件a.txt

 代码如下 # cd /tmp
# mkdir tm
# cd tm
# stat ../tm
  File: `../tm"
  Size: 4096       Blocks: 8          IO Block: 4096   directory
Device: ca01h/51713d Inode: 1050207     Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-17 10:57:32.993350411 +0800
Modify: 2014-03-17 10:57:32.993350411 +0800
Change: 2014-03-17 10:57:32.993350411 +0800
 Birth: -
# touch a.txt
# stat a.txt
  File: `a.txt"
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d Inode: 1050208     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-17 10:58:01.328805868 +0800
Modify: 2014-03-17 10:58:01.328805868 +0800
Change: 2014-03-17 10:58:01.328805868 +0800
 Birth: -

新创建的目录和文件的atime/mtime/ctime都是一样的。

打开a.txt,随便输入点内容然后保存。

 代码如下 # vi a.txt
# stat a.txt
  File: `a.txt"
  Size: 3          Blocks: 8          IO Block: 4096   regular file
Device: ca01h/51713d Inode: 1050208     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:02:22.399782635 +0800
Change: 2014-03-17 11:02:22.423782143 +0800
 Birth: -

vi a.txt的时候会修改文件的atime,输入内容保存后会修改mtime,同时因为文件的属性变更了(比如文件大小就变了),所以ctime也会改变。

修改文件的属性,ctime变化,mtime和atime不变。

 代码如下 # chmod g+w a.txt
# stat a.txt
  File: `a.txt"
  Size: 9          Blocks: 8          IO Block: 4096   regular file
Device: ca01h/51713d Inode: 1050208     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:02:22.399782635 +0800
Change: 2014-03-17 11:07:10.114234176 +0800
 Birth: -

修改内容一定会改变atime吗?答案是否!

 代码如下

# echo "world" >> a.txt
# stat a.txt
  File: `a.txt"
  Size: 9          Blocks: 8          IO Block: 4096   regular file
Device: ca01h/51713d Inode: 1050208     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:09:07.980591047 +0800
Change: 2014-03-17 11:09:07.980591047 +0800
 Birth: -


touch指令的作用就是用于改变文件的时间戳,touch命令的语法格式如下:

touch [选项]... filename...

选项与参数:
-a  : 仅修订 atime;
-c  : 仅修改档案的时间,若该档案不存在则不建立新档案;
-d  : 后面可以接欲修订的日期而不用目前的日期,也可以使用 --date="日期或时间"
-m  : 仅修改 mtime ;
-t  : 后面可以接欲修订的时间而不用目前的时间,格式为[YYMMDDhhmm]
-r  : 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同
...
示例演示

 代码如下 # touch a.txt
# stat a.txt
  File: `a.txt"
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d Inode: 1050217     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-18 09:30:49.965240705 +0800
Modify: 2014-03-18 09:30:49.965240705 +0800
Change: 2014-03-18 09:30:49.965240705 +0800
 Birth: -

先通过touch创建a.txt,如果已经存在则会修改文件的atime/mtime/ctime。

 代码如下 # touch a.txt
# stat a.txt
  File: `a.txt"
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d Inode: 1050217     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-18 09:31:43.584234285 +0800
Modify: 2014-03-18 09:31:43.584234285 +0800
Change: 2014-03-18 09:31:43.584234285 +0800
 Birth: -

再touch a.txt,发现atime/mtime/ctime都发生变化了。

 代码如下 # touch -a a.txt
# stat a.txt
  File: `a.txt"
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d Inode: 1050217     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-18 09:32:01.459898755 +0800
Modify: 2014-03-18 09:31:43.584234285 +0800
Change: 2014-03-18 09:32:01.459898755 +0800
 Birth: -

使用touch -a a.txt,只有atime和ctime发生变化了,mtime保存不变。

 代码如下 # touch -m a.txt
# stat a.txt
  File: `a.txt"
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d Inode: 1050217     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-18 09:32:01.459898755 +0800
Modify: 2014-03-18 09:32:22.591502109 +0800
Change: 2014-03-18 09:32:22.591502109 +0800
 Birth: -

使用touch -m a.txt,只有mtime和ctime发生变化了,atime保存不变。

任何情况下,使用touch指令,文件的ctime都会发生改变。以上经过touch指令后,atime/mtime/ctime的值都是当前时间点的值,通过touch指令还可以随意指定时间戳。

 代码如下 # touch -d "2 days ago" b.txt && stat b.txt
  File: `b.txt"
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d Inode: 1050218     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-16 09:38:49.261034132 +0800
Modify: 2014-03-16 09:38:49.261034132 +0800
Change: 2014-03-18 09:38:49.256243241 +0800
 Birth: -

touch -d 通过指定一个表示时间的字符串来当做当前时间,这个格式比较自由,只要是能表示时间的都可以,例如”2014-02-28 16:21:42″、”next Thursday”、”2 days ago”等。

 代码如下 # touch -t 201401011212.30 c.txt && stat c.txt
  File: `c.txt"
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d Inode: 1050219     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-01-01 12:12:30.000000000 +0800
Modify: 2014-01-01 12:12:30.000000000 +0800
Change: 2014-03-18 09:39:45.983178168 +0800
 Birth: -
# touch -t 01011212 d.txt && stat d.txt
  File: `d.txt"
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: ca01h/51713d Inode: 1050220     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-01-01 12:12:00.000000000 +0800
Modify: 2014-01-01 12:12:00.000000000 +0800
Change: 2014-03-18 09:41:59.092678768 +0800
 Birth: -

touch -t 指令后面的时间戳格式为:[[CC]YY]MMDDhhmm[.ss]

同样的,ctime始终是当前时间

ctime,mtime,atime的区别

当你同熟练的UNIX用户进行交谈时,你经常会听到他们傲慢地讲出术语“改变时间(change time)”和“修改时间(modification time)”。对于许多人(和许多字典而言),改变和修改是相同的。这里会有什么不同那?

    改变和修改之间的区别在于是改某个组件的标签还是更改它的内容。如果有人说chmod a-w myfile,那么这是一个改变;如果有人说echo foo >> myfile,那么
这是一个修改。改变是文件的索引节点发生了改变;修改是文本本身的内容发生了变化。[文件的修改时间也叫时间标志 (timestamp).]

    只要讨论改变时间和修改时间,就不可能不提到“访问时间(access time)”.访问时间是文件最后一次被读取的时间。因此阅读一个文件会更新它的访问时间,当它的改变时间并没有变化(有关文件的信息没有被改变),它的修改时间也同样如此(文件本身没有被改变)

有时,在许多地方改变时间或者“ctime”被错误地写成“创建时间”,包括某些UNIX参考手册。不要相信他们
下面是我man出来的内容,仅供参考!

 

 代码如下

    st_atime
           Time when file data was last accessed. Changed by  the
           following   functions:   creat(),   mknod(),   pipe(),
           utime(2), and read(2).

     st_mtime
           Time when data was last modified. Changed by the  fol-
           lowing  functions:  creat(), mknod(), pipe(), utime(),
           and write(2).

     st_ctime
           Time when file status was last changed. Changed by the
           following   functions:   chmod(),   chown(),  creat(),
           link(2),  mknod(),  pipe(),  unlink(2),  utime(),  and
           write().

ls显示出的time应该是mtime。

touch后,文件的三个时间应该都会改变,可以试一试。

问题描述
文件的 ctime、mtime、atime 之间有什么区别?
配置信息
解决方法
文件的 Access time,atime 是在读取文件或者执行文件时更改的。
文件的 Modified time,mtime 是在写入文件时随文件内容的更改而更改的。
文件的 Create time,ctime 是在写入文件、更改所有者、权限或链接设置时随 Inode 的内容更改而更改的。

因此,更改文件的内容即会更改 mtime 和 ctime,但是文件的 ctime 可能会在 mtime 未发生任何变化时更改 - 在权限更改,但是文件内容没有变化的情况下。
ls(1) 命令可用来列出文件的 atime、ctime 和 mtime。
ls -lc filename         列出文件的 ctime
ls -lu filename         列出文件的 atime
ls -l filename          列出文件的 mtime

    atime不一定在访问文件之后被修改,因为:使用ext3文件系统的时候,如果在mount的时候使用了noatime参数那么就不会更新atime的 信息。而这是加了 noatime 取消了, 不代表真??情?r.反正, ?三?? time stamp 都放在 inode 中.若 mtime, atime 修改, inode 就一定??? 既然 inode 改了, 那 ctime 也就跟著要改了.之所以在 mount option 中使用 noatime, 就是不想 file system 做太多的修改, 而改善?取效能.

本文来源:http://www.bbyears.com/shujuku/52834.html