Linux Shell Scripting
This is a simple shell script that i used often to search and replace a string when I have multiple files within multiple folders. It is very useful when you need to search and replace a text in a folder with multiple files. Feel free to use and modify it to suit your need.
cd $1
for folder in *
do
echo “$folder”
cd “$folder”
echo -n “Your current dir is ”
pwd
for fl in *.htm;
do
mv $fl $fl.old;
sed ’s/string-to-search/replace-with-this-string/g’ $fl.old > $fl;
done
rm -rf *.old
chown owner.owner *.htm
cd ..
echo “Done … getting next folder”
sleep 5s
done
To use the code, create a file named changer, insert the above code. Then, chmod 755 the created file. Place the file outside the folder that you are trying to work on. Then execute, ./changer foldername, where foldername is the folder you are working on.
Add comment July 11th, 2006