Linux Shell Scripting

July 11, 2006

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.

Filed under: Unix / Linux

Leave a Comment

(required)

(required), (Hidden)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

TrackBack URL  |  RSS feed for comments on this post.


 

July 2006
M T W T F S S
« May   Aug »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Pages

Blogroll

Categories