1

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?

2 Answers 2

9

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.

6
  • you take "one-liner" far to serious :)
    – akira
    Jul 30, 2010 at 14:37
  • nice one, but this is definitely option (C), shell script; not option (A), something I could memorize Aug 2, 2010 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. Aug 2, 2010 at 16:05
  • In which case $(date +%s -r filename) may be easier to remember than $(stat ...). Aug 3, 2010 at 9:22
  • I suppose I could manage to remember $[(`date +%s`-`date +%s -r filename`)/86400]. Aug 3, 2010 at 9:24
1

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.

You must log in to answer this question.

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