Skip to content

Instantly share code, notes, and snippets.

@ianhomer
Last active November 8, 2020 18:50
Show Gist options
  • Save ianhomer/5f0278e101cc3954e66a00bc690f6271 to your computer and use it in GitHub Desktop.
Save ianhomer/5f0278e101cc3954e66a00bc690f6271 to your computer and use it in GitHub Desktop.
change foo to bar in all js files in folder
#
# Change foo to bar in all js,ts,jsx and tsx files
#
# - Mac variant (i.e. -i '' for inline)
# - Only sed on files that match - i.e. don't waste energy sedding when no need
# - output lines that have changed
# - don't scan node_modules and dist folders
# - ignore files with test in name
#
find . -type d \( -name node_modules -o -name dist \) -prune -false \
-o -type f \( -name '*.[jt]s' -o -name '*.[jt]sx' \) -exec grep -l "foo" {} \; | \
egrep -v "test" | \
xargs -t -n 1 -P 10 sed -i '' "s#foo#bar#g w /dev/stdout"
# Change back again
find . -type d \( -name node_modules -o -name dist \) -prune -false \
-o -type f \( -name '*.[jt]s' -o -name '*.[jt]sx' \) -exec grep -l "bar" {} \; | \
egrep -v "test" | \
xargs -t -n 1 -P 10 sed -i '' "s#bar#foo#g w /dev/stdout"
# List files that are going to change
find . -type d \( -name node_modules -o -name dist \) -prune -false \
-o -type f \( -name '*.[jt]s' -o -name '*.[jt]sx' \) -exec grep -l "foo" {} \; | \
egrep -v "test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment