2012-02-07 14:59:56 +0000 2012-02-07 14:59:56 +0000
54
54
Advertisement

如何检查一个文件的所有时间戳?

Advertisement

Linux中有没有一个命令可以检查一个文件的所有时间戳?

我想查看文件的最后修改、创建和触及的日期。

Advertisement
Advertisement

答案 (2)

75
75
75
2012-02-07 15:03:39 +0000

这个命令叫做 stat .

$ stat test
234881026 41570368 -rw-r--r-- 1 werner staff 0 0 "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" 4096 0 0 test

如果你想调整格式,请参考手册页面,因为输出是特定的操作系统,在Linux/Unix下有所不同。

一般来说,你也可以通过正常的目录列表获得时间。

  • ls -l输出最后一次修改文件内容的时间,mtime
  • ls -lc输出最后一次修改文件状态的时间,ctime 有什么区别? )
  • ls -lu输出最后一次访问时间,atime (虽然这个概念的有用性是 有待讨论 )

当然,ctime并不记录文件被 “创建 "的时间。POSIX规范只定义了三个时间戳,但是一些Linux文件系统存储了出生时间/创建时间。如何找到文件的创建日期? 在这样的支持配置上,可以使用

stat --printf '%n\nmtime: %y\nctime: %z\natime: %x\ncrtime:%w\n'
```。
21
21
21
2014-03-15 09:24:38 +0000

每一个文件只有三个不同的时间值,正如POSIX标准所定义的那样: http://pubs.opengroup.org/onlinepubs/9699919799/ (参见基础定义_部分 -> 4.

每个文件都有三个不同的时间戳:最后一次数据访问的时间,最后一次数据修改的时间,以及最后一次文件状态改变的时间。这些值在文件特性结构struct stat中返回,如 <sys/stat.h> _.

而从

atime is for Last data access timestamp.
mtime is for Last data modification timestamp.
ctime is for Last file status change timestamp.

下面的例子显示了atime , mtimectime之间的区别,这些例子是在GNU/Linux BASH中。你可以在Mac OS X或其他BSD Dist.中使用stat -x来查看类似的输出格式。

$ stat --version
stat (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Michael Meskes.
$
$ touch test
$ stat test
  File: `test'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:58:28.609223953 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 10:58:28.609223953 +0800

当刚刚创建文件时,三个时间戳是一样的。


1.atime

首先,让我们通过读取(lessvim)、打印出来(cat)或复制到另一个文件(cp)来**访问文件的数据。

$ cat test #Nothing will be printed out, since the file is empty
$ stat test
  File: `test'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800 <-- atime Changed!
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 10:58:28.609223953 +0800

2. ctime

现在让我们改变文件的状态,通过改变权限(chmod)或重命名(mv)

$ chmod u+x test
$ stat stet
  File: `test'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 11:04:10.178285430 +0800 <-- ctime Changed!
$    
$ mv test testing
$ stat testing
  File: `testing'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 11:06:33.342207679 +0800 <-- ctime Changed again!

注意,直到现在,文件的内容(数据)仍然与创建时相同。


3.mtime

最后,让我们通过编辑文件来修改文件的内容。

$ echo 'Modify the DATA of the file' > testing
$ echo 'Modify the DATA of the file also change the file status' > testing
$ stat testing
  File: `testing'
  Size: 56 Blocks: 8 IO Block: 4096 regular file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 11:09:48.247345148 +0800 <-- mtime Changed!
Change: 2014-03-16 11:09:48.247345148 +0800 <-- ctime also Changed!

4.出生时间

还要注意的是,新版本的stat(例如Ubuntu 12.04中的stat --version 8.13)有第四个时间戳信息–出生时间(文件创建时间)。虽然目前可能还没有显示正确的时间。

$ stat --version
stat (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Michael Meskes.
$
$ stat birth_time
  File: `birth_time'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 4073946 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ bingyao) Gid: ( 1000/ bingyao)
Access: 2014-03-16 10:46:48.838718970 +0800
Modify: 2014-03-16 10:46:48.838718970 +0800
Change: 2014-03-16 10:46:48.838718970 +0800
 Birth: -
Advertisement

相关问题

6
10
5
37
16
Advertisement
Advertisement