Reverse find
· 1 min read · February 05, 2025 · #tech #code In the last few days I discovered I needed to search back up the filesystem from $CWD to find the first occurence of a file (specifically, a Justfile but that’s by-the-by). Got bored of doing it by hand so wrote a bash shell function; here ’tis:
rf () {
local D
while ! eza -l "${D:=.}/$1"; do # first, check `$CWD`
[ "$(realpath "$D/$1")" == "/$1" ] && break # stop if we hit `/` already
D=$D/.. # else, iterate one layer up
done
}Invoke as (e.g.,) rf Justfile. Alternatively, as a one-liner:
F=Justfile; while ! eza -l ${D:=.}/$F; do [ "$(realpath $D/$F)" == "/$F" ] && break; D=$D/..; done; unset D