For some reason I always lose this code snippet, so I decided to write it down here! It's a quick way to find and replace a string across any number of files.
grep -rl "old_string" somedir/ | xargs sed -i 's/old_string/new_string/g'
That's it! Simply feed a resulting collection of lines from files with some string old_string
, and replace with with new_string
. I highly recommend using some sort of version control and commiting right before using this. It could go terribly wrong!
PS. I believe on OSX, you need to provide another argument to sed with the file extension you want to use. You can also just provide a blank string:
grep -rl "old_string" somedir/ | xargs sed -i "" 's/old_string/new_string/g'