Recursive also means subdirectories, sub subdirectories, etc. On Sat, Apr 11, 2020 at 4:17 AM Bernhard Voelker <m...@bernhard-voelker.de> wrote:
> On 2020-04-10 19:29, Peng Yu wrote: > > On 4/10/20, Bernhard Voelker <m...@bernhard-voelker.de> 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