Find & Replace in Files on Linux
April 13, 2009 12:28 pm Code Monkey
A lot of solutions I’ve found for recursively replacing text in files is implemented using shell scripts, perl, php, or some other inconvenient way. Rushi got it right by using the Linux command line. Here it is (slightly modified) from his blog:
find . -name “*.cpp” -print | xargs sed -i ’s/[find]/[replace]/g’
where “[find]” and “[replace]” are the things you are searching for and substituting.
To search files with multiple file extensions, use:
find . -name “*.cpp” -o -name “*.h” -o -name “*.c” | xargs sed -i ’s/[find]/[replace]/g’
ADDED 4-13-2009: See comments for other variations.


April 13th, 2009 at 2:22 pm
Back when I actually worked for a living, I was always fond of the “exec” argument to find for this type of work. Will this suffice?
find . -name “*.cpp” -exec sed -ie ’s/[find]/[replace]/g’ {} \; -print
[Reply]
May 13th, 2009 at 4:20 am
Steve,
exec works well too, but it’s slower than using xargs
[Reply]