Linux的三种时间分别指的是atime、ctime和mtime。
atime(access time):最后一次访问文件的时间。
st_atime
Time when file data was last accessed. Changed by the following functions: creat(), mknod(), pipe(), utime(2), and read(2).
ctime(change time):文件状态最后被修改的时间。
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().
mtime(modify time): 最后一次修改文件的时间。
st_mtime
Time when data was last modified. Changed by the following functions: creat(), mknod(), pipe(), utime(),and write(2).
科(装)普(逼)完毕,举个栗子。
文件:
首先以文件为例,这里我新建了一个空文件testfile。用stat命令查看,可以看到三种时间是一致的。
[root@localhost testdir]# stat testfile
File: "testfile"
Size: 17 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 141566 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-07 03:14:46.734826221 +0800
Modify: 2016-03-07 03:14:46.734826221 +0800
Change: 2016-03-07 03:14:46.734826221 +0800
用cat命令查看一下文件,发现atime发生变化,其余不变。
[root@localhost testdir]# cat testfile
[root@localhost testdir]# stat testfile
File: "testfile"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 141564 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-07 03:36:49.608825105 +0800
Modify: 2016-03-07 03:36:28.962825120 +0800
Change: 2016-03-07 03:36:28.962825120 +0800
用chmod命令修改文件的权限,发现ctime发生变化,其余不变。
[root@localhost testdir]# chmod 444 testfile
[root@localhost testdir]# stat testfile
File: "testfile"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 141564 Links: 1
Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-07 03:36:49.608825105 +0800
Modify: 2016-03-07 03:36:28.962825120 +0800
Change: 2016-03-07 03:39:32.639824942 +0800
用echo命令修改文件的内容,mtime和ctime都发生变化,但atime不变。
[root@localhost testdir]# echo 'test' >testfile
[root@localhost testdir]# stat testfile
File: "testfile"
Size: 5 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 141564 Links: 1
Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-07 03:36:49.608825105 +0800
Modify: 2016-03-07 03:41:59.075824807 +0800
Change: 2016-03-07 03:41:59.075824807 +0800
文件夹:
之后,再用外层的testdir文件夹测试下。首先保持文件夹三种时间一致。[root@localhost ~]# touch testdir/
[root@localhost ~]# stat testdir/
File: "testdir/"
Size: 4096 Blocks: 8 IO Block: 4096 目录
Device: fd00h/64768d Inode: 135010 Links: 2
Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-07 03:47:00.913824582 +0800
Modify: 2016-03-07 03:47:00.913824582 +0800
Change: 2016-03-07 03:47:00.913824582 +0800
用ls命令查看下文件夹的内容,atime发生变化,其余不变。
[root@localhost ~]# ls testdir/
testfile
[root@localhost ~]# stat testdir/
File: "testdir/"
Size: 4096 Blocks: 8 IO Block: 4096 目录
Device: fd00h/64768d Inode: 135010 Links: 2
Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-07 03:48:16.772824524 +0800
Modify: 2016-03-07 03:47:00.913824582 +0800
Change: 2016-03-07 03:47:00.913824582 +0800
用chown修改文件夹的拥有者,ctime发生变化,其余不变。
[root@localhost ~]# chown ftp testdir/
[root@localhost ~]# stat testdir/
File: "testdir/"
Size: 4096 Blocks: 8 IO Block: 4096 目录
Device: fd00h/64768d Inode: 135010 Links: 2
Access: (0555/dr-xr-xr-x) Uid: ( 14/ ftp) Gid: ( 0/ root)
Access: 2016-03-07 03:48:16.772824524 +0800
Modify: 2016-03-07 03:47:00.913824582 +0800
Change: 2016-03-07 03:52:39.465824300 +0800
用echo命令再次修改文件夹中的内容,mtime和ctime发生变化,atime不变。
[root@localhost ~]# echo 'test1' >> testdir/testfile
[root@localhost ~]# stat testdir/testfile
File: "testdir/testfile"
Size: 11 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 141564 Links: 1
Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-07 03:36:49.608825105 +0800
Modify: 2016-03-07 03:55:15.232824183 +0800
Change: 2016-03-07 03:55:15.232824183 +0800
用vim修改文件夹的内容,三时间都发生了变化。
[root@localhost ~]# vim testdir/testfile
[root@localhost ~]# stat testdir/testfile
File: "testdir/testfile"
Size: 19 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 141563 Links: 1
Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-07 04:00:10.308824013 +0800
Modify: 2016-03-07 04:00:10.308824013 +0800
Change: 2016-03-07 04:00:10.319823913 +0800
通过以上的测试,我得出了以下结论:
1.在文件夹中新建文件,文件夹的atime会发生变化。
这解释了为什么用echo命令修改文件,atime不变,而用vim进行修改时,atime也会变化。因为用vim编辑时会产生.tmp文件。
2.mtime发生变化,ctime也会变化。ctime发生变化,mtime未必发生变化。
我再刚接触三时间概念的时候,曾想当然的认为ctime就是create time,这是个误区。ctime包含mtime,这里用了chown和chmod命令进行了验证。