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-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
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.