Skip to content

Instantly share code, notes, and snippets.

@josuecau
Created January 14, 2018 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josuecau/20b75e5caa37483d54c7eb72baae7f95 to your computer and use it in GitHub Desktop.
Save josuecau/20b75e5caa37483d54c7eb72baae7f95 to your computer and use it in GitHub Desktop.
#!/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