Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Given

  • a directory
  • a sh pattern which yields a subset of files directly in this directory (like *.log)
  • a sh pattern which can, given a filename,

what is the fastest way (in ksh) to obtain for each file filtered in by the pattern:

  • its name
  • the date and time of its last modification (i.e. text appended)
  • the date and time where it was created (supposing it was created in the directory were it is when we access it)

Ideally, it would work on both:

  • AIX 6100-04-03-1009
  • Linux 2.6.18
  • SunOS 5.10
share|improve this question
2  
Creation time doesn't exist in POSIX/SUS. –  Ignacio Vazquez-Abrams Jul 11 '12 at 15:58
add comment

2 Answers

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|improve this answer
add comment

Apologies for not writing the awk/sed for you, but the stat command will provide accessed, modified, and changed times. It will glob for you. Im not sure if it works in AIX, I have no access to that presently.

share|improve this answer
    
"Changed" and "modified", are they not synonyms? –  Pierre Gardin Jul 11 '12 at 16:10
    
If the files permissions, ownership, gets changed, then that will update the ctime, but not the mtime. –  Sirch Jul 11 '12 at 16:12
    
And I just logged into AIX, im afraid stat wasnt there... sorry, im an AIX smitty noob. –  Sirch Jul 11 '12 at 16:14
    
"metadata change time" –  Ignacio Vazquez-Abrams Jul 11 '12 at 16:17
    
"inode" change time. –  Sirch Jul 11 '12 at 16:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.