linux文件时间属性 查看和修改文件时间

来源:互联网 发布:专业淘宝图片拍摄德州 编辑:程序博客网 时间:2024/05/18 00:16

Linux下一个文件有三个主要的变动时间,mtime,ctime,atime:
mtime(modification time):当文件的内容更改时,就会更新这个时间。
ctime(status time):当文件的状态被更改时,会更改这个时间,比如像文件的权限或者属性被更改时就会更改这个时间。
atime(access time):就是文件访问时间,当文件的内容被读取时就会更改这个时间。比如使用cat指令读取某个文件时,这个时间就会被更改。

者三个时间可以使用可以同过加 –time 参数来获取
ll 文件名 这种方式会默认展现mtime
ll –time=atime 文件名 获取atime
ll –time=ctime 文件名 获取ctime

[root@localhost tmp]# ll timetest -rw-r--r-- 1 root root 0 Oct 29 22:19 timetest[root@localhost tmp]# ll --time=atime timetest -rw-r--r-- 1 root root 0 Oct 29 22:19 timetest[root@localhost tmp]# ll --time=ctime timetest -rw-r--r-- 1 root root 0 Oct 29 22:19 timetest

上面这个例子中,我的文件是刚刚创建的,所以显示的时间都是一样的。可以过几分钟用cat指令访问这个文件,再使用chmod指令修改这个文件权限,在来查看这个文件的三个时间时,会发现有变化了。

有时候由于时区等问题,你的文件时间可能和标准时间不一致,这个时候就可以使用touch指令来修改文件时间。

把时间修改到三天前

[root@localhost tmp]# touch -d "3 days ago" timetest [root@localhost tmp]# ll timetest ; ll --time=atime timetest ; ll --time=ctime timetest -rw-r--r-- 1 root root 0 Oct 26 22:29 timetest-rw-r--r-- 1 root root 0 Oct 26 22:29 timetest-rw-r--r-- 1 root root 0 Oct 29 22:29 timetest

把时间修改到某一个具体的时间点

[root@localhost tmp]# touch -t 1710262210 timetest [root@localhost tmp]# ll timetest ; ll --time=atime timetest ; ll --time=ctime timetest -rw-r--r-- 1 root root 0 Oct 26 22:10 timetest-rw-r--r-- 1 root root 0 Oct 26 22:10 timetest-rw-r--r-- 1 root root 0 Oct 29 22:30 timetest

注意:上面两个指令都指修改了mtime和atime ,ctime并没有被修改,要想修改ctime可以使用touch -c 文件 来实现

[root@localhost tmp]# touch -c timetest [root@localhost tmp]# ll timetest ; ll --time=atime timetest ; ll --time=ctime timetest -rw-r--r-- 1 root root 0 Oct 29 22:35 timetest-rw-r--r-- 1 root root 0 Oct 29 22:35 timetest-rw-r--r-- 1 root root 0 Oct 29 22:35 timetest

touch -c 文件名 这个指令会修改文件的三个时间,当文件不存在时会创建文件
touch -a 文件名 这个指令我使用man来查看时,上面说明只会修改文件的访问时间,但是在实际操作的时候发现除了mtime每变之外,atime和ctime都发生了改变。这个就有点百思不得其解了。先在这做个标记吧,有大神知道也可以指点一下。

[root@localhost tmp]# touch -a timetest [root@localhost tmp]# ll timetest ; ll --time=atime timetest ; ll --time=ctime timetest -rw-r--r-- 1 root root 0 Oct 29 22:35 timetest-rw-r--r-- 1 root root 0 Oct 29 22:37 timetest-rw-r--r-- 1 root root 0 Oct 29 22:37 timetest