Skip to content

Instantly share code, notes, and snippets.

@moalex
Created February 22, 2021 15:51
Show Gist options
  • Save moalex/39692026e06a10d6975a7987649953db to your computer and use it in GitHub Desktop.
Save moalex/39692026e06a10d6975a7987649953db to your computer and use it in GitHub Desktop.
This utility will find <Search> string and replace it with <Replacement> string in all files in current folder including their both filename and contents.
#!/bin/bash
cat <<EOF
This utility will find <Search> string and replace it with <Replacement> string
in all files in current folder including their both filename and contents
EOF
# check if GNU rename utility exists
if [ ! -f /usr/local/bin/rename ]
then
echo "No GNU rename utility was found. Install it by running:"
echo "brew install rename"
fi
echo "Enter Search string:"
read FROM
echo "Enter Replacement string:"
read TO
echo "";
echo "Renaming files..."
find . -iname "*${FROM}*" -exec echo "Renaming {}" \; -exec /usr/local/bin/rename s/$FROM/$TO/ {} \;
find . -iname "*.[m,h]" -exec echo "Replacing {}" \; -exec sed -i '' "s/${FROM}/${TO}/g" "{}" \;
echo "Done"
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment