Replace a string recursively in a directory on Mac OS X
Pretty often in the development world, you want to replace a given string by another one in a bunch of files. Some people like do that with their favorite editors (using search/replace or "refactoring" tools in IDEs for example).
If for some reason you have to use the terminal for this, the following command allows you to replace string1 by string2:
find . -type f | xargs sed -i '' 's/string1/string2/g'
Note that this operation will silently replace string1 by string2 in all the files of the current directory, recursively.
To restrict the replacement to particular file extensions, use the additional -name flag as follows:
find . -type f -name "*.rb" | xargs sed -i '' 's/string1/string2/g'
Which will only replace string1 by string2 in Ruby files.
Written by Bertrand Chardon
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Shell
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#