Skip to content

Instantly share code, notes, and snippets.

@rehmatworks
Last active September 14, 2017 17:38
Show Gist options
  • Save rehmatworks/6e6ab060a474d0d7cbbc29a6bd0fb706 to your computer and use it in GitHub Desktop.
Save rehmatworks/6e6ab060a474d0d7cbbc29a6bd0fb706 to your computer and use it in GitHub Desktop.
#!/bin/bash
printf "Original String: "
read -r ufr_oristring
if [[ -z "$ufr_oristring" ]]
then
echo -e "\e[31mPlease provide the original string that needs to be replaced\e[39m"
exit
fi
printf "New String (This will replace the Original String): "
read -r ufr_newstring
if [[ -z "$ufr_newstring" ]]
then
echo -e "\e[31mPlease provide the new string that should replace the string you provided above\e[39m"
exit
fi
printf "File Extensions to Search & Replace In (* for all): "
read -r ufrextension
if [[ -z "$ufrextension" ]]
then
echo -e "\e[31mPlease provide the extion of the files to search and replace in\e[39m"
exit
fi
printf "\e[31m$ufr_oristring\e[39m will be replaced with \e[31m$ufr_newstring\e[39m in files in current directory and all sub directories. Pleaes note that this change is permanent. You may break things horribly if you make wrong changes. Are you aware of this and want to proceed? (y/n): "
read -r ufr_confirm
if [[ "$ufr_confirm" == "y" || "$ufr_confirm" == "Y" ]]
then
find . -type f -name "*.$ufrextension" -exec sed -i'' -e "s#$ufr_oristring#$ufr_newstring#g" {} + &&
if [[ "$ufrextension" == "*" ]]
then
ufr_files="all files"
else
ufr_files="files with extension $ufrextension"
fi
echo -e "\e[32mThe find & replace query for $ufr_files in this and all sub directories has been run \e[39m"
else
echo -e "\e[31mYou decided not to execute this find & replace query. No actions performed.\e[39m"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment