[Gllug] Most Recent File

Benedikt Heinen gllug at ml.icemark.net
Tue Nov 22 16:29:05 UTC 2005


On Tue, 22 Nov 2005, Richard Jones wrote:

> On Tue, Nov 22, 2005 at 09:04:30AM -0000, Wiehe, Simon wrote:
>> I normally just use ls -ltr which gives a reverse sort by date, the bottom
>> of the list is the last file saved/touched.
>
> This is for a single directory though, right?  I also tried ls -lRt
> but that just sorts each individual directory - it doesn't give you
> the newest or oldest file in an entire tree.
>
> My best attempt was:
>
>  ls -l --time-style='+%s' -R | sort -k6 | less

Cute - didn't know that switch - but, from that there is another usable 
option - as the above has the (IMHO) big disadvantage that it will only 
show the filenames of files in subdirectories, instead of the full paths 
to the file.


How about a little variation on that, then?

 	find . -type f -print0 | xargs -0 ls -l --time-style=+%Y%m%d-%H%M%S | sort +5


As for script usability - you can still use it in scripts, though you 
might have to think of a way to cleanly extract the filename - two 
solutions spring to mind:


 	<command-as-above> | awk '{ print "$6" }'

but that would not yield the desired result, if you the path-/filename 
contained blanks.

Alternatively:

 	<command-as-above> | sed -e 's/.* [1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9] //'

(which would strip everything up to an including the formatted timestamp - 
the rest of the line would then hopefully be just the filename; though, 
this has the theoretical problem that it would cut away too much if (part 
of) the filename would match the timestamp regex above, e.g. if you had a 
file called

 	path/to/blabla 12345678-654321 foo.out

you would only get the "foo.out" part of that...   But - as I said, that 
seems rather unlikely, and can be circumvented by slightly modifying the 
date-format in ls and the matching sed regex...

Still an overkill solution, though... ;-)


Benedikt

   ALLIANCE, n.  In international politics, the union of two thieves who
     have their hands so deeply inserted in each other's pockets that
     they cannot separately plunder a third.
 			(Ambrose Bierce, The Devil's Dictionary)
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list