Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 07-23-2002, 01:03 AM   #1
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136

Rep: Reputation: 15
global search and replace?

Is there a relatively easy way to search for a string recursively throughout a directory tree and replace it with another string?

It could easily be restricted to files of a certain extension.

Old 07-23-2002, 01:17 AM   #2
LQ Addict
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
Look at perl scripts, it is a powerfull text editing tool as well

Last edited by neo77777; 07-23-2002 at 01:22 AM.
Old 07-23-2002, 06:06 AM   #3
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
I think the easiest way would be to create one simple script to replace a string. Something like:

if [ $# -lt 3 ]
  echo "usage: replace <filename> <searchstring> <replacestring>"

sed -e "s/$2/$3/g" $1 > $1.~bak
mv $1.~bak $1
And then run one command to search through a directory and replace strings using the script above. Something like:

find . -name "*.txt" -exec \replace {} "some string" "something" \;

It's just a quick script I put together so I can't guarantee that it will work in all cases. But it should work for most files. If it's important data you are gonna be running it on you might want to change the mv into a cp to make sure you still have the backup file in case it goes wrong.
Old 07-23-2002, 07:08 AM   #4
Registered: May 2001
Posts: 28,895
Blog Entries: 55

Rep: Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356
I use rpl, easy and safe (simulation mode).
Old 07-23-2002, 11:23 AM   #5
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136

Original Poster
Rep: Reputation: 15
I'll try them both.
Old 03-15-2006, 03:43 PM   #6
LQ Newbie
Registered: Mar 2006
Posts: 1

Rep: Reputation: 0
Cool RPL works nicely

Just to confirm, RPL works nicely. Tried it just now

I did not bother with scripting, as there are to many things to consider for such a simple procedure.....

best of luck...

--- www\.NetworkCriminals\.com


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
search/replace in many files allelopath Linux - General 1 08-02-2005 10:21 PM
Batch search and replace devilkin Linux - Newbie 2 02-14-2005 03:39 AM
linking downloaded c library to global search path syoh Programming 1 10-17-2003 02:33 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM
Search and replace links xtrude Linux - Newbie 4 01-29-2003 12:55 PM

All times are GMT -5. The time now is 01:15 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration