Linux search and replace
Search and Replace
If you want to search and replace text on a linux box you can use the tools grep and sed for revursive search and replace over a whole directory structure ...
The following command replaces all "searchstr" with "replacestr" in all subfolders. To limit this to a file filter you can replace "*" with something like "*.html" ... be careful with this command, because it changes all files directly without any further questions ...
grep -rl "searchstr" * | xargs sed -i -e 's/searchstr/replacestr/g'
Explanation on this command:
- grep -rl "searchstr" * build a file list with all files that contain "searchstr" and pipes it to xargs
- xargs takes this list and gives those files as parameters to the sed command
- sed does a in place edit and replaces "searchstr" with "replacestr" ... searchstr can be a regular expression ... if you have special chars within those strings you might need to escape them