Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'd like to see the age of a file (time since last modification) in some human-friendly units (bonus points for things like "yesterday", "2 days ago", "3 years ago", although just a number of days would be sufficient).

Is there a shell one-liner that's simple enough to memorize and type on demand? Is there a tool (packaged for Debian/Ubuntu)? Do I write a custom shell script to do some arithmetic and install it on all the servers I have?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

This will display a file's age in days:

age () { stat=$(stat --printf="%Y %F\n" "$1"); echo "The ${stat#* } '$1' is $((($(date +%s) - ${stat%% *})/86400)) days old."; }

Examples:

$ age foo
The regular file 'foo' is 41 days old.
$ age ../bar
The directory '../bar' is 296 days old.
$ age /path/to/baz
The symbolic link '/path/to/baz' is 207 days old.

Further refinement could be done to show the age in months, years, etc.

share|improve this answer
    
you take "one-liner" far to serious :) –  akira Jul 30 '10 at 14:37
    
nice one, but this is definitely option (C), shell script; not option (A), something I could memorize –  Marius Gedminas Aug 2 '10 at 15:48
    
@Marius: Well, the short version that just prints the number of days alone would be echo $((($(date +%s) - $(stat --printf="%Y" filename))/86400)) which may not be easy to memorize, but all you really need to memorize is the principle of how it works. It's easy enough to reconstruct on that basis. –  Dennis Williamson Aug 2 '10 at 16:05
    
In which case $(date +%s -r filename) may be easier to remember than $(stat ...). –  Marius Gedminas Aug 3 '10 at 9:22
    
I suppose I could manage to remember $[(`date +%s`-`date +%s -r filename`)/86400]. –  Marius Gedminas Aug 3 '10 at 9:24
show 1 more comment

It close enough to produce a good human readable date ! There is an option for ls --time-style. That let you format the date that will be displayed.

Example

 ls -l --time-style="+%b %_d %Y"

 -rw-r--r--  1 root root 11359620 Jul 20 2010 file.ext

To prevent typing this hung command you can alias it in your .bashrc file.

share|improve this answer
add comment

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.