Last modified file in 2 or more directories

Hi,

Is there any simple way to get the last modified file in a set of 2 or more directories? This should return one file only (not 1 file per directory)

Thanks for your help

ls -drt /dir1/* /dir2/* | tail -1

Thanks Annihilanic. Would you know how to circumvent the ls: Arg list too long problem ?

I'm presuming there are no subdirectories. It becomes difficult with large numbers of files because you need to use multiple ls commands, therefore you can no longer use its built-in sort-by-time capability. Instead you have to it yourself. This is a little easier if you can be sure that you are only interested in files that were modified in the last 12 months, so you can ignore files whose dates contain a year, e.g.

find dir1 dir2 -type f | xargs ls -l | awk '$8 ~ /:/' | sort -k 6,6M -k 7,7n | tail -1 | awk '{print $NF}'

Also hopefully your version of sort supports the M sort key (i.e. months), otherwise you have to fiddle some more.

This is good enough for me, at leat it works with my setup. Thanks again