2ivpsg
Last Updated: February 25, 2016
·
14.2K
· mattsenior
74838fae38c88e23f10018425ab90fb1

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.

Say Thanks
Respond

2 Responses
Add your response

4421
B6ce7a43b172768e46dcbe233f772931

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 ·
4423
74838fae38c88e23f10018425ab90fb1

@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 ·
Filed Under