Link to home
Start Free TrialLog in
Avatar of vaibhavmishra062201
vaibhavmishra062201

asked on

replace string in all the files

Hi,

I want to replace a string "abc" with "xyz" in all the files, in current directory and all its subdirectories. How to do that ?
I do not want to open each and every file and replace the string.

Thanx in advance,
Vaibhav
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi vaibhavmishra,

try this

ls -R | while read i
do
     sed -e 's/abc/xyz/g' $i
done

Cheers!
Sunny:o)
oops

ls -R <name of directory to which you want to apply changes>| while read i
do
     sed -e 's/abc/xyz/g' $i > $i.new
done

moreover.. this will store modified files in filename.new

you can execute a mv in similar loop to replace old files with new ones

ls -R | while read i
do
     sed -e 's/abc/xyz/g' $i > $i.new
     mv -f $i.new $i
done
ASKER CERTIFIED SOLUTION
Avatar of glassd
glassd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vaibhavmishra062201
vaibhavmishra062201

ASKER

hi sunnycoder

it is creating al the .new files in current folder ...
can you please modify the script so that .new file gets created in the folder same as the original file .. ?

Vaibhav
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok ... i ran it on a test folder and it is working fine ..

do you want to share the points with glassd ? if yes how many ?
I will add my usual comment about checking the status before renaming the file, otherwise you can end up nobbling loads of files

sed 's/abc/xyz/g' $i >$i.new && mv $i.new $i
>do you want to share the points with glassd ? if yes how many ?
you have to decide if you want to split and the ratio too