任意のフォルダ内の特定文字列を置換するBashスクリプト(MacOS用)

By shumpei, 20 4月, 2023

Chat GPTが教えてくれた。

#!/bin/bash search_string="bootstrap_barrio_subtheme" replace_string="bs_barrio_custom" folder_path="path/to/your/folder" # Find all files in the folder recursively find "$folder_path" -type f -print0 | while IFS= read -r -d $'\0' file; do # Check if the file contains the search string if grep -q "$search_string" "$file"; then echo "Replacing in $file" # Replace the search string with the replace string using sed # Use the following line for macOS sed -i '' "s/$search_string/$replace_string/g" "$file" # Use the following line for Linux # sed -i "s/$search_string/$replace_string/g" "$file" fi done