Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory tree with lots of branches and trees. At the top node, however, there are only five directories, let's say A-E. Within the directory tree, files are being updated at random.

I'd like to be able to get a listing of the directories (that is, like an ls) with a last modified file time which includes all sub directories and files within that directory. So I will end up with something like this:

A 12th June 08:00 B 13th June 08:00 C 14th June 08:00 D 15th June 08:00 E 16th June 08:00

I've tried variants on ls -t, but that does not seem to take sub-directories into account.

There's also something like this find . -type f | xargs ls -tr, but I can't see a way of getting the output I want using this (note: I'm very limited in my Unix knowledge, as you can probably tell).

share|improve this question
up vote 0 down vote accepted

This might be what you're looking for:

#!/bin/bash
for dir in A B C D E
do
    file=$dir
    while [[ -n $file ]]
    do
        prev=$file
        file=$(find $dir -newer $file -type f -print -quit)
    done
    mod=$(stat --format=%Y "$prev")
    echo "$dir $(date --date=@$mod '+%d %b %T')"
done
share|improve this answer
    
If you are going to assume stat (utterly reasonable), you can avoid the (potential) O(n^2) performance hit: mod="$(find "$dir" -print0 | xargs -0 stat --format=%Y | sort -rn | head -1)" – Chris Johnsen Jul 6 '10 at 16:11
    
Or a solid O(n): mod=$(max=0; find "$1" -print0 | xargs -0 stat --format=%Y | while read -r dt; do test $dt -gt $max && max=$dt && echo $max; done | tail -1) – Chris Johnsen Jul 6 '10 at 21:54
    
@Chris: Yes, yours is much faster. You should post it as an answer. However, you should know that the head -1 style is deprecated (and on my system tail -1 flat out refuses). It should be head -n 1. – Dennis Williamson Jul 8 '10 at 16:13

Use ls -clRt. It is recursive, and sorts by last modified time.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .