Saturday, July 19, 2014

Using Stat command to get file info for a script

I was wanting to get the last modified date of a file to use in a OS X shell script.

It seemed that using the 'stat' command was the way to go in order to get details about the file.

MAC OS X Developer Docs on stat command are here or you can 'man stat' on the command line.

Wading my way through the different formats that could be returned I came up with:

Find the last accessed time on a file

stat -f "%Sa" /path/to/file

Jul 19 12:05:26 2014


Find the last modified time on a file

stat -f "%Sm" /path/to/file

Oct  5 08:46:15 2013


Find the creation time on a file. (ie the Birth time of the inode)

stat -f "%SB" /path/to/file

Jul 18 19:21:39 2014

Not to be confused with %Sc which was the last time the inode changed, not the last change time.