[wplug] Shell: 'find' trick
Chester R. Hosey
chosey at nauticom.net
Sun Oct 16 09:22:29 EDT 2005
Eric Cooper wrote:
> On Sat, Oct 15, 2005 at 02:56:41PM -0400, Brandon Kuczenski wrote:
>
>>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:
>>
>># 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
That won't catch filenames within a directory tree, only ones in the
current directory. You could try one of the following:
# Use find to grab a list of filenames, and use that list
# as input to ls. Tell ls to sort by time, and only print
# the first match.
ls -t `find . -name ocean-fleem-\* -type f` | head -1
# Tell find to print print matching filenames with four-digit year,
# zero-padded day of year (001..366), and zero-padded time. Sort in
# reverse (descending order) and print the first.
find . -name ocean-fleem\* -type f -printf "%AY %Aj %AT %p\\n" \
| sort | head -1
The first uses backtics (on the ~ key) to substitute find's output into
the command line for ls. To get a feel for what backtics do, try
something like the following:
echo ls -t `find . -name ocean-fleem-\* -type f`
to see what command will actually result. The second just uses 'find' to
display information about the matching files in a way that's easy to
sort by date.
Please let me know if I can clarify, or if there's anything I've missed.
Chet
More information about the wplug
mailing list