1
Dec

Recursive Search/Replace Text from Terminal

This came in handy for myself the other day and I figured it might come in handy for some of you out there. This is a quick command that will recursively do a find and replace for certain keywords by using a combination of grep and sed. Basically, we search recursively using grep and then we pipe in the filenames into sed for the string replacement.

grep -Irl keyword . | xargs sed -i 's/keyword/replacement/g'

Here’s a breakdown of the options used (I’ll assume you’re familiar with xargs and piping):

grep -I will ignore binary files
grep -r is recursive
grep -l will spit out the filename only
sed -i will edit the files in place

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 2 Comments So Far

Share your thoughts, leave a comment!