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

Peng Yu <[email protected]>
Newsgroups gmane.comp.gnu.findutils.bugs
Message-ID <CABrM6wkdSXDE-8736c95YuMauThD6aY7emkOQdk2WHDNmci0ZQ@mail.gmail.com>
Recursive also means subdirectories, sub subdirectories, etc.

On Sat, Apr 11, 2020 at 4:17 AM Bernhard Voelker <[email protected]>
wrote:

> On 2020-04-10 19:29, Peng Yu wrote:
> > On 4/10/20, Bernhard Voelker <[email protected]> wrote:
> >> ---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; }
> >>
> >> p="."
> >>
> >> # Search until the parent is identical to the current directory (='/').
> >> until [ "$p" -ef "$p/.." ]; do
> >>   if [ -e "$p/$f" ]; then
> >>     echo "$p/$f"
> >>     exit 0
> >>   fi
> >>   p="$p/.."
> >> done
> >>
> >> # Now we're in the '/' dir; check once again.
> >> if [ -e "$p/$f" ]; then
> >>   echo "$p/$f"
> >>   exit 0
> >> fi
> >>
> >> echo "'$f' not found" >&2
> >> exit 1
> >> --->8--->8--->8--->8--->8--->8--->8--->8--->8--->8--->8---
> >>
> >> Have a nice day,
> >> Berny
> >>
> > Thanks. I will need to search the parent directory recursively. Does
> > this code only search whether a file is in a parent their parents,
> > etc., but not recursively for a given ancestor?
>
> it looks in the current directory, then its parent, then its grandparent,
> ... until either the file has been found, or until a directory is identical
> with its own parent (which is only true for the '/' root directory.
>
> Have a nice day,
> Berny
>
-- 
Regards,
Peng
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.