Skip to content

Instantly share code, notes, and snippets.

@mscalora
Last active March 8, 2020 00:29
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 mscalora/3f9756735036c27b7b13b9063c054d1d to your computer and use it in GitHub Desktop.
Save mscalora/3f9756735036c27b7b13b9063c054d1d to your computer and use it in GitHub Desktop.
search up a directory tree for multiple filenames
#!/bin/bash
search_all_up() {
# extended from https://stackoverflow.com/a/19011599/370746
local look=${PWD%/}
while [[ -n $look ]]; do
for name in $@ ; do
[[ -e $look/$name ]] && {
printf '%s\n' "$look/$name"
return
}
done
look=${look%/*}
done
[[ -e /$1 ]] && echo /
}
search_all_up one.txt two.txt three.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment