Skip to content

Instantly share code, notes, and snippets.

@jmbrunskill
Created June 9, 2022 04:49
Show Gist options
  • Save jmbrunskill/9d4fae10dc130ea6407bf02fa55e8a60 to your computer and use it in GitHub Desktop.
Save jmbrunskill/9d4fae10dc130ea6407bf02fa55e8a60 to your computer and use it in GitHub Desktop.
Bash Script to Search and Replace across Multiple files
#!/bin/bash
echo "Updating Files"
FILE_LIST=`find . -type f | grep -E "\.(rs|ts|tsx)$"` #Choose your prefered file types here
REPLACEMENTS_LIST=("s/Example/TitlecaseReplacement/g" "s/example/snake_case_relacement/g") #Add more replacements here as desire
echo "Replacing:"
for REPLACEMENT in ${REPLACEMENTS_LIST[@]}
do
echo $REPLACEMENT
done
echo "In these files:"
for FILE in $FILE_LIST
do
echo $FILE
done
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) echo "yes";;
* ) exit;;
esac
for FILE in $FILE_LIST
do
echo $FILE
cp $FILE $FILE.bak
for REPLACEMENT in ${REPLACEMENTS_LIST[@]}
do
echo $REPLACEMENT
sed -i '' $REPLACEMENT $FILE
done
done
read -p "Delete .bak files (y/n)?" choice
case "$choice" in
y|Y ) echo "yes";;
* ) exit;;
esac
for FILE in $FILE_LIST
do
echo $FILE
rm $FILE.bak
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment