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

Ant*_*ler 69 linux timestamp date-modified

Linux 中是否有命令可以检查文件的所有时间戳?

我正在尝试查看文件上最后修改、创建和触及的日期。

slh*_*hck 91

该命令称为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
Run Code Online (Sandbox Code Playgroud)

如果要调整格式,请参阅手册页,因为输出是特定于操作系统的,并且在 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'
Run Code Online (Sandbox Code Playgroud)


YaO*_*OzI 23

根据POSIX 标准的定义,每个文件只存储三个不同的时间值:http : //pubs.opengroup.org/onlinepubs/9699919799/(参见基本定义部分 -> 4. 一般概念 -> 4.8 文件时代更新)

每个文件都有三个不同的相关时间戳:上次访问数据的时间、上次修改数据的时间和上次更改文件状态的时间。这些值在文件特征结构 struct stat 中返回,如<sys/stat.h> 中所述

来自<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.
Run Code Online (Sandbox Code Playgroud)

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

$ 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
Run Code Online (Sandbox Code Playgroud)

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


1. 时间

首先,让我们通过读取(或)、打印()或将其复制到另一个文件()来访问文件的数据。lessvimcatcp

$ 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
Run Code Online (Sandbox Code Playgroud)

2. 时间

现在,让我们改变文件的状态,通过改变许可(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!
Run Code Online (Sandbox Code Playgroud)

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


3. 时间

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

$ 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!
Run Code Online (Sandbox Code Playgroud)

4.出生时间

另请注意,较新版本stat(例如stat --version 8.13在 Ubuntu 12.04 中)具有第 4 个时间戳信息 -出生时间(文件创建时间)。虽然它现在可能不会显示正确的时间:

$ 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: -
Run Code Online (Sandbox Code Playgroud)

  • [是](http://man7.org/linux/man-pages/man2/stat.2.html)。*“自内核 2.5.48 起,stat 结构支持三个文件时间戳字段的纳秒分辨率......”* (2认同)
  • 出生时间……很高兴。因为上次修改和上次更改几乎是出于相同的目的,但仍然不同。如果***所有***四个*存在就好了。 (2认同)