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

Bernhard Voelker <[email protected]>
Newsgroups gmane.comp.gnu.findutils.bugs
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.