Linux下文件的时间属性node
每一个文件都有三个主要的时间属性,这三个时间属性的意义是什么呢?web
modify time (mtime)bash
当该文件的“内容数据”更改时,就会更新这个时间。内容数据指的是文件的内容,而不是文件的属性或权限。ide
change time (ctime)性能
当该文件的“状态、元数据”更改时,就会更新这个时间。好比更改文件权限、属主、属组、文件名等元数据。this
access time (atime)spa
当该文件的“内容被访问、读取”时,就会更新这个读取时间(access)。好比经过cat,more命令去读取文件,那么就会更新该文件的atime了。
orm
如何【查看】文件的三个时间属性?开发
stat命令, 能够显示文件或者文件系统状态。
it
语法: stat [OPTION]... FILE... [root@web ~]# stat strerror.c File: `strerror.c' Size: 201 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 143765 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-02-11 21:00:46.976652765 +0800 Modify: 2016-02-11 21:00:40.286651956 +0800 Change: 2016-02-11 21:00:40.342654314 +0800
默认状况下, ls 显示出来的是该文件的 mtime, 也就是这个文件的内容上次被更改的时间。
[root@web ~]# ls -l strerror.c -rw-r--r-- 1 root root 201 Feb 11 2016 strerror.c [root@web ~]# ls -l --time=atime strerror.c -rw-r--r-- 1 root root 201 Feb 11 2016 strerror.c [root@web ~]# ls -l --time=ctime strerror.c -rw-r--r-- 1 root root 201 Feb 11 2016 strerror.c
如何【修改】文件的三个时间属性?
touch命令能够修改文件的时间属性。
touch [OPTION]... FILE... -a change only the access time -m change only the modification time -r, --reference=FILE use this file’s times instead of current time
针对 access time 没有改变的问题
有时候,用 cat filename,或者 more filename 访问相应文件时,并无刷新文件的 access time ,而是在修改了文件内容以后,才刷新了access time, 以及 modity time。
官方解释:
在kernel版本 2.6.30以前, Linux的核心开发人员针对 ext3/ext4文件系统的性能进行讨论,其中包括 atime。 在 kernel 2.6.30 以前,文件系统中默认会及时的更新atime,这样会带来两个问题。
(1)系统中大量的文件访问,将atime写入到磁盘中,消耗时间,从而下降系统性能
(2)频繁的更新操做,也会消耗大量电能
其实在Linux上不多程序须要获取精确的atime时间,而且Linux核心开发人员从ext3/ext4文件系统的性能角度出发,决定在2.6.30版本的内容中修改atime的更新方式,只有在如下三种状况之一才会更新atime:
(1)若是将分区mount挂载的时候指定采用非relatime方式(默认采用 relatime方式),如 strictatime。补充:在OS启动的时候,将各个分区挂载到不一样的目录,在挂载(mount)的参数中采用 strictatime,代表及时更新atime。在2.6.30以后mount添加了 relatime 和 strictatime 两个选项,详细的能够经过 man mount 查看。
(2)atime 小于 ctime 或者 小于 mtime 的时候
(3)本次的 access time 和 上次的 access time 超过24个小时