?
Solved

replace string in all the files

Posted on 2003-11-10
8
Medium Priority
?
670 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 150 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 225 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

809 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