Created
May 28, 2010 10:15
-
-
Save badsyntax/416993 to your computer and use it in GitHub Desktop.
Simple recursive search and replace string in files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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