[Linux] 文件的atime、mtime和ctime

1.使用stat命令查看文件的三个时间
新创建的文件atime,mtime,ctime是一样的,也就是创建时间,但是之后再想查创建时间没有找到方法。

[root@xxx testtime]# ls -l
total 12
-rw-r--r--. 1 root root 4 Jan 16 11:22 aaa.txt
-rw-r--r--. 1 root root 4 Jan 16 11:23 bbb.txt
-rw-r--r--. 1 root root 4 Jan 16 11:23 ccc.txt
[root@xxx testtime]# stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796504   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:22:55.902450877 +0800
Modify: 2020-01-16 11:22:55.902450877 +0800
Change: 2020-01-16 11:22:55.902450877 +0800
 Birth: -
[root@xxx testtime]#

三种时间的介绍
atime(Access Time) -文件的最近访问时间(cat、vim等)
mtime(Modify Time) -文件的内容最近修改的时间(vim编辑,重定向写入等)
ctime(Change Time) -文件属性最近修改的时间(内容、路径、所有者、权限等变化)

(1)使用cat命令查看文件,仅atime会更新

[root@xxx testtime]# cat aaa.txt
aaa
[root@xxx testtime]# stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796504   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:26:10.860493954 +0800
Modify: 2020-01-16 11:22:55.902450877 +0800
Change: 2020-01-16 11:22:55.902450877 +0800
 Birth: -
[root@xxx testtime]#

(2)使用vim命令查看文件但不修改(:q),仅atime会更新;修改内容(:wq),三个时间都会更新

[root@xxx testtime]# vim aaa.txt
[root@xxx testtime]# stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796507   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:27:29.221913212 +0800
Modify: 2020-01-16 11:27:29.221913212 +0800
Change: 2020-01-16 11:27:29.291913584 +0800
 Birth: -
[root@xxx testtime]#

(3)使用重定向追加内容到文件,会更新mtime和ctime,由于并没有读原文件内容atime不更新

[root@xxx testtime]# stat bbb.txt
  File: ‘bbb.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:23:06.606508142 +0800
Modify: 2020-01-16 11:23:06.606508142 +0800
Change: 2020-01-16 11:23:06.606508142 +0800
 Birth: -
[root@xxx testtime]# cat aaa.txt >> bbb.txt
[root@xxx testtime]# stat bbb.txt
  File: ‘bbb.txt’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:23:06.606508142 +0800
Modify: 2020-01-16 11:28:23.973206143 +0800
Change: 2020-01-16 11:28:23.973206143 +0800
 Birth: -
[root@xxx testtime]#

(4)改变文件路径,仅ctime会更新

[root@xxx testtime]# stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796507   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:28:23.973206143 +0800
Modify: 2020-01-16 11:27:29.221913212 +0800
Change: 2020-01-16 11:27:29.291913584 +0800
 Birth: -
[root@xxx testtime]#
[root@xxx testtime]# mv aaa.txt ../;cd ..;stat aaa.txt
  File: ‘aaa.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796507   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:28:23.973206143 +0800
Modify: 2020-01-16 11:27:29.221913212 +0800
Change: 2020-01-16 11:56:17.259158698 +0800
 Birth: -
[root@xxx test]#

2.使用touch指令修改时间
(1) touch -a更新atime(ctime也会更新)

[root@xxx testtime]# date;touch -a bbb.txt
Thu Jan 16 11:29:11 CST 2020
[root@xxx testtime]# stat bbb.txt
  File: ‘bbb.txt’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:29:11.802462045 +0800
Modify: 2020-01-16 11:28:23.973206143 +0800
Change: 2020-01-16 11:29:11.802462045 +0800
 Birth: -
[root@xxx testtime]#

使用touch不带选项会更新三个时间

[root@xxx testtime]# date;touch bbb.txt;stat bbb.txt
Thu Jan 16 11:32:22 CST 2020
  File: ‘bbb.txt’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 11:32:22.494482300 +0800
Modify: 2020-01-16 11:32:22.494482300 +0800
Change: 2020-01-16 11:32:22.494482300 +0800
 Birth: -
[root@xxx testtime]#

(2) touch -d 使用指定的日期来修改

[root@xxx testtime]# touch -d "06:00:00" bbb.txt;stat bbb.txt
  File: ‘bbb.txt’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796505   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 06:00:00.000000000 +0800
Modify: 2020-01-16 06:00:00.000000000 +0800
Change: 2020-01-16 11:39:25.439745178 +0800
 Birth: -
[root@xxx testtime]#

(3)用某文件的时间来设置另一个文件的时间,会复制atime和mtime,ctime根据当前时间更新
例如用bbb.txt的时间来设置ccc.txt的时间

[root@xxx testtime]# date;touch -r bbb.txt ccc.txt;stat ccc.txt
Thu Jan 16 11:41:06 CST 2020
  File: ‘ccc.txt’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 134796506   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:var_t:s0
Access: 2020-01-16 06:00:00.000000000 +0800
Modify: 2020-01-16 06:00:00.000000000 +0800
Change: 2020-01-16 11:41:06.349285070 +0800
 Birth: -
[root@xxx testtime]#

参考资料:
查看文件的创建、修改时间 https://www.cnblogs.com/yizhipanghu/p/9634325.html
Linux系统stat指令用法 https://www.cnblogs.com/klb561/p/9241228.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值