schack8888
风云使者
风云使者
  • 注册日期2010-12-06
  • 发帖数686
  • QQ
  • 火币3641枚
  • 粉丝161
  • 关注102
阅读:3531回复:0

一天一点学习Linux之atime,mtime,ctime

楼主#
更多 发布于:2011-10-24 12:59

其实这个内容本不想拿出来单独讲的,但考虑到一些朋友可能对这三个时间不是很明白,所以就和大家再一起来学习一下。

在Linux系统下,文件的时间有三种记录方式
mtime (modification time ):在写入文件时随文件内容的更改而更改的时间。我们用ls -l看到的时间,就是mtime
ctime (status time):是在写入文件、更改所有者、权限或链接设置时随Inode的内容更改而更改的时间。相当于ls -l –time=ctime所看到的时间
atime (access time):读取文件或者执行文件时更改的时间。也就是用ls -l –time=atime看到的时间

可以通过下面的三个例子对比来研究
ls -l install.log; ll –time=ctime install.log; ll –time=atime install.log
具体的内容就不在这里讲述了,大家仔细理解,我想没有什么难度的。
正是由于这些时间的关系,有可能某个文件的时间与目前系统的时间有差距。因为全世界的时间是不一样的。
上面三个时间定义也很简单,关键是要理解,加深理解的办法就是自己实践。OK,下面我们来给大家介绍个touch命令,他作用就是用来”改变文件或目录时间命令”。

改变文件或目录时间命令touch
参数说明
-a :修改atime
-m :修改mtime
-c :仅修改文件的时间(三个时间一起修改),若该文件不存在则不建立新的文件
-d :后面可以接想修改的日期而不用目前的日期,也可以使用 –date=”日期或时间”
-t :后面可以接想修改是时间而不用目前的时间,格式为[YYMMDDhhmm]

我们来看个例子
[root@yufei ~]# ls -l install.log; ll –time=ctime install.log; ll –time=atime install.log
-rwxr–r–. 1 root root 31537 Jan 20 05:09 install.log
-rwxr–r–. 1 root root 31537 Feb 9 19:25 install.log
-rwxr–r–. 1 root root 31537 Feb 11 16:28 install.log
[root@yufei ~]# touch -c install.log
[root@yufei ~]# ls -l install.log; ll –time=ctime install.log; ll –time=atime install.log
-rwxr–r–. 1 root root 31537 Feb 11 20:38 install.log
-rwxr–r–. 1 root root 31537 Feb 11 20:38 install.log
-rwxr–r–. 1 root root 31537 Feb 11 20:38 install.log
在预设的状态下,如果touch后面有接文件,则该文件的三个时间(atime/ctime/mtime)都会更新为目前的时间。若该文件不存在,则会主动的建立一个新的空的文件,与-c参数不同,-c不创建文件哦!这个可以自己touch一个文件出来看一下。

我们再来看一个例子
[root@yufei ~]# cp -a /etc/profile ./
[root@yufei ~]# ls -l profile; ls -l –time=atime profile; ls -l –time=ctime profile
-rw-r–r–. 1 root root 1459 Jun 30 2010 profile
-rw-r–r–. 1 root root 1459 Feb 11 10:51 profile
-rw-r–r–. 1 root root 1459 Feb 11 20:46 profile
第一行为此文件的mtime,也就是这个文件创建或编辑的时间
第二行为此文件的atime,这个时间是记录此文件的最后访问时间
第三行为此文件的ctime,因为这个文件是刚复制过来的,此文件的I节点与原文件肯定不一样了
[root@yufei ~]# ls -li /etc/profile;ls -li profile
651556 -rw-r–r–. 1 root root 1459 Jun 30 2010 /etc/profile
522034 -rw-r–r–. 1 root root 1459 Jun 30 2010 profile
希望通过上面的讲解,大家能对这三个时间能明白了。
从上面的例子我们可以看出,即使我们复制一个文件时,复制所有的属性,但也没有办法复制ctime这个属性的。ctime可以记录这个文件最近的状态(status)被改变的时间。

如果想改变时间,我们可以通过下面的方法来实现
如将刚才的文件,日期和时间改成”2010/11/22 8:08″
[root@yufei ~]# touch -t 1011220808 profile
[root@yufei ~]# ls -l profile; ls -l –time=atime profile; ls -l –time=ctime profile
-rw-r–r–. 1 root root 1459 Nov 22 08:08 profile
-rw-r–r–. 1 root root 1459 Nov 22 08:08 profile
-rw-r–r–. 1 root root 1459 Feb 11 21:02 profile
我们看到atime与mtime都改变了,但ctime则是记录的是系统目前的时间。

讲了这么多,其实touth常用的功能就只有两个
1、创建一个空的文件
2、修改时间(mtime atime)不过,这个功能用到的也不多

希望通过上面的讲解,大家能对这三个时间能有个深刻的理解了。

喜欢0 评分0
兼职版主
游客

返回顶部