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

LinuxBashShellScriptingUbuntu

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

If you have a version of find (such as GNU find) that supports -printf then there's no need to call stat repeatedly:

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, though:

stat --printf="%y\n" *

Solution 3 - Linux

If I could, I would vote for the answer by Paulo. I tested it and understood the concept. I can confirm it works. The find command can output many parameters. For example, add the following to the --printf clause:

%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 raise this question as well: Does the author of this question want to solve his problem using Bash or PHP? That should be specified.

Solution 4 - Linux

It seems to me that simply: ls -lt mydirectory does the job...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrantisekView Question on Stackoverflow
Solution 1 - LinuxPaulo ScardineView Answer on Stackoverflow
Solution 2 - LinuxDennis WilliamsonView Answer on Stackoverflow
Solution 3 - LinuxMartianView Answer on Stackoverflow
Solution 4 - LinuxRodolfo MedinaView Answer on Stackoverflow