Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I want to accomplish here is this:

I have an apache website, and on that website, I want to display something like

Latest website update: 01/12/2011 at 6h32 AM

I had an idea on how to do this. I could write an hourly script that checks the date of the latest modified file in the /var/www. And then store this value in a file or in the database for fast access.

How can I do this, and if you have a better idea, please share it with me.

share|improve this question – quanta Dec 2 '11 at 16:27
up vote 2 down vote accepted

This gives the exact output you asked for in your question:

echo "Latest website update: $(date -d @$(find /var/www -type f -exec stat -c%Z {} \; | sort | tail -1) "+%d/%m/%Y at %lh%M %p")"

Latest website update: 02/12/2011 at 8h55 PM

It was a fun one-liner to puzzle together, but I wouldn't recommend using it. It will probably be slow.

share|improve this answer
That's exactly what I was looking for, and it works! – Jonathan Rioux Dec 2 '11 at 17:48
$lastupdated = `ls -ltr <directory> | tail -n 1`

need to do some cutting on the line, but basically this is your last updated file + date.

share|improve this answer
No, this gives the date for the folder only! Test it by yourself and you'll see that you're wrong. – Jonathan Rioux Dec 2 '11 at 16:25
This looks correct to me, unless you need to do a recursive search in all sub-directories of /var/www. In that case you would need to use the method at (suggested by quanta). – Scott Duckworth Dec 2 '11 at 16:47
Place a trailing slash at the end of the directory ls -ltr <directory/> and this will work. – calman Dec 2 '11 at 17:14

Maybe using the output of GNU stat will help. stat -x /var/www

share|improve this answer
You're wrong, this gives the date for the folder only. Iv tested it and it doesnt work. – Jonathan Rioux Dec 2 '11 at 16:29

Are you looking for any file in and under the given directory?

For one directory, @Flash's answer works fine. (Although ls -lt /var/www | head -n 2 | cut -c40-53 is a little bit faster, at the expense of an extra \n in the result)

For a whole directory tree, you can use a variation on

`find /var/www  -type f -printf '%T@\t%TH:%TM on %Tx\t%p\n' | sort -k1 -n | cut -f 2 | head -n 1`
share|improve this answer
very much as suggested by @quanta, only difference is the formatting being done on each line. quanta's method is probably wiser, you can use $(insert-language-of-choice perl php ...) to convert into your preferred date format without doing so for every file – BRPocock Dec 2 '11 at 17:01

Your Answer


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.