相关文章推荐
阳刚的枕头  ·  C++11 的 stoi() - 知乎·  1 年前    · 
瘦瘦的柚子  ·  python - TypeError: ...·  1 年前    · 

在linux上用Python获取文件创建时间

22 人关注

os.stat返回st_mtime和st_ctime属性,修改时间是st_mtime,st_ctime是POSIX的 "改变时间"。 是否有任何函数可以在Linux下使用python返回文件的创建时间?

2 个评论
CSJ
看起来这取决于你使用什么文件系统。 unix.stackexchange.com/questions/7562/...
python
linux
Ali Mezgani
Ali Mezgani
发布于 2009-09-11
7 个回答
SingleNegationElimination
SingleNegationElimination
发布于 2018-02-25
已采纳
0 人赞同

你可能 不能。 :

3.1)  How do I find the creation time of a file?
      You can't - it isn't stored anywhere.  Files have a last-modified
      time (shown by "ls -l"), a last-accessed time (shown by "ls -lu")
      and an inode change time (shown by "ls -lc"). The latter is often
      referred to as the "creation time" - even in some man pages -
      but that's wrong; it's also set by such operations as mv, ln,
      chmod, chown and chgrp.
      The man page for "stat(2)" discusses this.
    
So is there a command to find the time & date of creation of a file in Linux If stat does not ?
一般来说,UNIX文件系统根本不存储创建时间--没有任何方法可以检索那些一开始就没有写入磁盘的数据。
现在大多数较新的FS都支持创建/出生时间。 截至2019年3月,Linux内核、glibc和coreutils支持 statx() crtime
Jon
Jon
发布于 2018-02-25
0 人赞同
st_birthtime

但它并不保证在所有系统上都可用。 从文档中可以看出。

在一些Unix系统上(如Linux)。 以下属性也可能是 可用:st_blocks (分配给文件的块的数量) 分配给文件的块数),st_blksize (文件系统的块大小), st_rdev (如果是inode设备,设备类型) 设备的类型,如果是一个inode设备)。 st_flags (用户为文件定义的标志)

在其他Unix系统(如 FreeBSD),以下属性可能 是可用的(但可能只有在root试图使用它们时才会填写 试图使用它们):st_gen (文件生成号), st_birthtime (文件创建的时间)。

http://docs.python.org/2/library/os.html#os.stat

PascalVKooten
PascalVKooten
发布于 2018-02-25
0 人赞同

由于缺乏一个好的工具,我已经创建了 crtime .

pip install crtime

然后你可以像这样使用它。

sudo crtime ./

Would print:

1552938281  /home/pascal/crtime/.gitignore
1552938281  /home/pascal/crtime/README.md
1552938281  /home/pascal/crtime/crtime
1552938281  /home/pascal/crtime/deploy.py
1552938281  /home/pascal/crtime/setup.cfg
1552938281  /home/pascal/crtime/setup.py
1552938961  /home/pascal/crtime/crtime.egg-info
1552939447  /home/pascal/crtime/.git
1552939540  /home/pascal/crtime/build
1552939540  /home/pascal/crtime/dist

注意,对于大的目录,它将比上面的xstat轻松地快1000倍,因为这将创建一个临时文件,然后一次性执行所有文件的stat调用。

在python中(别忘了你在linux上还需要用sudo调用它)。

from crtime import get_crtimes, get_crtimes_in_dir
get_crtimes_in_dir("./")
    
Padraic Cunningham
Padraic Cunningham
发布于 2018-02-25
0 人赞同

根据一个主题 here OS X的HFS和微软的NTFS也都跟踪出生时间,我听说OS X和Cygwin版本的stat()都会返回这个信息。 其中看了一下 osx statage manpage 至少对Mac来说,似乎是正确的。

a, m, c, B

文件最后一次被访问或修改的时间,即最后一次改变inode的时间,或 birth time of the inode .

对于Linux来说,较新的文件系统,如ext4、Btrfs和JFS,都支持使用 debugfs ,有一个bash函数取自 here 这将提取日期创建的时间戳。

如果你处理有能力的文件系统,如EXT4--Linux的日志文件系统,你可以恢复文件创建日期。

改进的时间戳

...Ext4提供以纳秒为单位的时间戳。此外,Ext4还增加了对日期创建的时间戳的支持。 但在社区中没有达成共识,所以

......正如Theodore Ts'o所指出的,虽然在inode中增加一个额外的创建日期字段很容易(从而在技术上实现对ext4中的日期创建时间戳的支持),但修改或增加必要的系统调用,如stat()(可能需要一个新版本)和依赖它们的各种库(如glibc)则比较困难。这些改变需要许多项目的协调。因此,即使ext4的开发者实现了对创建日期时间戳的初步支持,这项功能暂时也不会被用户程序使用。 这就是Linus最后说的

让我们等待五年,看看是否真的有任何关于需要和使用它的共识,而不是仅仅因为 "我们可以 "而匆忙行事。

xstat() {
  for target in "${@}"; do
    inode=$(ls -di "${target}" | cut -d ' ' -f 1)
    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" "${crtime}" "${target}"

运行它返回创建日期。

:~$ echo 'print("hello world")' > blah.py
:~$ xstat "blah.py"
Mon Jul  6 13:43:39 2015    blah.py
:~$ echo 'print("goodbye world")' > blah.py
:~$ xstat "blah.py"
Mon Jul  6 13:43:39 2015    blah.py

所以,除非文件系统支持,否则是不可能的,如果文件系统支持,那么你可以使用子进程运行debugfs并解析输出。

Basile Starynkevitch
Basile Starynkevitch
发布于 2018-02-25
0 人赞同

你可以解释你为什么要这样做。

一个间接的解决方案可能是使用一些 revision control 系统(又称版本控制系统=VCS)来管理其出生时间需要的文件。

So you could use git 在这些文件上(即把它们当作 "源代码 "来处理)。然后你不仅知道它们何时被创建(实际上是用 git add 在VCS中注册),而且知道为什么,由谁创建,为了什么,等等。使用 git log 来获得所有这些...

当然,你需要以某种方式教育你的用户使用像 git 这样的VCS。

Fletch Hasues
Fletch Hasues
发布于 2018-02-25
0 人赞同

某些文件系统确实支持记录出生时间,但Linux没有提供获取时间的接口。

See http://lwn.net/Articles/397442/

如果人们试图使用 "stat "命令来获取它。 % stat -c %w {file or dir}.

结果将是一个"-",因为它没有能力检索它。 然而,人们可以使用这个样本方法,利用debugfs和xstat来检索它(前提是所使用的文件系统支持收集它)。

https://gist.github.com/moiseevigor/8c496f632137605b322e

xstat() {
  for target in "${@}"; do
    inode=$(ls -di "${target}" | cut -d ' ' -f 1)
    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" "${crtime}" "${target}"

Note this requires sudo.

自2019年3月起,Linux现在通过内核、glibc和coreutils中的 statx() 支持它。
Tom Grushka
Tom Grushka
发布于 2018-02-25
0 人赞同

什么叫不能做[1]?的功能。

os.stat(path).st_birthtime
, works great. Somebody said that it couldn’t be done But he with a chuckle replied That “maybe it couldn’t,” but he would be one Who wouldn’t say so till he’d tried. So he buckled right in with the trace of a grin