How to get file creation date/time in Bash/Debian?

后端 未结 12 1640
有刺的猬
有刺的猬 2020-11-28 23:00

I\'m using Bash on Debian GNU/Linux 6.0. Is it possible to get the file creation date/time? Not the modification date/time. ls -lh a.txt and stat -c %y a.

相关标签:
12条回答
  • 2020-11-28 23:34

    If you really want to achieve that you can use a file watcher like inotifywait.

    You watch a directory and you save information about file creations in separate file outside that directory.

    while true; do
      change=$(inotifywait -e close_write,moved_to,create .)
      change=${change#./ * }
      if [ "$change" = ".*" ]; then ./scriptToStoreInfoAboutFile; fi
    done
    

    As no creation time is stored, you can build your own system based on inotify.

    0 讨论(0)
  • 2020-11-28 23:39

    You can find creation time - aka birth time - using stat and also match using find.
    We have these files showing last modified time:

    $ ls -l --time-style=long-iso | sort -k6
    total 692
    -rwxrwx---+ 1 XXXX XXXX 249159 2013-05-31 14:47 Getting Started.pdf
    -rwxrwx---+ 1 XXXX XXXX 275799 2013-12-30 21:12 TheScienceofGettingRich.pdf
    -rwxrwx---+ 1 XXXX XXXX  25600 2015-05-07 18:52 Thumbs.db
    -rwxrwx---+ 1 XXXX XXXX 148051 2015-05-07 18:55 AsAManThinketh.pdf
    

    To find files created within a certain time frame using find as below.
    Clearly, the filesystem knows about the birth time of a file:

    $ find -newerBt '2014-06-13' ! -newerBt '2014-06-13 12:16:10' -ls 
    20547673299906851  148 -rwxrwx---   1 XXXX XXXX   148051 May  7 18:55 ./AsAManThinketh.pdf
    1407374883582246  244 -rwxrwx---   1 XXXX XXXX   249159 May 31  2013 ./Getting\ Started.pdf
    


    We can confirm this using stat:

    $ stat -c "%w %n" * | sort
    2014-06-13 12:16:03.873778400 +0100 AsAManThinketh.pdf
    2014-06-13 12:16:04.006872500 +0100 Getting Started.pdf
    2014-06-13 12:16:29.607075500 +0100 TheScienceofGettingRich.pdf
    2015-05-07 18:32:26.938446200 +0100 Thumbs.db
    


    stat man pages explains %w:

    %w     time of file birth, human-readable; - if unknown
    
    0 讨论(0)
  • 2020-11-28 23:39

    Another trick to add to your arsenal is the following:

    $ grep -r "Copyright" /<path-to-source-files>/src
    

    Generally speaking, if one changes a file they should claim credit in the “Copyright”. Examine the results for dates, file names, contributors and contact email.

    example grep result:

    /<path>/src/someobject.h: * Copyright 2007-2012 <creator's name> <creator's email>(at)<some URL>>
    
    0 讨论(0)
  • 2020-11-28 23:40

    Cited from https://unix.stackexchange.com/questions/50177/birth-is-empty-on-ext4/131347#131347 , the following shellscript would work to get creation time:

    get_crtime() {
       for target in "${@}"; do
           inode=$(stat -c %i "${target}")
           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" "${target}" "${crtime}"
       done
    }
    
    0 讨论(0)
  • 2020-11-28 23:42
    ls -i file #output is for me 68551981
    debugfs -R 'stat <68551981>' /dev/sda3 # /dev/sda3 is the disk on which the file exists
    
    #results - crtime value
    [root@loft9156 ~]# debugfs -R 'stat <68551981>' /dev/sda3
    debugfs 1.41.12 (17-May-2010)
    Inode: 68551981   Type: regular    Mode:  0644   Flags: 0x80000
    Generation: 769802755    Version: 0x00000000:00000001
    User:     0   Group:     0   Size: 38973440
    File ACL: 0    Directory ACL: 0
    Links: 1   Blockcount: 76128
    Fragment:  Address: 0    Number: 0    Size: 0
     ctime: 0x526931d7:1697cce0 -- Thu Oct 24 16:42:31 2013
     atime: 0x52691f4d:7694eda4 -- Thu Oct 24 15:23:25 2013
     mtime: 0x526931d7:1697cce0 -- Thu Oct 24 16:42:31 2013
    **crtime: 0x52691f4d:7694eda4 -- Thu Oct 24 15:23:25 2013**
    Size of extra inode fields: 28
    EXTENTS:
    (0-511): 352633728-352634239, (512-1023): 352634368-352634879, (1024-2047): 288392192-288393215, (2048-4095): 355803136-355805183, (4096-6143): 357941248-357943295, (6144
    -9514): 357961728-357965098
    
    0 讨论(0)
  • 2020-11-28 23:42

    As @mikyra explained, creation date time is not stored anywhere.

    All the methods above are nice, but if you want to quickly get only last modify date, you can type:

    ls -lit /path
    

    with -t option you list all file in /path odered by last modify date.

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