3

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).

1

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
  • 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. – Paused until further notice. Jul 8 '10 at 16:13
2

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

  • Does not answer the question -- the poster was not asking for a recursive list of directories and their contents but rather a flat list of specific items, each alongside a stat (last modified file time) that has been obtained by recursive traversal. – AndrewF Apr 10 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.