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: Sun, 12 Apr 2020 20:00:22 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0

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



reply via email to

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