This is the script to replace a particualr pattern in the files. The script works recursively. This is actually combination of the two scripts. as shown below.

Just copy the above below in a file named “replaceInFile”


#Script for replacing patterns in files




if [ $numPar -gt 2 ]


chmod 777 $3

printf "Replacing $1 by $2 in $3 ..."

sed -e "s/$pattern/$text/g" $3 1>> $

mv $3 $3.bak

mv $ $3

echo "done."


printf "repStr: invalid parameter count!\n"


This is the script which will actually perform the replacement. It uses the “sed” command to perform the actual substitution.

Now as we want the replacement to be done recursively in a folder we will use another script which will give the filenames one by one to this script.

Copy the following code in the file named replaceAll


find . -name "*.cpp" -exec ./replaceInFile "<>" " <>" {} \;

The above script will call the “replaceInFile” script for each “.cpp” of the file in the directory. The patterns to be searched and replaced should be mentioned in the breackets.

Now copy both the scripts in the directory where the replacement is to be done and just rin the replaceAll script. ( The script generates “.bak” files as backup )