简体   繁体   English

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

[英]Get file creation time with Python on linux

os.stat returns st_mtime and st_ctime attributes, the modification time is st_mtime and st_ctime "change time" on POSIX. os.stat 返回 st_mtime 和 st_ctime 属性,修改时间在 POSIX 上为 st_mtime 和 st_ctime “更改时间”。 is there any function that return the creation time of a file using python and under Linux?是否有任何函数可以使用 python 和在 Linux 下返回文件的创建时间?

You probably can't. 你可能不能。 :

 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.

try:尝试:

st_birthtime

It isnt' guaranteed to be available on all systems though.但不能保证在所有系统上都可用。 From the docs:从文档:

On some Unix systems (such as Linux), the following attributes may also be available: st_blocks (number of blocks allocated for file), st_blksize (filesystem blocksize), st_rdev (type of device if an inode device).在某些 Unix 系统(例如 Linux)上,以下属性也可用:st_blocks(为文件分配的块数)、st_blksize(文件系统块大小)、st_rdev(设备类型,如果是 inode 设备)。 st_flags (user defined flags for file). st_flags(用户定义的文件标志)。

On other Unix systems (such as FreeBSD), the following attributes may be available (but may be only filled out if root tries to use them): st_gen (file generation number), st_birthtime (time of file creation).在其他 Unix 系统(例如 FreeBSD)上,以下属性可能可用(但只有在 root 尝试使用它们时才可能填写):st_gen(文件生成编号)、st_birthtime(文件创建时间)。

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

By lack of a good utility, I've created crtime .由于缺乏好的实用程序,我创建了crtime

pip install crtime

Then you can use it like:然后你可以像这样使用它:

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

Note that for large directories it will be easily 1000x faster than xstat above, as this creates a temporary file and then executes stat calls for all files at once.请注意,对于大目录,它会比上面的xstat快 1000 倍,因为这会创建一个临时文件,然后一次对所有文件执行stat调用。

In python (don't forget you need to still call it with sudo on linux):在 python 中(不要忘记你仍然需要在 linux 上用 sudo 调用它):

from crtime import get_crtimes, get_crtimes_in_dir
get_crtimes_in_dir("./")

According to a thread here OS X's HFS and Microsoft's NTFS also both track the birth time, and I'm told the OS X and Cygwin versions of stat() return this information.根据这里的一个线程 OS X 的 HFS 和 Microsoft 的 NTFS 也都跟踪出生时间,我被告知 OS X 和 Cygwin 版本的 stat() 返回此信息。 which looking at the osx stat manpage seems correct at least for mac:至少对于 mac,查看osx stat 联机帮助页似乎是正确的:

a, m, c, B甲、米、丙、乙

The time file was last accessed or modified, of when the inode was last changed, or the birth time of the inode .上次访问或修改文件的时间、上次更改 inode 的时间inode出生时间

For linux newer filesystems like ext4, Btrfs and JFS do support this using debugfs , there is a bash function taken from here that will extract the date-created timestamp:对于像EXT4 Linux的文件系统更新,增加了Btrfs和JFS不支持这种使用debugfs ,有取自一个bash的功能在这里,将提取的日期创建的时间戳:

You may recover the file creation date if you deal with capable filesystem like EXT4 - journaling file system for Linux:如果你处理像 EXT4 这样的文件系统——Linux 的日志文件系统,你可以恢复文件创建日期:

Improved timestamps改进的时间戳

... Ext4 provides timestamps measured in nanoseconds. ... Ext4 提供以纳秒为单位的时间戳。 In addition, ext4 also adds support for date-created timestamps.此外,ext4 还增加了对日期创建时间戳的支持。 But there no consensus in the community on that so但是社区对此没有达成共识,所以

... as Theodore Ts'o points out, while it is easy to add an extra creation-date field in the inode (thus technically enabling support for date-created timestamps in ext4), it is more difficult to modify or add the necessary system calls, like stat() (which would probably require a new version) and the various libraries that depend on them (like glibc). ...正如 Theodore Ts'o 指出的那样,虽然在 inode 中添加额外的创建日期字段很容易(从而在技术上支持 ext4 中的日期创建时间戳),但更难修改或添加必要的系统调用,如 stat()(可能需要新版本)和依赖它们的各种库(如 glibc)。 These changes would require coordination of many projects.这些变化需要许多项目的协调。 So even if ext4 developers implement initial support for creation-date timestamps, this feature will not be available to user programs for now.因此,即使 ext4 开发人员实现了对创建日期时间戳的初始支持,此功能现在也不会对用户程序可用。 Which end up with the Linus final quote最终得到 Linus 的最终报价

Let's wait five years and see if there is actually any consensus on it being needed and used at all, rather than rush into something just because "we can".让我们等五年,看看是否真的就需要和使用它达成了任何共识,而不是仅仅因为“我们可以”就急于求成。

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}"
  done
}

Running it returns the creation date:运行它返回创建日期:

:~$ 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

So unless the file system supports it then it is not possible, if the file system does then you could run the debugfs using subprocess and parse the output.因此,除非文件系统支持它,否则这是不可能的,如果文件系统支持,那么您可以使用子debugfs运行debugfs并解析输出。

You might explain why you want to do this.您可能会解释为什么要这样做。

An indirect solution might be to use some revision control system (aka version control system = VCS) to manage the files whose birth time is needed.一个间接的解决方案可能是使用一些修订控制系统(又名版本控制系统 = VCS)来管理需要出生时间的文件。

So you could use git on such files (ie handle them as "source code").所以你可以在这样的文件上使用git (即将它们作为“源代码”处理)。 Then you know not only when they have been created (in fact registered in the VCS using git add ), but why, by whom, what for, etc... Use git log to get all this...然后你不仅知道它们是何时创建的(实际上是使用git add在 VCS 中注册的),而且知道为什么、由谁、为了什么等等......使用git log来获取所有这些......

Of course you somehow need to educate your users to use a VCS like git当然,您需要以某种方式教育您的用户使用像git这样的 VCS

Certain file systems do support recording the birth time, but Linux does not provide an interface for obtaining it.某些文件系统确实支持记录出生时间,但 Linux 不提供获取它的接口。

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

If one atempts to use the "stat" command to obtain it: % stat -c %w {file or dir}如果尝试使用“stat”命令来获取它: % stat -c %w {file or dir}

The result will be a "-" due to it not having the ability to retrieving it.结果将是“-”,因为它无法检索它。 However, one can use this sample method utilizing debugfs with xstat to retrieve it (providing again, that the file system being used supports collecting it.)但是,可以使用此示例方法利用 debugfs 和 xstat 来检索它(再次提供,所使用的文件系统支持收集它。)

https://gist.github.com/moiseevigor/8c496f632137605b322e 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}"
  done
}

Note this requires sudo.请注意,这需要 sudo。

What do you mean it can't be done [1]?你是什​​么意思它不能完成[1]? The function,功能,

os.stat(path).st_birthtime
, works great. ,效果很好。

\n[1]: [1]:\nSomebody said that it couldn't be done有人说做不到\n      But he with a chuckle replied但他笑着回答\nThat “maybe it couldn't,” but he would be one那个“也许不能”,但他会是一个\n      Who wouldn't say so till he'd tried.在他尝试之前,谁不会这么说。\nSo he buckled right in with the trace of a grin所以他带着一丝笑容直接扣了进去\n      On his face.在他的脸上。 If he worried he hid it.如果他担心,他就会隐藏起来。\nHe started to sing as he tackled the thing当他处理这件事时,他开始唱歌\n      That couldn't be done, and he did it!那是不可能的,而他做到了!\n-- Edgar Albert Guest ——埃德加·阿尔伯特·客人\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM