Looking for a shell one-liner to display the age of a file (in days)

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

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

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like