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-11 12:19, Peng Yu wrote:
> Recursive also means subdirectories, sub subdirectories, etc.

ah, so you want to fall back for the search to the parent and all
parent directories.  E.g. if you are in
  /d1/d2/d3
then you want to search in
  /d1/d2/d3
falling back to
  /d1/d2
and
  /d1
and
  /
right?

Then you just need to convert $PWD accordingly and pass it
to one find invocation.  Maybe something like this?

---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; }

# Expand "/d1/d2/d3" into "/d1/d2/d3  /d1/d2  /d1  /".
sedSplitDirs='
  :a # loop target.
  # handle end of loop: append slash, print + quit.
  /^$/ {
    c\
/
    p; q};
    # Print each line.
    p
    # Then strip off trailing dir ...
    s|/[^/]*$||;
    # ... and jump to :a.
    ta
'

find -H $( pwd | sed "$sedSplitDirs" ) -name "$f" -ls -quit
--->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.