Özz*_*esh 121 linux stat timestamps files
我想找出特定文件的创建日期,而不是修改日期或访问日期。
我已经尝试过ls -ltrh和stat filename。
Tho*_*man 112
stat -c '%w' file 在存储创建时间的文件系统上。
请注意,在 Linux 上,这需要coreutils8.31、2.28glibc和内核版本 4.11 或更高版本。
POSIX 标准只为每个文件定义了三个不同的时间戳:上次访问数据的时间、上次修改数据的时间和上次更改文件状态的时间。
现代 Linux 文件系统,例如 ext4、Btrfs、XFS(v5 和更高版本)和 JFS,确实存储文件创建时间(也就是出生时间),但对相关字段使用不同的名称(crtime在 ext4/XFS、otimeBtrfs 和 JFS 中) )。Linux 提供了statx(2)系统调用接口,用于检索自内核版本 4.11 起支持它的文件系统的文件出生时间。(因此,即使在文件系统中添加了创建时间支持,一些已部署的内核也没有立即支持它,即使在为该文件系统版本(例如 XFS v5)添加了名义支持之后。)
正如Craig Sanders和Mohsen Pahlevanzadeh指出的那样,在8.31 版之前,stat确实支持%w和%W格式说明符来显示文件出生时间(分别以人类可读格式和自纪元以来的秒数)coreutils。但是,仅在版本 8.31 之后才coreutils stat使用statx()可用的系统调用来检索出生时间。在coreutils8.31 版本之前,通过 gnulib (in )提供的stat访问出生时间,它从系统调用返回的结构的和字段中获取出生时间。而例如 BSD 系统(以及扩展的 OS X)通过get_stat_birthtime()lib/stat-time.hst_birthtimest_birthtimensecstatstat()st_birthtimestat,Linux没有。这就是为什么在8.31之前的 Linux 上 stat -c '%w' file输出-(指示未知的创建时间)的原因,coreutils即使对于在内部存储创建时间的文件系统也是如此。
正如Stephane Chazelas 指出的那样,一些文件系统,例如 ntfs-3g,通过扩展文件属性公开文件创建时间。
ric*_*2hs 28
TLDR;使用stap( "SystemTap" ) 创建您自己的内核 API。下面演示 ext4 创建时间提取。
您可以提取 Fedora 19 系统上的 ext4 创建时间。这是我的:
$ uname -a
Linux steelers.net 3.11.1-200.fc19.i686.PAE #1 SMP Sat Sep 14 15:20:42 UTC 2013 i686 i686 i386 GNU/Linux
Run Code Online (Sandbox Code Playgroud)
很明显,我的 ext4 分区上的 inode 有创建时间。这是一个 shell 脚本,它确定与文件名关联的 inode,然后stat使用stap("systemtap")用创建时间来增加输出。
注意:这只是一个演示和巨大低效的,因为内核模块中创建,加载和卸载的每个执行。这也可能非常脆弱,因为没有执行错误检查。一个合适的内核 API 会更可取,但是这个脚本可以变得更有效率,并且可以读取多个文件/inode 的创建时间。
[stap_stat.sh 的内容]
#/bin/sh
my_inode_str=$(stat --printf="%i" $1)
stap - << end_of_stap_script
global my_offsetof
probe begin {
system("stat $1");
my_offsetof = &@cast(0,"struct ext4_inode_info")->vfs_inode;
}
probe kernel.function("ext4_getattr@fs/ext4/inode.c") {
probe_inode=\$dentry->d_inode;
if (@cast(probe_inode, "struct inode")->i_ino == $my_inode_str) {
my_i_crtime = &@cast(probe_inode - my_offsetof,"struct ext4_inode_info")->i_crtime;
printf("CrTime: %s GMT\n", ctime(@cast(my_i_crtime, "timespec")->tv_sec));
printf("CrTime (nsecs): %d\n", @cast(my_i_crtime, "timespec")->tv_nsec);
exit();
}
}
end_of_stap_script
Run Code Online (Sandbox Code Playgroud)
这是一个演示:
$ ll testfile
ls: cannot access testfile: No such file or directory
$ touch testfile
$ ./stap_stat.sh testfile
File: ‘testfile’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd02h/64770d Inode: 4850501 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ Rick) Gid: ( 1001/ Rick)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2013-09-28 06:17:04.221441084 -0400
Modify: 2013-09-28 06:17:04.221441084 -0400
Change: 2013-09-28 06:17:04.221441084 -0400
Birth: -
CrTime: Sat Sep 28 10:17:04 2013 GMT
CrTime (nsecs): 220441085
$ ll testfile
-rw-rw-r--. 1 Rick Rick 0 Sep 28 06:17 testfile
$ cat - >> testfile
Now is the time ...
$ ll testfile
-rw-rw-r--. 1 Rick Rick 20 Sep 28 06:18 testfile
$ ./stap_stat.sh testfile
File: ‘testfile’
Device: fd02h/64770d Inode: 4850501 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ Rick) Gid: ( 1001/ Rick)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2013-09-28 06:17:04.221441084 -0400
Modify: 2013-09-28 06:18:33.684374740 -0400
Change: 2013-09-28 06:18:33.684374740 -0400
Birth: -
CrTime: Sat Sep 28 10:17:04 2013 GMT
CrTime (nsecs): 220441085
$ cat testfile
Now is the time ...
$ ./stap_stat.sh testfile
File: ‘testfile’
Size: 20 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 4850501 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ Rick) Gid: ( 1001/ Rick)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2013-09-28 06:19:12.199349463 -0400
Modify: 2013-09-28 06:18:33.684374740 -0400
Change: 2013-09-28 06:18:33.684374740 -0400
Birth: -
CrTime: Sat Sep 28 10:17:04 2013 GMT
CrTime (nsecs): 220441085
$ mv testfile testfile2
$ ./stap_stat.sh testfile2
File: ‘testfile2’
Size: 20 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 4850501 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ Rick) Gid: ( 1001/ Rick)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2013-09-28 06:19:12.199349463 -0400
Modify: 2013-09-28 06:18:33.684374740 -0400
Change: 2013-09-28 06:20:45.870295668 -0400
Birth: -
CrTime: Sat Sep 28 10:17:04 2013 GMT
CrTime (nsecs): 220441085
$
Run Code Online (Sandbox Code Playgroud)
beg*_*ner 18
在ext4它是可能的; 因为ext4文件系统存储文件创建时间。但是,您仍然会发现该stat命令无法显示日期,因为我认为内核没有任何用于此的 API。
反正文件的出生时间是存进去的ext4,可以查到,虽然不是直接的方法,但是通过使用debugfs
sudo debugfs -R "stat /ABSOLUTE/PATH" /dev/sdxX | grep 时间
Lri*_*Lri 11
在OS X,你可以使用ls -lU,stat -f%B,GetFileInfo -d,或mdls -n kMDItemFSCreationDate:
$ ls -lU
total 0
-rw-r--r-- 1 lauri staff 0 Apr 25 03:58 a
$ stat -f%B a
1398387538
$ stat -f%SB -t %Y%m%d%H%M a
201404250358
$ GetFileInfo -d a
04/25/2014 03:58:58
$ mdls -n kMDItemFSCreationDate a
kMDItemFSCreationDate = 2014-04-25 00:58:58 +0000
Run Code Online (Sandbox Code Playgroud)
Per*_*ulf 10
stat --printf='%w' yourfile #human readable
stat --printf='%W' yourfile #seconds from Epoch , 0 if unknown
Run Code Online (Sandbox Code Playgroud)
FreeBSD和GNU\Linuxon之间的区别stat command:
如果您stat在GNU\Linux其中调用command会调用该-x 选项,但在 FreeBSD 中,您应该自己调用该-x选项。
注释: --printf在scripting....中非常有用!
| 归档时间: |
|
| 查看次数: |
372239 次 |
| 最近记录: |