bug-findutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: recursive find in current and parent etc until an item is found


From: Bernhard Voelker
Subject: Re: recursive find in current and parent etc until an item is found
Date: Mon, 13 Apr 2020 18:55:55 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0

On 2020-04-12 21:12, Peng Yu wrote:
> OK. So this will make sure `/d1/d2/d3` will not be searched multiple
> times? Or it is still searched when `/d1/d2`, `/d1` or `/` is
> searched?

Well, it's easy to go up the tree and using the -prune option
to omit the already searched directories.

---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---
#!/bin/sh

f="$1" \
  && test -n "$f" \
  || { echo "Usage: $0 FILE" >&2; exit 1; }

# Search in current directory.
p="$( pwd )"
find -H "$p" -name "$f" -ls -quit \
  | grep . && exit 0;

# Search in all parent directories until the '/' root directory,
# using -prune to omit the already searched subdirectory.
while subdir="$p" && [ "$subdir" != '/' ] && p="$( dirname "$p" )"; do
  find -H "$p" -path "$subdir" -prune -o -name "$f" -ls -quit \
    | grep . && exit 0;
done

echo "$0: not found: '$f'" >&2
exit 1
--->8--->8--->8--->8--->8--->8--->8--->8--->8--->8--->8---

Have a nice day,
Berny



reply via email to

[Prev in Thread] Current Thread [Next in Thread]