bash OSX - 如何从命令行获取文件的创建和修改时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34123076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:08:38  来源:igfitidea点击:

OSX - How to get the creation & modification time of a file from the command line

macosbash

提问by Mr_Pouet

While solving some programming puzzle, I wanted to see how long it took me to write a solution to the problem. To do so, I thought it'd be a good idea to compare the file creation date with the latest modification date.

在解决一些编程难题的同时,我想看看我写了一个问题的解决方案花了多长时间。为此,我认为将文件创建日期与最新修改日期进行比较是个好主意。

In the terminal (OSX), I tried the following command and was surprised to see the same date three times in a row:

在终端 (OSX) 中,我尝试了以下命令,并惊讶地看到连续 3 次出现相同的日期:

stat my_file.py
16777220 10280844 -rw-r--r-- 1 username staff 0 7214 \
"Dec  5 08:32:39 2015"  \
"Dec  5 08:32:39 2015"  \
"Dec  5 08:32:39 2015"  \
"Dec  5 08:32:39 2015" 4096 16 0 my_file.py

The way I created then modified the file:

我创建的方式然后修改了文件:

touch my_file.py
vim my_file.py   # <- modify some content
stat my_file.py

Any idea on how to get these two dates from the command line?

关于如何从命令行获取这两个日期的任何想法?

Clarification: I do not want to time the execution time of the script.

澄清:我不想为脚本的执行时间计时。

EDIT: The issue was with vim changing the creation date on save, the accepted answer still answers the question in depth for those who are interested.

编辑:问题在于vim在保存时更改了创建日期,接受的答案仍然为感兴趣的人深入回答了这个问题。

回答by blm

statreports the standard Unix dates, last access time, last modification time, and inode change time (which is often mistaken for creation time). Mac OS X also maintains the file creation time, and it's accessible using the GetFileInfocommand:

stat报告标准 Unix 日期、上次访问时间、上次修改时间和 inode 更改时间(经常被误认为是创建时间)。Mac OS X 还维护文件创建时间,可以使用以下GetFileInfo命令访问:

$ GetFileInfo -d .bash_profile
10/08/2015 09:26:35

Here's a more complete example:

这是一个更完整的示例:

$ ls -l my_file.py
ls: my_file.py: No such file or directory
$ touch my_file.py
$ stat -x my_file.py
  File: "my_file.py"
  Size: 0            FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/     blm)  Gid: (   20/   staff)
Device: 1,5   Inode: 26863832    Links: 1
Access: Sun Dec  6 13:47:24 2015
Modify: Sun Dec  6 13:47:24 2015
Change: Sun Dec  6 13:47:24 2015
$ GetFileInfo my_file.py
file: "/Users/blm/my_file.py"
type: "
stat -f "Access (atime): %Sa%nModify (mtime): %Sm%nChange (ctime): %Sc%nBirth  (Btime): %SB" file.txt

Access (atime): Nov 16 19:44:55 2017
Modify (mtime): Nov 16 19:44:25 2017
Change (ctime): Nov 16 19:44:48 2017
Birth  (Btime): Nov 16 19:44:05 2017
##代码####代码####代码##" creator: "##代码####代码####代码####代码##" attributes: avbstclinmedz created: 12/06/2015 13:47:24 modified: 12/06/2015 13:47:24 $ echo hello >my_file.py $ stat -x my_file.py File: "my_file.py" Size: 6 FileType: Regular File Mode: (0644/-rw-r--r--) Uid: ( 501/ blm) Gid: ( 20/ staff) Device: 1,5 Inode: 26863832 Links: 1 Access: Sun Dec 6 13:47:24 2015 Modify: Sun Dec 6 13:47:35 2015 Change: Sun Dec 6 13:47:35 2015 $ GetFileInfo my_file.py file: "/Users/blm/my_file.py" type: "##代码####代码####代码####代码##" creator: "##代码####代码####代码####代码##" attributes: avbstclinmedz created: 12/06/2015 13:47:24 modified: 12/06/2015 13:47:35 $ cat my_file.py hello $ stat -x my_file.py File: "my_file.py" Size: 6 FileType: Regular File Mode: (0644/-rw-r--r--) Uid: ( 501/ blm) Gid: ( 20/ staff) Device: 1,5 Inode: 26863832 Links: 1 Access: Sun Dec 6 13:47:54 2015 Modify: Sun Dec 6 13:47:35 2015 Change: Sun Dec 6 13:47:35 2015 $ GetFileInfo my_file.py file: "/Users/blm/my_file.py" type: "##代码####代码####代码####代码##" creator: "##代码####代码####代码####代码##" attributes: avbstclinmedz created: 12/06/2015 13:47:24 modified: 12/06/2015 13:47:35

Note that using vimto test this may be misleading because vimwill write your modified file to a new temporary file, then rename the old one and the new one, so the creation time will be updated to when the file was written. See this postfor a workaround I came up with for that.

请注意,使用vim来测试这可能会产生误导,因为vim会将您修改的文件写入一个新的临时文件,然后重命名旧文件和新文件,因此创建时间将更新为写入文件的时间。请参阅这篇文章,了解我为此提出的解决方法。

回答by wisbucky

As you already identified, the real culprit was that vimresets all 4 datetime stamps.

正如您已经确定的,真正的罪魁祸首是vim重置所有 4 个日期时间戳。

But to answer your original question, here is a statformatting for Mac OSX that will clearly show the 4 datetime stamps (including Creation/Birth and Modify):

但是为了回答您的原始问题,这里是statMac OSX的格式,它将清楚地显示 4 个日期时间戳(包括创建/出生和修改):

##代码##