Last Updated: February 25, 2016
·
78.89K
· mattsenior

Recursive find & replace with sed

Handy command to search recursively from the current directory, and use sed to replace text.

The example below will replace all occurrences of foo with bar:

egrep -lRZ 'foo' . | xargs -0 -l sed -i -e 's/foo/bar/g'

egrep -R is what enables the recursive search, and sed -i enables Sed’s ‘in-place’ mode to modify the files directly.

2 Responses
Add your response

Usually I use something like that: `find . -exec sed -i -e 's/foo/bar/g' {} \;' . Is there some feature uncovered that your methods does?

over 1 year ago ·

@yifu Hey, not much difference, no, it just greps through first to narrow the list of files that the sed replace is run against. Maybe wasted effort as sed will search anyway, but it does the job :)

over 1 year ago ·