对于linux系统中的文件或目录,有三种时间状态,分别为ctime、mtime和atime,各个时间表示的意思如下:
atime:Access Time 最后一次访问文件(读取或执行)或目录的时间
mtime:Modofy Time 最后一次修改文件(内容)或目录(内容)的时间
ctime:Change Time 最后一次改变文件(属性)或目录(属性)的时间
如何查看文件或目录的atime/mtime/ctime
1、stat filename
2、
atime:ls -lu filename
mtime:ls -l filename
ctime:ls -lc filename
示例演示
在/tmp下新建一个目录tm,tm下新建一个文件a.txt
# cd /tmp
# mkdir tm
# cd tm
# stat ../tm
File: `../tm'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: ca01h/51713dInode: 1050207 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 10:57:32.993350411 +0800
Modify: 2014-03-17 10:57:32.993350411 +0800
Change: 2014-03-17 10:57:32.993350411 +0800
Birth: -
# touch a.txt
# stat a.txt
File: `a.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713dInode: 1050208 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 10:58:01.328805868 +0800
Modify: 2014-03-17 10:58:01.328805868 +0800
Change: 2014-03-17 10:58:01.328805868 +0800
Birth: -
新创建的目录和文件的atime/mtime/ctime都是一样的。
打开a.txt,随便输入点内容然后保存。
# vi a.txt
# stat a.txt
File: `a.txt'
Size: 3 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713dInode: 1050208 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:02:22.399782635 +0800
Change: 2014-03-17 11:02:22.423782143 +0800
Birth: -
vi a.txt的时候会修改文件的atime,输入内容保存后会修改mtime,同时因为文件的属性变更了(比如文件大小就变了),所以ctime也会改变。
修改文件的属性,ctime变化,mtime和atime不变。
# chmod g+w a.txt
# stat a.txt
File: `a.txt'
Size: 9 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713dInode: 1050208 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:02:22.399782635 +0800
Change: 2014-03-17 11:07:10.114234176 +0800
Birth: -
修改内容一定会改变atime吗?答案是否!
# echo 'world' >> a.txt
# stat a.txt
File: `a.txt'
Size: 9 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713dInode: 1050208 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-17 11:02:19.619836157 +0800
Modify: 2014-03-17 11:09:07.980591047 +0800
Change: 2014-03-17 11:09:07.980591047 +0800
Birth: -