How can I get the age of a given file in, at least, days?

I'm well aware of ls -lh and similar commands. I want something that will work sort of like this:

getfage <FILE> # prints out '12d' (12 days)

Also, this needs to be somewhat cross-platform since I'd also like to use this under Mac OS X, but the primary use-case is on my Linux-box.


Since Linux doesn't track creation time, I'm looking for two-fold solution: one for mtime (linux)--that is the last time said file was modified--and one for Mac OS X, which can either deal with mtime or creation time.


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.


$ 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


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.


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.


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


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.


  • Thanks for the update, that helps. Actually, I read here that ext4 can keep creation dates but this has not been implemented in the kernel yet. – terdon Nov 27 '13 at 2:26
  • @terdon - yeah it makes sense that some filesystems would support it. It seems long overdue to me, I understand in the past why it was left out, but everything has advanced so much since the beginning that it doesn't make sense to explicitly not have this. Also you can disable the support for access time so it could be implemented similarly and then disabled if you really didn't want it. – slm Nov 27 '13 at 2:35

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"
  • Does this depend on the OS or the filesystem? This Q&A seems to imply that the filesystem keeps them but the kernel needs to provide a method for accessing them. – terdon Nov 27 '13 at 0:55
  • @terdon - see my updates. – slm Nov 27 '13 at 2:18
  • @terdon Both. That Q&A you link is correct, current filesystems have been tracking the creation time for a while now. In FreeBSD, you can actually get at this from stat, in Linux the syscall has not been reworked so you have to resort to hacking with debugfs. – wingedsubmariner Nov 27 '13 at 4:20
  • 1
    @wingedsubmariner With the caveat that the creation time in the filesystem is often not the creation date in the application, as many applications use create-new-then-move as the method to save a new version rather than overwrite-in-place. – Gilles 'SO- stop being evil' Nov 27 '13 at 10:16

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")
        echo $(( $(date +%s) - $fileMod ))
        return $?
  • 1
    Neat function , easy to adapt by adding simple division to see age in Hour, Days, week .... – Emmanuel Devaux Nov 19 '18 at 18:35
stat -c %Y <filename>

look at man stat for -c (format) options


You can use stat -f%B, ls -lU, or mdls -n kMDItemFSCreationDate to see the creation (birth) time in OS X:

$ stat -f%B file
$ ls -lU file
-rw-r--r--  1 admin  staff  108514 Jun  9 22:06 file
$ mdls -n kMDItemFSCreationDate file
kMDItemFSCreationDate = 2014-06-09 19:06:43 +0000
$ echo $((($(date +%s)-$(stat -f%B file))/86400))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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