Solved

replace string in all the files

Posted on 2003-11-10
8
662 Views
Last Modified: 2013-12-27
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
0
Comment
Question by:vaibhavmishra062201
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9714311
Hi vaibhavmishra,

try this

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

Cheers!
Sunny:o)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9714328
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
0
 
LVL 7

Accepted Solution

by:
glassd earned 50 total points
ID: 9714437
Alternatives.

Use find to get the file list:

for File in $(find . -type f -print)
do
   ...
done

Use ed to edit the files in place. No separate output file created:

ed - $File <<- EOF
g/abc/s/abc/xyz/g
w
EOF
0
 

Author Comment

by:vaibhavmishra062201
ID: 9714863
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 75 total points
ID: 9714906
riding on glassd's suggestion of using find

for i in `find . -type f -print`
do
    sed -e 's/abc/xyz/g' $i > $i.new
    mv -f $i.new $i
done

0
 

Author Comment

by:vaibhavmishra062201
ID: 9715426
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 ?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 9718401
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
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9720077
>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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now