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.
Written by Matt Senior
Related protips
2 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Sed
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#