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.

link|improve this question

74% accept rate
serverfault.com/a/335384/59925 – quanta Dec 2 '11 at 16:27
feedback

4 Answers

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.

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

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

link|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 serverfault.com/a/335384/59925 (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
feedback

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

link|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
feedback

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`
link|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
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.