86

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?

Frantisek
  • 7,485
  • 15
  • 59
  • 102

4 Answers4

72

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
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • 1
    According to [the coreutils documentation](https://www.gnu.org/software/coreutils/manual/html_node/head-invocation.html#head-invocation), you might consider `head -n 1` or `sed 1q` instead of the obsolete syntax. – sappjw Dec 20 '12 at 20:16
  • 2
    So `head -1` to return just the first line is now obsolete... Man, I remember when it used to be the new flag - I'm that old!!! :o) – Paulo Scardine Mar 18 '15 at 20:52
  • This is such a great example of the strength and weakness of the linux command line. – Ternary Aug 05 '15 at 18:04
  • 1
    In most cases, I would add the `-type f` flag, so it only checks the modification time of files. I can't remember which program it was I used, but when I copied files from one location to another, it kept the timestamps of the files, but marked the directories as brand new, throwing off the results. – IQAndreas Apr 16 '16 at 10:36
  • What a user-friendly OS – EugZol Oct 22 '21 at 16:09
31

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" *
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
7

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.

Martian
  • 71
  • 1
  • 3
    Good question, I thought it was obvious though, as my tags say "bash, scripting, shell". I want to solve this problem using Bash, yes. It's for backing up server files. I think the problem is solved now, the solutions work perfect :) – Frantisek Feb 15 '11 at 11:34
  • You seem to be using *both* `stat` and the built-in `-printf` flag of `find`, and then basically throwing away one of the results. I guess you should prefer the latter over the former, as outlined in a separate answer (no need to spawn a separate process for each file when `find` already does it all in one process). – tripleee Dec 27 '17 at 15:17
4

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

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • 1
    It does not, but it's easy to think that. `ls -t` (or the `--time` option) on a directory will show when that directory was created, but it will not reflect the last-modified time of files within that directory. – dshaw Nov 02 '20 at 00:56
  • 1
    `ls -lt mydirectory' shows to me the last-modified time of files and subdirs within that dir... – Rodolfo Medina Nov 07 '20 at 07:56
  • This only works for files inside mydirectory, not for files inside any subdir in mydirectory. See [this](https://stackoverflow.com/questions/3451863) discussion. – yagus Jun 14 '21 at 11:14