Created
January 14, 2018 13:34
-
-
Save josuecau/20b75e5caa37483d54c7eb72baae7f95 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# | |
# Recursive search & replace | |
# Usage: search-replace SEARCH REPLACE [/path/to/search] | |
# Author: Josué Cau <me@josuecau.com> | |
# Version: v1.0.0 | |
if [ $# -lt 2 ]; then | |
echo "Usage: $(basename "$0") SEARCH REPLACE [/path/to/search]" | |
exit 1 | |
fi | |
if ! type realpath >/dev/null 2>&1; then | |
echo 'realpath(1) not found' | |
exit 1 | |
fi | |
if ! type rg >/dev/null 2>&1; then | |
echo 'rg(1) not found' | |
exit 1 | |
fi | |
export LC_CTYPE=C LANG=C | |
search="$1" | |
replace="$2" | |
dest=${3-"$PWD"} | |
dest="$(realpath "$dest")" | |
rg --files-with-matches --fixed-strings --null "$search" "$dest" \ | |
| xargs -0 sed -i '' "s|$search|$replace|g" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment