Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Created May 28, 2010 10:15
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save badsyntax/416993 to your computer and use it in GitHub Desktop.
Simple recursive search and replace string in files
#!/usr/bin/env bash
#
# simple recursive search and replace string in files
# setup: alias replace='~/replace.sh'
# notes: you will need to escape special chars! eg: replace '\/dir1' '\/dir2' .
# usage: replace <oldstring> <newstring> <path> <depth>
# examples:
# replace "([^\s]+.php)" "\/\1" .
# replace "\"\/([^\/]+.php)" "\"\/dir\/\1" .
if [ ! -n "$1" ] || [ ! -n "$2" ] || [ ! -n "$3" ]; then
echo "Usage: replace.sh OLDSTRING NEWSTRING PATH"
exit;
fi
[ -n "$4" ] && maxdepth=$4 || maxdepth=9999
find "$3" -maxdepth "$maxdepth" -type f -exec perl -pi -e "s/$1/$2/g" '{}' \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment