实际使用的文件的出生日期是什么时候?
当我输入以下内容时:
$ 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: -
我看到/bin/bash
的出生日期为空/空。 这个字段什么时候被使用过,当Linux工作时它有什么用途?
我很欣赏有一种更短的使用stat
方法,但这在开发周期中出现,我复制并粘贴。
出生时间是在文件系统上创建文件的时间,也称为文件创建时间(在crtime
上为crtime)。 注意,这不是由POSIX定义的; 只有最后访问时间( atime
),最后修改时间( mtime
)和inode更改时间( ctime
)是POSIX强制要求的。
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]; };
这里stx_btime
是文件创建时间。
在此期间, stat
显示缺少stat()
调用返回的字段(或空值) st_birthtime
/ st_birthtimesec
,在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 */ };
有一些文件系统级调试请求的技巧来从FS元数据获取创建信息,例如EXTFS:
debugfs -R 'stat /path/to/file' /dev/sda1
假设有问题的文件的FS位于分区/dev/sda1
。 您可以提取crtime
的值以获取文件的创建时间。
应该是文件创建时间,但是有一个突出的问题,gnu coreutils团队在内核中等待xstat()
支持。
TODO:stat(1)和ls(1)支持出生时间