Skip to content

Instantly share code, notes, and snippets.

@takayamaekawa
Last active March 15, 2025 04:36
Show Gist options
  • Select an option

  • Save takayamaekawa/bd58b460175567aaa76c76b2e84ae446 to your computer and use it in GitHub Desktop.

Select an option

Save takayamaekawa/bd58b460175567aaa76c76b2e84ae446 to your computer and use it in GitHub Desktop.
階層文字検索・置換のためのバッシュスクリプト

CLI環境でVSCodeのC-SHIFT-f(階層文字列検索)やC-SHIFT-H(階層文字列置換)に対応するバッシュスクリプト!!
入力はその都度変わると思うので、対話的に受け取るように。
また、それらバッシュスクリプトをグローバルに使うためのバッシュスクリプトを用意。

├── global
│   ├── finder.sh
│   └── replacer.sh
└── setglobal.sh
#!/bin/bash
read -p "検索対象のディレクトリを入力してください(デフォルト: .): " search_dir
search_dir=${search_dir:-.}
read -p "除外するディレクトリを入力してください(デフォルト: */node_modules): " exclude_dir
exclude_dir=${exclude_dir:-*/node_modules}
read -p "検索する文字列を入力してください: " search_string
if [ -z "$search_string" ]; then
echo "エラー: 検索する文字列を入力してください。" >&2
exit 1
fi
find "$search_dir" -type f ! -path "$exclude_dir/*" -exec grep -Hn "$search_string" {} \;
#!/bin/bash
read -p "検索対象のディレクトリを入力してください(デフォルト: .): " search_dir
search_dir=${search_dir:-.}
read -p "除外するディレクトリを入力してください(デフォルト: */node_modules): " exclude_dir
exclude_dir=${exclude_dir:-*/node_modules}
read -p "検索する文字列を入力してください: " search_string
if [ -z "$search_string" ]; then
echo "エラー: 検索する文字列を入力してください。" >&2
exit 1
fi
read -p "置換する文字列を入力してください(空文字も許容): " replace_string
find "$search_dir" -type f ! -path "$exclude_dir/*" -exec grep -l "$search_string" {} \; | while read -r file; do
sed -i "s|$search_string|$replace_string|g" "$file"
echo "置換完了: $file"
done
#!/bin/bash
cd "$(dirname "$0")"
SOURCE_DIR="./global"
DEST_DIR="/usr/local/bin"
for file in "$SOURCE_DIR"/*.sh; do
if [ -f "$file" ]; then
filename=$(basename "$file" .sh)
dest_file="$DEST_DIR/$filename"
sudo cp "$file" "$dest_file"
sudo chmod +x "$dest_file"
echo "コピー完了: $file -> $dest_file"
else
echo "対象ファイルが見つかりません: $file"
fi
done
echo "すべての.shファイルのコピーが完了しました。"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment