get age of given file

OSX keeps track of file creation, but most other unices don't, so there's no way to know the elapsed time since the file creation. You can obtain the elapsed time since its last modification on just about any operating system.

There's no portable shell utility to retrieve a file's modification time, except ls which has output that's nigh-impossible to parse. Under Linux, the following command prints the age of a file:

echo $(($(date +%s) - $(date +%s -r "$filename"))) seconds
echo $((($(date +%s) - $(date +%s -r "$filename")) / 86400)) days

Under Linux, you can use stat -c %Y -- "$filename" as a synonym of date +%s -r "$filename".

OSX's date and stat commands are different. You can use the following command:

echo $(($(date +%s) - $(stat -t %s -f %m -- "$filename"))) seconds

Non-embedded Linux systems and OSX have Perl installed by default.

perl -l -e 'print -M $ARGV[0], " days"' "$filename"

Unix doesn't keep track of a creation date. The only information that's available is typically the last times the files was:

  1. Accessed
  2. Modified
  3. Changed
  • Access - the last time the file was read
  • Modify - the last time the file was modified (content has been modified)
  • Change - the last time meta data of the file was changed (e.g. permissions)

(From this answer)

You can get dates related to a particular file using the stat command.

Example

$ stat ffmpeg 
  File: `ffmpeg'
  Size: 19579304    Blocks: 38248      IO Block: 4096   regular file
Device: fd02h/64770d    Inode: 10356770    Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (  500/    saml)   Gid: (  501/    saml)
Access: 2013-11-26 10:49:09.908261694 -0500
Modify: 2013-11-02 17:05:13.357573854 -0400
Change: 2013-11-02 17:05:13.357573854 -0400

OSX and HFS

If you're using OSX the filesystem that's used under that Unix is HFS. This is one of the few (that I'm aware of) that keeps the creation date within the filesystem, along with modification time etc. similar to other Unixes.

excerpt

A File Record stores a variety of metadata about the file including its CNID, the size of the file, three timestamps (when the file was created, last modified, last backed up), the first file extents of the data and resource forks and pointers to the file's first data and resource extent records in the Extent Overflow File. The File Record also stores two 16 byte fields that are used by the Finder to store attributes about the file including things like its creator code, type code, the window the file should appear in and its location within the window.

Timestamps

Time stamps are always maintained in the filesystem, so you're limited by whatever time tracking is offered through them (EXT3, EXT4, XFS, etc.).

Filesystems

If you're ever curious take a look at this Wikipedia topic titled: Comparison of file systems. It has the most extensive list of filesytems I'm aware of along with a nice table of the various features and the status of whether it's supported or not within a given filesystem.

References

  • How to find creation date of file?
  • How do I do a ls and then sort the results by date created?
  • List files created on Sundays
  • Get file created/creation time?
  • Why does Unix time start at 1970-01-01?

Based on answer by Gilles, here is a bash function that returns file age in seconds or error.

function fileAge
{
    local fileMod
    if fileMod=$(stat -c %Y -- "$1")
    then
        echo $(( $(date +%s) - $fileMod ))
    else
        return $?
    fi
}