in linux terminal, how do I show the folder's last modification date, taking its content into consideration?

Richard Rodriguez picture Richard Rodriguez · Feb 14, 2011 · Viewed 104.1k times · Source

So here's the deal. Let's say I have a directory named "web", so

$ ls -la

drwx------  4 rimmer rimmer 4096 2010-11-18 06:02 web

BUT inside this directory, web/php/

$ ls -la

-rw-r--r-- 1 rimmer rimmer 1957 2011-01-05 08:44 index.php

That means that even though the content of my directory, /web/php/index.php has been last modified at 2011-01-05, the /web/ directory itself is reported as last modified at 2010-11-18.

What I need to do is have my /web/ directory's last modification date reported as the latest modification date of any file/directory inside this directory, recursively.

How do I go about doing this?

Answer

Paulo Scardine picture Paulo Scardine · Feb 14, 2011

Something like:

find /path/ -type f -exec stat \{} --printf="%y\n" \; | 
     sort -n -r | 
     head -n 1

Explanation:

  • the find command will print modification time for every file recursively ignoring directories (according to the comment by IQAndreas you can't rely on the folders timestamps)
  • sort -n (numerically) -r (reverse)
  • head -n 1: get the first entry