OSX-如何进行创建和创建从命令行修改文件的时间 [英] OSX - How to get the creation & modification time of a file from the command line
问题描述
在解决一些编程难题时,我想看看我花了多长时间编写了解决该问题的方法.为此,我认为最好将文件创建日期与最新修改日期进行比较.
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.
在终端(OSX)中,我尝试了以下命令,但很惊讶连续三天看到同一日期:
In the terminal (OSX), I tried the following command and was surprised to see the same date three times in a row:
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?
说明:我不想计时脚本的执行时间.
编辑:问题是vim在保存时更改了创建日期,接受的答案仍对有兴趣的人深入回答了该问题.
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.
推荐答案
stat
报告标准Unix日期,上次访问时间,上次修改时间和inode更改时间(通常将其误认为是创建时间). Mac OS X还可以维护文件创建时间,可以使用GetFileInfo
命令进行访问:
stat
reports 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 GetFileInfo
command:
$ 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: "\0\0\0\0"
creator: "\0\0\0\0"
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: "\0\0\0\0"
creator: "\0\0\0\0"
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: "\0\0\0\0"
creator: "\0\0\0\0"
attributes: avbstclinmedz
created: 12/06/2015 13:47:24
modified: 12/06/2015 13:47:35
请注意,使用vim
进行测试可能会产生误导,因为vim
会将修改后的文件写入新的临时文件,然后重命名旧文件和新文件,因此创建时间将更新为文件已写入.有关我想出的解决方法,请参见这篇文章为此.
Note that using vim
to test this may be misleading because vim
will 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 post for a workaround I came up with for that.
这篇关于OSX-如何进行创建和创建从命令行修改文件的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!