Find and Replace within Files

duanestubbs's picture
Find and Replace text in files from the command line
Search through all the files and sub-folders (remove R from -RIZ to just do current directory) and replace text within files.

grep -RlZ {search text in files} . \ | xargs -0 -l  sed -i -e 's/{find}/{replace}/'

Example:

If you need to replace the IP address 192.168.2.104 with 192.168.2.20 within php files (*.php), cd to the directory containing the files, then type:

grep -RlZ 192.168.2.104 *.php \ | xargs -0 -l  sed -i -e 's/192.168.2.104/192.168.2.20/'  

 

Warning: the changes cannot be undone, unless your do another find and replace of course!