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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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"
Method 2
Unix doesn’t keep track of a creation date. The only information that’s available is typically the last times the files was:
- Accessed
- Modified
- 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)
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?
Method 3
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
}
Method 4
stat -c %Y <filename>
look at man stat for -c (format) options
Method 5
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
Method 6
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0