In linux terminal, how can I show the date of the last modification of a folder, given its contents?

So here is the deal. Say I have a directory called "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 

This means that although the contents of my directory, /web/php/index.php was last changed in 2011-01-05, the / web / directory itself is reported as last modified in 2010-11-18.

What I need to do is to indicate the last modification date of my / web / directory, indicated as the last modification date of any file / directory inside this directory, recursively.

How should I do it?

+52
linux scripting bash shell ubuntu
Feb 14 2018-11-21T00:
source share
3 answers

Something like:

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

Explanation:

  • the find command will print the modification time for each file, recursively ignoring directories (according to IQAndreas comment you cannot rely on folder timestamps)
  • sort -n (numeric) -r (reverse)
  • head -n 1: get first record
+47
Feb 14 2018-11-21T00:
source share

If you have a version of find (e.g. GNU find ) that supports -printf , then there is no need to call stat several times:

 find /some/dir -printf "%T+\n" | sort -nr | head -n 1 

or

 find /some/dir -printf "%TY-%Tm-%Td %TT\n" | sort -nr | head -n 1 

If you don't need recursion:

 stat --printf="%y\n" * 
+17
Feb 14 '11 at 10:18
source share

If I could, I would vote for Paulo's answer. I checked this and understood the concept. I can confirm that it works. The find can output many parameters. For example, add the following to the --printf :

 %a for attributes in the octal format %n for the file name including a complete path 

Example:

 find Desktop/ -exec stat \{} --printf="%y %n\n" \; | sort -n -r | head -1 2011-02-14 22:57:39.000000000 +0100 Desktop/new file 

Let me also raise this question: Can the author of this question solve his problem using Bash or PHP? This should be indicated.

+5
Feb 14 '11 at 10:17
source share



All Articles