我正在使用Debian GNU/Linux 6.0上的Bash。是否可以获取文件的创建日期/时间?而不是修改日期/时间。 ls -lh a.txt
和 stat -c%y a.txt
都只提供修改时间。
我正在使用Debian GNU/Linux 6.0上的Bash。是否可以获取文件的创建日期/时间?而不是修改日期/时间。 ls -lh a.txt
和 stat -c%y a.txt
都只提供修改时间。
很遗憾,通常情况下你的请求是不可能实现的。因为根据POSIX标准(请参见Base Definitions section 4.8 File Times Update),每个文件仅存储了三个不同的时间值。
每个文件都有三个不同的关联时间戳:最后一次数据访问的时间、最后一次数据修改的时间以及文件状态最后更改的时间。这些值在文件特性结构struct stat中返回,如<sys/stat.h>所述。
编辑:正如下面评论中提到的,根据使用的文件系统,元数据可能包含文件创建日期。但是需要注意的是,这种信息的存储不是标准化的。依赖于这种存储方式可能会导致在移动到另一个文件系统时出现可移植性问题,因为实际使用的文件系统可能以某种方式存储它。
ls -i file #output is for me 68551981
debugfs -R 'stat <68551981>' /dev/sda3 # /dev/sda3 is the disk on which the file exists
#results - crtime value
[root@loft9156 ~]# debugfs -R 'stat <68551981>' /dev/sda3
debugfs 1.41.12 (17-May-2010)
Inode: 68551981 Type: regular Mode: 0644 Flags: 0x80000
Generation: 769802755 Version: 0x00000000:00000001
User: 0 Group: 0 Size: 38973440
File ACL: 0 Directory ACL: 0
Links: 1 Blockcount: 76128
Fragment: Address: 0 Number: 0 Size: 0
ctime: 0x526931d7:1697cce0 -- Thu Oct 24 16:42:31 2013
atime: 0x52691f4d:7694eda4 -- Thu Oct 24 15:23:25 2013
mtime: 0x526931d7:1697cce0 -- Thu Oct 24 16:42:31 2013
**crtime: 0x52691f4d:7694eda4 -- Thu Oct 24 15:23:25 2013**
Size of extra inode fields: 28
EXTENTS:
(0-511): 352633728-352634239, (512-1023): 352634368-352634879, (1024-2047): 288392192-288393215, (2048-4095): 355803136-355805183, (4096-6143): 357941248-357943295, (6144
-9514): 357961728-357965098
ctime
表示的是目录信息最后一次更新的时间;而mtime
则表示的是文件本身最后一次被修改的时间。 - PJSCopelandmikyra的回答很好,就像他所说的那样。
[jason@rh5 test]$ stat test.txt
File: `test.txt'
Size: 0 Blocks: 8 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 588720 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 500/ jason) Gid: ( 500/ jason)
Access: 2013-03-14 01:58:12.000000000 -0700
Modify: 2013-03-14 01:58:12.000000000 -0700
Change: 2013-03-14 01:58:12.000000000 -0700
如果你想要验证哪个文件是最先创建的,你可以在创建一系列文件时,在文件名后添加系统日期来构造你的文件名。
您可以使用stat查找创建时间,也可以使用find进行匹配。
这些文件显示上次修改时间:
$ ls -l --time-style=long-iso | sort -k6
total 692
-rwxrwx---+ 1 XXXX XXXX 249159 2013-05-31 14:47 Getting Started.pdf
-rwxrwx---+ 1 XXXX XXXX 275799 2013-12-30 21:12 TheScienceofGettingRich.pdf
-rwxrwx---+ 1 XXXX XXXX 25600 2015-05-07 18:52 Thumbs.db
-rwxrwx---+ 1 XXXX XXXX 148051 2015-05-07 18:55 AsAManThinketh.pdf
$ find -newerBt '2014-06-13' ! -newerBt '2014-06-13 12:16:10' -ls
20547673299906851 148 -rwxrwx--- 1 XXXX XXXX 148051 May 7 18:55 ./AsAManThinketh.pdf
1407374883582246 244 -rwxrwx--- 1 XXXX XXXX 249159 May 31 2013 ./Getting\ Started.pdf
$ stat -c "%w %n" * | sort
2014-06-13 12:16:03.873778400 +0100 AsAManThinketh.pdf
2014-06-13 12:16:04.006872500 +0100 Getting Started.pdf
2014-06-13 12:16:29.607075500 +0100 TheScienceofGettingRich.pdf
2015-05-07 18:32:26.938446200 +0100 Thumbs.db
stat命令的man页面解释了%w:
%w time of file birth, human-readable; - if unknown
ls -l
显示的是修改时间,而非创建或出生时间。 - truf$ /usr/bin/find -newerBt '2014-06-13' /usr/bin/find: This system does not provide a way to find the birth time of a file. /usr/bin/find: invalid predicate
-newerBt'` - user907433294490 是指 inode 编号。
然后执行:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg-root 4.0G 3.4G 408M 90% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 124M 27M 92M 23% /boot
/dev/mapper/vg-var 7.9G 1.1G 6.5G 15% /var
要找到根目录“/”文件系统的安装点,因为文件menus.xml在“/”上,即“/dev/mapper/vg-root”上。
输出可能像下面这样:
debugfs 1.41.12 (17-May-2010)
Inode: 94490 Type: regular Mode: 0644 Flags: 0x0
Generation: 2826123170 Version: 0x00000000
User: 0 Group: 0 Size: 4441
File ACL: 0 Directory ACL: 0
Links: 1 Blockcount: 16
Fragment: Address: 0 Number: 0 Size: 0
ctime: 0x5266e438 -- Wed Oct 23 09:46:48 2013
atime: 0x5266e47b -- Wed Oct 23 09:47:55 2013
mtime: 0x5266e438 -- Wed Oct 23 09:46:48 2013
Size of extra inode fields: 4
Extended attributes stored in inode body:
selinux = "unconfined_u:object_r:usr_t:s0\000" (31)
BLOCKS:
(0-1):375818-375819
TOTAL: 2
您可以看到创建时间的位置:
ctime: 0x5266e438 -- Wed Oct 23 09:46:48 2013
如@mikyra所解释的那样,创建日期时间并未存储在任何地方。
以上所有方法都很好,但如果您只想快速获取最后修改日期,可以输入:
ls -lit /path
使用 -t 选项可以列出 /path 目录下按最后修改日期排序的所有文件。
get_crtime() {
for target in "${@}"; do
inode=$(stat -c %i "${target}")
fs=$(df "${target}" | tail -1 | awk '{print $1}')
crtime=$(sudo debugfs -R 'stat <'"${inode}"'>' "${fs}" 2>/dev/null | grep -oP 'crtime.*--\s*\K.*')
printf "%s\t%s\n" "${target}" "${crtime}"
done
}
sudo
和 debugfs
。 - Jesse Chisholm