Search and replace text in matching files on the command line.

After a bit of searching and trying a few different methods, I’ve settled on the following commands to find and replace a text string in all files that match the search string identified through grep.

grep -r -l 'searchtext' . | sort | uniq | xargs perl -e "s/matchtext/replacetext/" -pi
grep -r -l 'searchtext' .
$ | sort | uniq
$ | xargs perl -e "s/matchtext/replacetext/" -pi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store