Skip to content

Instantly share code, notes, and snippets.

@theirishpenguin
Created January 30, 2012 17:46
Show Gist options
  • Save theirishpenguin/1705634 to your computer and use it in GitHub Desktop.
Save theirishpenguin/1705634 to your computer and use it in GitHub Desktop.
Recursively replace files in the current directory and subdirectories using regular expressions
# Usage
# (Note: This command does not handle spaces in filenames!)
# * cd into the directory containing the files and subdirectories of files in
# which you wish to carry out string substitution using a regex
# * Run the command:
# rsub 'search regex' 'replacement text'
# * This will effect the desired substitution in files in the current directory
# and in any subdirectories
# Explaination
# * The -I {} allows you to grab each thing that xargs iterates over
# as {}
# * So {} is probably the most important thing
#
egrep -rl "$1" .|xargs -I {} sed -i "s/$1/$2/g" {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment