Skip to content

Instantly share code, notes, and snippets.

@s-hiiragi
Created August 4, 2025 14:50
Show Gist options
  • Select an option

  • Save s-hiiragi/7b2d4aa0d42139e4cab2ee7029cde387 to your computer and use it in GitHub Desktop.

Select an option

Save s-hiiragi/7b2d4aa0d42139e4cab2ee7029cde387 to your computer and use it in GitHub Desktop.
ルートディレクトリ方向に向かってファイルを検索するコマンド
#!/bin/bash
usage_exit() {
local PROG=$(basename "$0")
cat <<EOS >&2
$PROG -- find a file towards the root directory
$PROG [OPTIONS] FILENAME
OPTIONS
-d show only a directory name
-h show this help
EOS
exit 1
}
ONLY_DIRNAME=false
case "$1" in
-h | --h | "")
usage_exit
;;
-d)
ONLY_DIRNAME=true
shift
;;
esac
FILENAME="$1"
dir=$(pwd)
while :; do
f="$dir/$FILENAME"
if [[ -f "$f" ]]; then
if "$ONLY_DIRNAME"; then
echo "$(dirname "$f")"
else
echo "$f"
fi
exit 0
fi
if [[ "$dir" == "/" ]]; then
exit 1
fi
dir=$(dirname "$dir")
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment