Linux tips

How to replace a word in all files inside and directory recursively.
This command will replace all the word in inside a directory recursively:
grep -rl string1 ./ | xargs sed -i 's/string1/string2/g'
To replace a word in files inside a certain directory recursively run this command:
grep -rl string1 somedir/ | xargs sed -i 's/string1/string2/g'
To replace a word in files with a extension inside a directory:
sed -i 's/fea/asd/g' *.php
The command above will replace the fea with asd in all php files inside a directory.
This command will replace a word in all files inside a directory:
replace "old_string" "new_string" -- *
If you have list of files with extension, you can use:
replace "old_string" "new_string" -- *.extension
Also can use this perl code replace a word in files with an extension inside a directory:
perl -pi -w -e 's/search/replace/g;' *.php
Top


<< Previous Next >>