atime,mtime和ctime的区别

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

atime、mtime、ctime

atime: 在文件被读、写、truncate、mknod、utime 时更改atime
mtime: 在文件被mknod、utime更改和写入时 更改mtime
ctime: 文件i-node最近一次修改的时间
文件所有者、组、权限 被更改时更新

Time
创建文件
更改文件内容
查看文件(只读)
修改文件属性
如权限、用户、组、chattr...

atime
Y
Y ---   读、写方式如用编辑器等打开,并更改文件内容时,同时做了查看和更改,所以此时更改atime
N ---  用>和>>或只写打开时不改变atime
Y
N

mtime
Y
Y
N
N

ctime
Y
Y
N
Y

[root@localhost ~]# php a.php
ATime: 01-30 15:45:47
MTime: 01-30 15:46:14
CTime: 01-30 15:46:14
注意: 用mv时只改变了ctime,而此时没用访问所以atime不变, 因为没有改文件内容所以mtime不变,这时改的是i-node所以ctime进行了改变
[root@localhost ~]# mv a.txt b.txt
[root@localhost ~]# vi a.php  把里面的a.txt 改成b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:45:47
MTime: 01-30 15:46:14
CTime: 01-30 15:47:08
==============================================
.创建文件时:
[root@localhost ~]# touch a.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:39:58
MTime: 01-30 15:39:58
CTime: 01-30 15:39:58
.编辑文件时
[root@localhost ~]# echo 1 >a.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:39:58
MTime: 01-30 15:42:03
CTime: 01-30 15:42:03
[root@localhost ~]# vi a.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:42:23
MTime: 01-30 15:42:23
CTime: 01-30 15:42:23
[root@localhost ~]# gedit a.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:43:17
MTime: 01-30 15:43:16
CTime: 01-30 15:43:16
[root@localhost ~]# gedit  b.txt
[root@localhost ~]# php a.php
ATime: 01-30 17:05:41
MTime: 01-30 17:05:41
CTime: 01-30 17:05:41
[root@localhost ~]# kate a.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:44:48
MTime: 01-30 15:44:48
CTime: 01-30 15:44:48
[root@localhost ~]# echo "fddddddd6" >a.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:45:47
MTime: 01-30 15:46:14
CTime: 01-30 15:46:14
[root@localhost ~]# mv a.txt b.txt
[root@localhost ~]# vi a.php  把里面的a.txt 改成b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:45:47
MTime: 01-30 15:46:14
CTime: 01-30 15:47:08
.修改b.txt文件的权限
[root@localhost ~]# chmod +x b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:45:47
MTime: 01-30 15:46:14
CTime: 01-30 15:48:42
.用echo方式向文件内写入(重定向)内容2
[root@localhost ~]# echo 2 >b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:45:47
MTime: 01-30 15:49:44
CTime: 01-30 15:49:44
..访问文件时,只是更改atime
.用cat方式来访问文件内容
[root@localhost ~]# cat b.txt
2
[root@localhost ~]# php a.php
ATime: 01-30 15:51:22
MTime: 01-30 15:49:44
CTime: 01-30 15:49:44
.用vi b.txt方式打开文件,只是查看下内容,但不修改
[root@localhost ~]# vi b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:51:57
MTime: 01-30 15:49:44
CTime: 01-30 15:49:44
.用head方式来访问文件内容
[root@localhost ~]# head b.txt
2
[root@localhost ~]# php a.php
ATime: 01-30 15:52:49
MTime: 01-30 15:49:44
CTime: 01-30 15:49:44
.用more方式来访问文件内容
[root@localhost ~]# more b.txt
2
[root@localhost ~]# php a.php
ATime: 01-30 15:53:07
MTime: 01-30 15:49:44
CTime: 01-30 15:49:44
.用less方式来访问文件内容
[root@localhost ~]# less b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:53:38
MTime: 01-30 15:49:44
CTime: 01-30 15:49:44
.truncate截取文件内容时
[root@localhost ~]# cat a.c
#include
#include
int main()
{
truncate("b.txt",0);
return 0;
}
[root@localhost ~]# make a
cc  a.c  -o a
[root@localhost ~]# ./a
[root@localhost ~]# php a.php
ATime: 01-30 15:53:38
MTime: 01-30 16:02:04
CTime: 01-30 16:02:04
.修改所属用户
[root@localhost ~]# chown cnscn b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:53:38
MTime: 01-30 16:02:04
CTime: 01-30 16:29:39
.修改文件属组
[root@localhost ~]# chown .cnscn b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:53:38
MTime: 01-30 16:02:04
CTime: 01-30 16:30:11
.chattr
[root@localhost ~]# chattr  -i b.txt
[root@localhost ~]# php a.php
ATime: 01-30 15:53:38
MTime: 01-30 16:02:04
CTime: 01-30 16:35:42
.综上:
1)文件只读时, 影响atime
2)文件内容修改时,同时更新mtime和ctime
3)修改文件属性时(chattr、chmod、chown),只更改ctime
即1)atime、ctime可以单独出现
2)出现mtime必定时修改ctime
[注] 测试仅仅做了上面一些,如果总结不的对,请大家补充,谢谢

转载于:https://my.oschina.net/dddttttt/blog/77792

你可能感兴趣的:(atime,mtime和ctime的区别)