Skip to content

Instantly share code, notes, and snippets.

@psaxton
Last active January 6, 2022 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psaxton/6937fec3357f8295828d2e3577f9bcb6 to your computer and use it in GitHub Desktop.
Save psaxton/6937fec3357f8295828d2e3577f9bcb6 to your computer and use it in GitHub Desktop.
sh function for finding the first existence of a file in a parent directory. Usage: `lsup [path] file`
lsup() {
path="$1";
file="$2";
if [ -z "${file}" ]; then
file="${path}";
path="$(pwd)";
fi;
path="$(realpath -es "${path}")"
[ -n "${SH_DEBUG}" ] && echo "Searching for ${file} from ${path}…";
while [ "${path}" != "/" ]; do
[ -e "${path}/${file}" ] && break;
[ -n "${SH_DEBUG}" ] && echo "Not found in ${path}";
path="$(realpath -s "${path}/..")";
done;
[ -e "${path}/${file}" ] && echo "${path}/${file}" || return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment