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: Peng Yu
Subject: Re: recursive find in current and parent etc until an item is found
Date: Sat, 11 Apr 2020 05:19:12 -0500

Recursive also means subdirectories, sub subdirectories, etc.

On Sat, Apr 11, 2020 at 4:17 AM Bernhard Voelker <address@hidden>
wrote:

> On 2020-04-10 19:29, Peng Yu wrote:
> > On 4/10/20, Bernhard Voelker <address@hidden> 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


reply via email to

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