[wplug] Shell: 'find' trick

Eric Cooper ecc at cmu.edu
Sat Oct 15 15:16:41 EDT 2005


On Sat, Oct 15, 2005 at 02:56:41PM -0400, Brandon Kuczenski wrote:
> I have a series of files that all start with the same name prefix: 
> something like `hostname -s`-fleem-`date +%F`.  The files were created on 
> various irregular dates (i.e. not once per day or week, but once per 
> whenever).
> 
> Can anyone think of a way to use 'find' to select only the newest file in 
> a directory tree that starts with a given name?  I don't know when 
> it was modified, only that it was modified more recently than the 
> other files that match the pattern.  Example:
> 
> # ls
> ocean-fleem-2005-10-01
> ocean-fleem-2005-10-04
> ocean-fleem-2005-10-05
> ocean-fluum-2005-10-01
> ocean-fluum-2005-10-04
> ocean-fluum-2005-10-06
> ocean-floob-2005-10-11
> # find . -name "ocean-fleem*" -magic-commands-to-list-most-recent
> ./ocean-fleem-2005-10-05
> #

Find is overkill. If you can rely on the date that's encoded in the
filename, just do

$ ls -1 ocean-fleem-* | head -1

If you want to use the filesystem modification time instead,
you can do

$ ls -1t ocean-fleem-* | head -1

-- 
Eric Cooper             e c c @ c m u . e d u


More information about the wplug mailing list