linux下的-atime,-ctime,-mtime含义
我们经常会在论坛或者群里面被问到,在linux或者unix下如何查看某文件的创建日期?
经常又会有人说用find命令加选项-ctime,其实这里的-ctime并非是create time,而是change time。
在linux或者unix这类操作系统,并没有为我们保存文件的创建日期。[@more@]
linux下的-atime,-ctime,-mtime含义
我们经常会在论坛或者群里面被问到,在linux或者unix下如何查看某文件的创建日期?
经常又会有人说用find命令加选项-ctime,其实这里的-ctime并非是create time,而是change time。
在linux或者unix这类操作系统,并没有为我们保存文件的创建日期。
我们可以先来看看linux系统里文件的状态信息,下面这个文件是我一周前创建的:
[root@ora01 ~]# stat 3
File: “3”
Size: 15 Blocks: 8 IO Block: 4096 一般文件
Device: fd00h/64768d Inode: 489602 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-07-17 22:14:55.000000000 +0800
Modify: 2012-07-17 15:22:45.000000000 +0800
Change: 2012-07-17 15:22:45.000000000 +0800
可以看到,文件状态里有三个时间,分别是access,modify,change。
下面我们再来看看find命令下的这几个选项:
[oracle@ora01 admin]$ man find
-atime n
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional
part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.
-mtime n
File’s data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file
modification times.
-ctime n
File’s status was last changed n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file
status change times.
发现三个时间正好与文件的三个时间状态相同,而且帮助也已经很明显的告诉了我们其具体含义:
atime的意思是access time,即文件的最近的一次访问时间,+n意思为查找n天以前的文件,-n为查找n天以内的文件
例如有一个文件data4.txt,查看一下它的状态:
[oracle@ora01 ~]$ stat data4.txt
File: `data4.txt'
Size: 49 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 458037 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 500/ oracle) Gid: ( 500/oinstall)
Access: 2012-07-10 11:46:05.000000000 +0800Modify: 2012-07-10 11:44:37.000000000 +0800
Change: 2012-07-10 11:44:37.000000000 +0800
我们来查看一下它的内容:
[oracle@ora01 ~]$ more data4.txt
"SCOTT",12,"F444"
"BRENTT",43,"54GSS"
"SYS",4566
再来看看文件的状态:
[oracle@ora01 ~]$ stat data4.txt
File: `data4.txt'
Size: 49 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 458037 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 500/ oracle) Gid: ( 500/oinstall)
Access: 2012-07-22 23:21:10.000000000 +0800Modify: 2012-07-10 11:44:37.000000000 +0800
Change: 2012-07-10 11:44:37.000000000 +0800
可以发现,只要你查看了文件的内容,无论是more,cat,vi,那么文件的access time都会更新。
mtime比较好理解,为modify time,即文件数据最新的修改时间,指的就是文件内容的最新修改时间。
[oracle@ora01 ~]$ stat ctl1.txt
File: `ctl1.txt'
Size: 288 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 458031 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 500/ oracle) Gid: ( 500/oinstall)
Access: 2012-07-22 23:46:05.000000000 +0800
Modify: 2012-07-10 11:44:05.000000000 +0800Change: 2012-07-10 11:44:05.000000000 +0800
对文件进行一下编辑:
[oracle@ora01 ~]$ echo "" >>ctl1.txt
[oracle@ora01 ~]$ stat ctl1.txt
File: `ctl1.txt'
Size: 291 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 458070 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 500/ oracle) Gid: ( 500/oinstall)
Access: 2012-07-22 23:46:05.000000000 +0800
Modify: 2012-07-22 23:46:31.000000000 +0800
Change: 2012-07-22 23:46:31.000000000 +0800发现文件的modify和change时间都变化了,change time 下面讨论。
ctime的意思是change time,文件状态最新改变的时间。是文件的status change time,何为文件的status呢?
我们都知道文件有一些个基本的属性,权限,用户,组,大小,修改时间等,只要是这些信息变化了,那么ctime都会发生变化,
所以上面修改文件内容时为何ctime会变化,因为其mtime已经变化了,mtime也是文件状态的一个。
文件状态可以通过ls -l 查看:
[root@ora01 ~]# ls -l 3
-rw-r--r-- 1 root root 15 07-17 15:22 3
下面这个文件的Change时间为2012-07-17:
[root@ora01 ~]# stat 3
File: “3”
Size: 15 Blocks: 8 IO Block: 4096 一般文件
Device: fd00h/64768d Inode: 489602 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-07-17 22:14:55.000000000 +0800
Modify: 2012-07-17 15:22:45.000000000 +0800
Change: 2012-07-17 15:22:45.000000000 +0800我们改变一下它的权限:
[root@ora01 ~]# chmod 755 3
再来看看它的Change time:
[root@ora01 ~]# stat 3
File: “3”
Size: 15 Blocks: 8 IO Block: 4096 一般文件
Device: fd00h/64768d Inode: 489602 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-07-17 22:14:55.000000000 +0800
Modify: 2012-07-17 15:22:45.000000000 +0800
Change: 2012-07-22 23:17:40.000000000 +0800再改一下它的用户:
[root@ora01 ~]# chown oracle.root 3
[root@ora01 ~]# stat 3
File: “3”
Size: 15 Blocks: 8 IO Block: 4096 一般文件
Device: fd00h/64768d Inode: 489602 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 500/ oracle) Gid: ( 0/ root)
Access: 2012-07-17 22:14:55.000000000 +0800
Modify: 2012-07-17 15:22:45.000000000 +0800
Change: 2012-07-22 23:33:59.000000000 +0800再改变一下它的所属组:
[root@ora01 ~]# chgrp oinstall 3
[root@ora01 ~]# ll
总计 4
-rwxr-xr-x 1 oracle oinstall 15 07-17 15:22 3
[root@ora01 ~]# stat 3
File: “3”
Size: 15 Blocks: 8 IO Block: 4096 一般文件
Device: fd00h/64768d Inode: 489602 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 500/ oracle) Gid: ( 500/oinstall)
Access: 2012-07-17 22:14:55.000000000 +0800
Modify: 2012-07-17 15:22:45.000000000 +0800
Change: 2012-07-22 23:36:14.000000000 +0800发现,只要是修改ls -l 里面的任何信息,那么change time都会发生变化。
总结:find命令中的ctime并非是创建时间,而是文件状态改变时间。文件的时间三属性分别为access time,modify time和change time.