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

后端 未结 2 1498
走了就别回头了
走了就别回头了 2020-12-23 20:48

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 c

相关标签:
2条回答
  • 2020-12-23 21:08

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

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

    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
    
    0 讨论(0)
  • 2020-12-23 21:16

    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
    

    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.

    0 讨论(0)
提交回复
热议问题