Based on the Idea of @Womble, I wrote a small script that does the trick:
#!/bin/bash
#
# rfind: finds a file in one of the parent directories
needle=$1
current_dir=$(pwd)
path=
while [ "$current_dir" != "$(dirname $current_dir)" ]; do
if [ -e "${current_dir}/$needle" ]; then
echo $path$needle
exit 0
else
path=../$path
current_dir="$(dirname "$current_dir")"
fi
done
if [ ! "$current_dir" != "$(dirname $current_dir)" ]; then
echo "rfind: file $needle not found" >&2
exit 1
fi
And named it 'rfind' in my own ~/bin directory. It does the trick:
/tmp $ mkdir -p x/y/z
/tmp $ cd x/y/z/
/tmp/x/y/z $ rfind a.txt || echo "not found."
rfind: file a.txt not found
not found.
/tmp/x/y/z $ touch ../../a.txt
/tmp/x/y/z $ rfind a.txt || echo "not found."
../../a.txt
/tmp/x/y/z $
/tmp/x/y/z $ cd ..
/tmp/x/y $ mkdir z2
/tmp/x/y $ cd z
/tmp/x/y/z $ touch ../z2/a.txt
/tmp/x/y/z $ rfind a.txt || echo "not found."
../../a.txt
/tmp/x/y/z $ rm ../../a.txt
/tmp/x/y/z $ rfind a.txt || echo "not found."
rfind: file a.txt not found
not found.