Skip to content

Instantly share code, notes, and snippets.

@artursapek
Last active December 10, 2015 16:09
Show Gist options
  • Save artursapek/4459138 to your computer and use it in GitHub Desktop.
Save artursapek/4459138 to your computer and use it in GitHub Desktop.
Find/replace strings in files. WARNING: IT'S RECURSIVE!
#!/bin/sh
# Replace all instances of first string with second string. Use third arg as file(s) selector.
# If no selector is provided, do it to all files.
# Usage:
# ra artursapek ArturSapek
# => Replaces all instances of "artursapek" with "ArturSapek" in all files in this directory and its directories.
# Makes backups with names like index.html.1357352744.sedbak
# Required for sed to treat all ASCII as ASCII
# http://stackoverflow.com/questions/11287564/getting-sed-error-illegal-byte-sequence-in-bash
LANG=C
# Set SELECTOR to "*" by default
if [ -z "$3" ]
then
SELECTOR="*"
else
SELECTOR="*.$3"
fi
# This does the work:
find . -name "$SELECTOR" -type f -print0 | xargs -0 sed -i .$(($(date +%s))).sedbak "s/$1/$2/g"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment