1

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.

Jonathan Rioux
  • 1,938
  • 6
  • 33
  • 57

4 Answers4

2

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.

quanta
  • 51,413
  • 19
  • 159
  • 217
Kenny Rasschaert
  • 9,045
  • 3
  • 42
  • 58
1
$lastupdated = `ls -ltr <directory> | tail -n 1`

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

Flash
  • 1,310
  • 7
  • 13
  • No, this gives the date for the folder only! Test it by yourself and you'll see that you're wrong. – Jonathan Rioux Dec 02 '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 http://serverfault.com/a/335384/59925 (suggested by quanta). – Scott Duckworth Dec 02 '11 at 16:47
  • Place a trailing slash at the end of the directory `ls -ltr ` and this will work. – calman Dec 02 '11 at 17:14
0

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

Tim
  • 3,017
  • 17
  • 15
0

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`
BRPocock
  • 198
  • 6
  • 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 02 '11 at 17:01