习惯了winddow系统,每个文件或者目录都有三个时间属性。分别是文件的创建时间, 文件的修改时间 和文件的访问时间。 Linux的文件或者目录也有三个关于时间的属性,分别是文件的访问时间, 文件的修改时间, 文件状态改变的时间, 这里需要注意的是linux中没有文件创建的时间。
名称 | 含义 | 缩写 |
---|---|---|
访问时间 | Access | atime |
修改时间 | Modify | mtime |
改变时间(文件内容的改变,或者文件元信息的改变) | Change | Ctime |
使用命令 stat 命令查看三个时间
stat a.sh
File: ‘a.sh’
Size: 317 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 165866 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-10-09 20:33:20.000901020 +0800
Modify: 2017-10-09 20:33:19.995900890 +0800
Change: 2017-10-09 20:33:19.998900968 +0800
Birth: -
使用 stata 命令查看文件的信息,不会更改文件的访问时间。使用命令 cat, more, less 等则会修改文件的访问时间。
文件修改的时间,如果文件进行了修改, 文件的 mtime 一定会改变,同时 ctime 也会改变。
文件改变的时间,可能是内容的改变(mtime变化), 也可能是文件的元信息变化,比如 文件的所有者变化了,或者文件的属性(chattr)变化。
示例分析
首先创建了一个文件
touch test.txt
stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1099933 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-11-13 21:54:13.886745796 +0800
Modify: 2017-11-13 21:54:13.886745796 +0800
Change: 2017-11-13 21:54:13.886745796 +0800
对文件进行修改
echo "test" >> test.txt
$ stat test.txt
File: `test.txt'
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 1099933 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-11-13 21:54:13.886745796 +0800
Modify: 2017-11-13 21:55:26.438533898 +0800
Change: 2017-11-13 21:55:26.438533898 +0800
可以看到有两个时间发生了变化,一个是修改的时间(mtime),一个是改变的时间(ctime)。
如果仅仅修改 改变的时间 ctime
chattr +a test.txt
stat test.txt
File: `test.txt'
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 1099933 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-11-13 21:54:13.886745796 +0800
Modify: 2017-11-13 21:55:26.438533898 +0800
Change: 2017-11-13 21:57:17.511209387 +0800
这里可以看到仅仅修改了 ctime ,其他的两个时间(atime, mtime)变化。
如果仅仅修改 atime, 一个cat命令就可以做到
$ cat test.txt
test
stat test.txt
File: `test.txt'
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 1099933 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-11-13 21:58:49.886939476 +0800
Modify: 2017-11-13 21:55:26.438533898 +0800
Change: 2017-11-13 21:57:17.511209387 +0800
如果使用了编辑器进行了文件的编辑,及时文件没有修改,仅仅保存一下。通常三个时间都会被修改。
使用ls 查看三个时间
ls -l # 文件的最后修改时间
ls -lu # 文件最后访问的时间, u 可以理解为用户,用户看了文件,就会改动这个时间
ls -lc # cime