Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

NOTE

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.

share|improve this question
    
possible duplicate of How do I do a ls and then sort the results by date created? –  slm Nov 26 '13 at 23:38
    
@slm That's specifically about creation time, it doesn't go into computing the age on the command line. –  Gilles Nov 27 '13 at 0:14
    
Duplicate of How to find creation date of file?. –  terdon Nov 27 '13 at 0:56
    
@terdon, not really. See my NOTE above. –  Alexej Magura Nov 27 '13 at 16:33
    
@AlexejMagura and how do the answers in the dupe not do for you? stat gives you all you need right? –  terdon Nov 27 '13 at 17:18

3 Answers 3

up vote 7 down vote accepted

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

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

share|improve this answer
    
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"
share|improve this answer
    
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
    
@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 Nov 27 '13 at 10:16

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
1402340803
$ 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))
10
share|improve this answer

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.