何时实际使用文件的出生日期?

Win*_*nix 13 command-line stat

当我输入以下内容时:

$ IFS=$'\n'; arr=( $(stat "/bin/bash") ); for i in ${arr[@]} ; do echo $i ; done
  File: '/bin/bash'
  Size: 1037528     Blocks: 2032       IO Block: 4096   regular file
Device: 823h/2083d  Inode: 656086      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-23 16:38:03.848124217 -0600
Modify: 2017-05-16 06:49:55.000000000 -0600
Change: 2017-05-18 07:43:22.946694155 -0600
 Birth: -
Run Code Online (Sandbox Code Playgroud)

我看到出生日期为/bin/bash空/空。这个字段什么时候使用过,它在 Linux 中的作用是什么?

我很欣赏有一种更短的使用方式,stat但这是在开发周期中出现的,我复制并粘贴了。

hee*_*ayl 20

出生时间是文件在文件系统上创建的时间,也称为文件创建时间(crtime在 EXTFS 上)。请注意,这不是由 POSIX 定义的;POSIX 只规定了上次访问时间 ( atime)、上次修改时间 ( mtime) 和 inode 更改时间 ( ctime)。

IIRC,Linux没有提供任何获取出生时间的接口,有一个xstat()fxstat()的建议,尚未实现。

正如@muru 所指出的,较新的方法是statx(),它最近已合并到主线内核中。因此,任何(修改后的)用户空间工具都可以statx在任何此类最新内核上利用它(现在的结构,见下文)。

struct statx {
    __u32   stx_mask;
    __u32   stx_blksize;
    __u64   stx_attributes;
    __u32   stx_nlink;
    __u32   stx_uid;
    __u32   stx_gid;
    __u16   stx_mode;
    __u16   __spare0[1];
    __u64   stx_ino;
    __u64   stx_size;
    __u64   stx_blocks;
    __u64   __spare1[1];
    struct statx_timestamp  stx_atime;
    struct statx_timestamp  stx_btime;
    struct statx_timestamp  stx_ctime;
    struct statx_timestamp  stx_mtime;
    __u32   stx_rdev_major;
    __u32   stx_rdev_minor;
    __u32   stx_dev_major;
    __u32   stx_dev_minor;
    __u64   __spare2[14];
};
Run Code Online (Sandbox Code Playgroud)

stx_btime是文件创建时间。

同时,stat在结构中显示没有字段(或空值)st_birthtime/st_birthtimesecstat()调用返回stat

struct stat {
   dev_t     st_dev;     /* ID of device containing file */
   ino_t     st_ino;     /* inode number */
   mode_t    st_mode;    /* protection *
   nlink_t   st_nlink;   /* number of hard links */
   uid_t     st_uid;     /* user ID of owner */
   gid_t     st_gid;     /* group ID of owner */
   dev_t     st_rdev;    /* device ID (if special file) */
   off_t     st_size;    /* total size, in bytes */
   blksize_t st_blksize; /* blocksize for filesystem I/O */
   blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
   time_t    st_atime;   /* time of last access */
   time_t    st_mtime;   /* time of last modification */
   time_t    st_ctime;   /* time of last status change */
   };
Run Code Online (Sandbox Code Playgroud)

文件系统级调试请求有一些技巧可以从 FS 元数据中获取创建信息,例如 EXTFS:

debugfs -R 'stat /path/to/file' /dev/sda1
Run Code Online (Sandbox Code Playgroud)

假设有问题的文件的 FS 在 partition 上/dev/sda1。您可以提取 的值crtime以获取文件的创建时间。

  • 值得指出的是,文件创建时间通常并不是特别有用,因为许多文本编辑器将通过创建新文件然后覆盖旧文件来保存(这意味着如果在保存过程中崩溃,您将永远不会获得部分保存的文件)。在 Windows 上,他们使用一些极端的黑客技术来使其看起来有效(除非它不起作用 - 请参阅 https://blogs.msdn.microsoft.com/oldnewthing/20050715-14/?p=34923 ),但据我所知,没有Linux 中存在这样的东西。 (2认同)