Fastest way to get creation and last modification times of a lot of files

119

A Posix file system node typically has three time attributes:

  • atime (access time) -- when was the file last read
  • mtime (modification time) -- when was it last written to.
  • ctime (change time) -- when was its inode (metadata) changed.

The ctime attribute is frequently misunderstood as creation time, and sometimes it is, which tends to confuse people.

POSIX shells have no standard way of extracting these three attributes, and you will depend on the ls command. ls -l $file by default shows modification time.

  • ls -lc $file shows ctime
  • ls -lu $file shows atime

It is recommended to use ls --time-style=full-iso or another iso format for consistent output, if you are on a GNU/linux system.

In Perl and other scripting languages, it's easier to stat() a file and access its attributes. Perl even has the -M, -A, and -C operators that return mtime, atime, and ctime for a file system object. Note the time offset, though. Perl tends to report times relative to the process start time.

Share:
119

Related videos on Youtube

EranSch
Author by

EranSch

Updated on September 18, 2022

Comments

  • EranSch
    EranSch over 1 year

    I've been having trouble finding references to this concept in the documentation so I figured I'd ask here. We're working with some boolean query logic and I've been working on building a $match stage with something like the following:

    { '$match': 
     { '$or': 
        [ { depth: { '$or': [ { '$gt': 1 }, { '$lte': 36 }, { '$ne': 15 } ] } },
          { height: { '$and': [ { '$gt': 1 }, { '$lte': 36 }, { '$ne': 15 } ] } } ] } },
    

    Can anyone confirm if this type of nested boolean logic is supported?

    In my testing, it doesn't return any results but it also doesn't raise any exceptions.

    Thanks!

    • Ignacio Vazquez-Abrams
      Ignacio Vazquez-Abrams almost 12 years
      Creation time doesn't exist in POSIX/SUS.
  • asdasdasd
    asdasdasd almost 12 years
    "Changed" and "modified", are they not synonyms?
  • Sirch
    Sirch almost 12 years
    If the files permissions, ownership, gets changed, then that will update the ctime, but not the mtime.
  • Sirch
    Sirch almost 12 years
    And I just logged into AIX, im afraid stat wasnt there... sorry, im an AIX smitty noob.
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams almost 12 years
    "metadata change time"
  • Sirch
    Sirch almost 12 years
    "inode" change time.
  • Adam Luchjenbroers
    Adam Luchjenbroers about 8 years
    If on AIX, check for istat instead.
  • EranSch
    EranSch almost 7 years
    Thanks for the reply. I don't think this completely achieves my goal, however, as I'm hoping to be able to combine booleans. I've updated my example to hopefully better illustrate my needs.
  • EranSch
    EranSch almost 7 years
    Ahhh that makes perfect sense. Thank you for clarifying! Marking this as the accepted answer.