Linux文件属性——三种时间

Linux修改、访问、改变时间简介

一、三种时间简介:

mtime
atime
ctime

时间种类全拼含义解释
mtimemodified time文件的修改时间文件内容的变化时间
ctimechange time文件属性的改变时间硬链接数量、文件大小、文件权限
atimeaccess 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并未发生变化。

就酱。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值