Skip to content

Instantly share code, notes, and snippets.

@juliantroeps
Created June 3, 2019 14:29
Show Gist options
  • Select an option

  • Save juliantroeps/3db90c0a7a84dd26ff2da233ae3cd8a3 to your computer and use it in GitHub Desktop.

Select an option

Save juliantroeps/3db90c0a7a84dd26ff2da233ae3cd8a3 to your computer and use it in GitHub Desktop.
Basic recursively string replacement shell script.
#!/bin/bash
# Usage: 'sh replace.sh old_string new_string'
# Replaces given string in all files and subfolders (recursively).
if [ -n "$1" ]; then
if [ -n "$2" ]; then
echo "Replacing $1 with $2.";
grep -ilr $1 * | xargs -I@ sed -i '' "s/$1/$2/g" @
echo "Done!";
else
echo "Second parameter not supplied. Choose a replacement for string $1."
fi
else
echo "First parameter not supplied. Usage: replace.sh old_string newstring"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment