Linux修改、访问、改变时间简介
一、三种时间简介:
mtime
atime
ctime
时间种类 | 全拼 | 含义 | 解释 |
---|---|---|---|
mtime | modified time | 文件的修改时间 | 文件内容的变化时间 |
ctime | change time | 文件属性的改变时间 | 硬链接数量、文件大小、文件权限 |
atime | access time | 文件的访问时间 | 例如cat命令查看的时间 |
二、查看三种时间方法
通过命令:stat [文件名] 即可查看。
[root@localhost oldboy]# stat coolman.txt
File: ‘coolman.txt’
Size: 65 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68858363 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2020-02-06 00:06:51.916081051 -0800
Modify: 2020-02-06 00:06:45.975080479 -0800
Change: 2020-02-06 00:06:45.975080479 -0800
Birth: -
三、修改三种时间方法
1、以文本为例,修改文本内容。
[root@localhost oldboy]# echo lalala >>coolman.txt
[root@localhost oldboy]# stat coolman.txt
File: ‘coolman.txt’
Size: 72 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68858363 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2020-02-06 00:06:51.916081051 -0800
Modify: 2020-02-06 00:07:36.734085368 -0800
Change: 2020-02-06 00:07:36.734085368 -0800
Birth: -
我添加单词“lalala”,并再次查看文件“coolman”三种时间,发现mtime(文件内容修改时间)发生变化。与此同时,ctime(文件属性修改时间)也发生了变化,这是为什么呢?因为文件的大小变了。
2、以文本为例,为目标文本创建硬链接。
[root@localhost oldboy]# stat coolman.txt
File: ‘coolman.txt’
Size: 72 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68858363 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2020-02-06 00:06:51.916081051 -0800
Modify: 2020-02-06 00:07:36.734085368 -0800
Change: 2020-02-06 00:07:36.734085368 -0800
Birth: -
[root@localhost oldboy]# ln coolman.txt coolman.txt-hard
[root@localhost oldboy]# stat coolman.txt
File: ‘coolman.txt’
Size: 72 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68858363 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2020-02-06 00:06:51.916081051 -0800
Modify: 2020-02-06 00:07:36.734085368 -0800
Change: 2020-02-06 00:16:58.682139496 -0800
Birth: -
结果发现,创建硬链接后,文本内容并未发生改变,mtime和Atime均未变化。因为文件属性改变(硬链接数),所以仅ctime(文件属性修改时间)发生变化。
3、使用cat访问文件。
[root@localhost oldboy]# stat coolman.txt
File: ‘coolman.txt’
Size: 72 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68858363 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2020-02-06 00:06:51.916081051 -0800
Modify: 2020-02-06 00:07:36.734085368 -0800
Change: 2020-02-06 00:16:58.682139496 -0800
Birth: -
[root@localhost oldboy]# cat coolman.txt
hello
i am a cool man
i am enjoy about this title.
that is all.
lalala
[root@localhost oldboy]# stat coolman.txt
File: ‘coolman.txt’
Size: 72 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68858363 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2020-02-06 00:30:06.031215334 -0800
Modify: 2020-02-06 00:07:36.734085368 -0800
Change: 2020-02-06 00:16:58.682139496 -0800
Birth: -
通过命令cat查看文件后,仅Atime发生变化。
还没完,如果每看一次,就修改一次atime,因为查看时间也属于文件属性,必然ctime也会跟这变,文件记录会增大,这样磁盘占用空间会一直增加,怎么办?
其实Atime已经设定好了,只有在修改文件内容后,Atime才会变,也就是说,mtime发生变化,Atime才会发生变化,减少变动。
以上面内容为例,我再次查看文件后,查询Atime变化。
[root@localhost oldboy]# cat coolman.txt
hello
i am a cool man
i am enjoy about this title.
that is all.
lalala
[root@localhost oldboy]# stat coolman.txt
File: ‘coolman.txt’
Size: 72 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68858363 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2020-02-06 00:30:06.031215334 -0800
Modify: 2020-02-06 00:07:36.734085368 -0800
Change: 2020-02-06 00:16:58.682139496 -0800
Birth: -
结果显示,Atime并未发生变化。
就酱。