Shell Script: File Name replacement (using SED)

Problem:

We have an environment named MX11, where we are planning to change the environment name with Q1_MX11.
Because of this, all files inside MX11 directory needs to be renamed. I need to change all the files or directories that have the name MX11 to Q1_MX11. please help me with a script to change all the directories or files under home\MX11.

Solution:
********
#!/bin/bash

SEARCH_DIR=/home/MX11
SEARCH_STR=MX11
REPLACE_STR=Q1_MX11

for fname in `find $SEARCH_DIR -name “*$SEARCH_STR*”`
do

#echo $fname
nfname=`echo $fname | sed “s/$SEARCH_STR/$REPLACE_STR/g”`
#echo $nfname
echo “$fname is changed to $nfname”
sleep 1

done

 

Thanks,
Ravi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: