快捷搜索:   nginx

linux下文件的三个主要修改时间(zt)

Linux有3个修改, 很多朋友都喜欢弄混淆, 现在列出来供参考.

1) modification time (mtime,文件内容修改时间): 这个时间指的是文件内容修改的时间, 而不是文件属性的修改, 当数据内容修改时, 这个时间就会改变, 用命令ls -l默认显示的就是这个时间.

2) status time (ctime,文件状态改变时间): 当一个文件的状态改变时, 这个时间就会改变, 例如更改了文件的权限与属性等, 它就会改变.

3) access time (atime,文件最后访问时间): 当读取文件内容时, 就会更改这个时间, 例如使用cat 去读取/etc/man.config,那么该文件的atime就会改变.

例如:

$ touch test.txt
$ ls -l --full-time test.txt
-rw-r--r--  1 oracle oinstall 0 2010-03-17 11:45:12.000000000 +0800 test.txt
$ ls -l --time=atime --full-time test.txt
-rw-r--r--  1 oracle oinstall 0 2010-03-17 11:45:12.000000000 +0800 test.txt
$ ls -l --time=ctime --full-time test.txt
-rw-r--r--  1 oracle oinstall 0 2010-03-17 11:45:12.000000000 +0800 test.txt

cat 执行之后再查看atime, 该时间已经更改:
$ cat test.txt
$ ls -l --time=atime --full-time test.txt
-rw-r--r--  1 oracle oinstall 0 2010-03-17 11:46:59.000000000 +0800 test.txt

更改文件权限再查看ctime, 该时间已经更改:
$ chmod o-r test.txt
$ ls -l --time=ctime --full-time test.txt
-rw-r-----  1 oracle oinstall 0 2010-03-17 11:50:09.000000000 +0800 test.txt

如果对文件内容进行编辑, 那么mtime就会改变了.

如果要一起查看文件的三个时间情况, 可以用命令stat进行查看:

$ stat test.txt
File: `test.txt'
Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 6806h/26630d    Inode: 16          Links: 1
Access: (0640/-rw-r-----)  Uid: (  500/  oracle)   Gid: (  500/oinstall)
Access: 2010-03-17 11:46:59.000000000 +0800
Modify: 2010-03-17 11:45:12.000000000 +0800
Change: 2010-03-17 11:50:09.000000000 +0800

时间与上面的时间完全吻合.

文件的时间很重要, 因为如果误判文件时间, 可能会造成某些程序无法正常运行, 万一我们发现一个文件的时间是未来的时间(很多时候会有这个问题, 我们在安装的时候提到的GMT时间就是那个意思), 那么怎样才能让次时间变成现在的时间呢? 我们只需要一个 touch命令即可.

touch的用法为:

touch [-actmd] 文件

参数:
-a:仅修改access time
-c:仅修改时间而不建立文件
-t:后面可以接时间, 格式为: [YYMMDDhhmm]
-m:仅修改mtime
-d:后面可以接日期, 也可以使用--date="日期或时间"

例如:
将test.txt的日期调整为两天前, 则:

$ touch -d "2 days ago" test.txt
$ stat test.txt
File: `test.txt'
Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 6806h/26630d    Inode: 16          Links: 1
Access: (0640/-rw-r-----)  Uid: (  500/  oracle)   Gid: (  500/oinstall)
Access: 2010-03-15 11:56:07.000000000 +0800
Modify: 2010-03-15 11:56:07.000000000 +0800
Change: 2010-03-17 11:56:07.000000000 +0800

结果atime和mtime将会改变而ctime不会改变.

通过touch命令, 可以轻松的修改文件文件的日期与时间, 并且也可以建立一个空文件, 不过要注意的是, 即使复制一个文件并复制所有属性也没有办法复制ctime属性.

touch命令常用的情况是:

1)建立一个空文件
2)将某个文件日期修改为当前日期(mtime和atime)

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论