[wplug] Re: Shell: 'find' trick

Christian Holtje list.wplug at docwhat.gerf.org
Mon Oct 17 12:35:42 EDT 2005


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
> #


>From the example, this sounds like it would work:

# warning: this was written off the cuff....but any bugs should be harmless
for i in $(ls -1 | perl -p -e 's/^(.*-fleem)-\d\d\d\d-\d\d-\d\d$/$1/;' |
sort | uniq); do
ls -1 $i-* | sort | tail -n 1
done

That's assuming that you only want to go by filename and ignore the
filesystem information (mtime and ctime).

Ciao!


More information about the wplug mailing list