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.


8 Answers 8


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
echo $((($(date +%s) - $(stat -t %s -f %m -- "$filename")) / 86400)) days

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

perl -l -e 'print -M $ARGV[0], " days"' "$filename"
perl -l -e 'print 86400 * -M $ARGV[0], " seconds"' "$filename"
perl -l -e '$mtime = (stat($ARGV[0]))[9]; print time - $mtime, " seconds"' -- "$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, 2013 at 0:55
  • @terdon - see my updates.
    – slm
    Nov 27, 2013 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. Nov 27, 2013 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. Nov 27, 2013 at 10:16
  • First example echo command which gives file age in seconds worked, but the other variant (third echo command) didn't work for me under Ubuntu 20 in a Bash shell script.
    – Jan
    Aug 9, 2021 at 12:25

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, 2013 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, 2013 at 2:35
  • As of now (2023), running 'stat' on a file stored on an ext4 filesystem (kernel 5.15) prints a 'Birth' date. Mar 28, 2023 at 6:26

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 $?
  • 2
    Neat function , easy to adapt by adding simple division to see age in Hour, Days, week .... Nov 19, 2018 at 18:35
stat -c %Y <filename>

look at man stat for -c (format) options


If you just want output as 12d or 12 days I use this...

now=$(date +%s)
modified=$(date -r "$filename" "+%s")
printf 'Created %d days ago\n' $((delta/86400))
printf 'Created %dh:%dm:%ds ago\n' $((delta/3600)) $((delta%3600/60)) $((delta%60))

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))

For standard Unix, stat -c %Y as seen in other answers gets you the 'last modified' time. But for some circumstances you might want the 'inode change time' - this is when the metadata last changed. It gets updated when you first make the file and then when you do things like chmod (which, most of the time, you won't be doing - so in many circumstances it stays as the original creation time). That can be output with stat -c %Z.

  • There's no stat command in the UNIX standard. Serveral Unices have a stat command. The zsh shell has a stat builtin. But all have completely different APIs. The stat implementation that understands -c %Y is the GNU own (GNU standing for GNU's Not UNIX), though busybox also added a stat applet with a similar API Nov 11, 2022 at 8:02

Came across this today.

The Linux stat command now seems to have a birth date:

    $ stat myfile.ext
  File: myfile.ext
  Size: 4091        Blocks: 8          IO Block: 4096   regular file
Device: 0,38    Inode: 5476021     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/chandana)   Gid: ( 1000/chandana)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2022-11-11 14:07:03.744610297 +1100
Modify: 2022-11-11 14:06:45.516562357 +1100
Change: 2022-11-11 14:06:45.519562365 +1100
 Birth: 2022-11-11 14:06:45.516562357 +1100
  • As mentioned in the accepted answer, the stat command is only half of the issue, the filesystem must also support storing the relevant metadata. You don't mention what filesystem you use, nor what Linux you are using. BusyBox stat on e.g. Alpine Linux does not mention birth date, for example.
    – Kusalananda
    Nov 11, 2022 at 7:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .