Wednesday, June 10, 2015

Find and Replace String in multiple files.

Script to scan through files and replace a string

#!/bin/bash
     for fl in *.php; do
     mv $fl $fl.old
     sed 's/FINDSTRING/REPLACESTRING/g' $fl.old > $fl
     rm -f $fl.old
     done


$ find /home/bruno/old-friends -type f -exec sed -i 's/ugly/beautiful/g' {} \;

If there is single file.

$ sed -i 's/ugly/beautiful/g' /home/bruno/old-friends/sue.txt