Replacing text within a bunch of files

Ed Wilts ewilts at ewilts.org
Sat Apr 10 19:49:14 UTC 2004


On Sat, Apr 10, 2004 at 11:55:24AM -0700, Chris wrote:
> I'm having a bit of a problem trying to figure out how to properly use 'sed' to
> do a global search and replace on a character string within ALL files in a
> directory and all its subdirectories.  The one-file-at-a-time method works fine,
> but obviously this needs to be looped to traverse all files and all
> subdirectories...
> 
> This is what I've tried:
> 
> sed -e 's/old_string/new_string/g'  foo1.txt > foo2.txt
> 
> and it works fine.  How can I loop it automatically through all
> file/subdirectories, though?

You should be able to use something like:

find -type f -exec ...
or
find -type f | xargs -n 1 ...
or
find -type f | xargs -n 1 replace old_string new_string --

The latter will do an inplace replacement.
[root at pe400 foo]# find -type f |xargs -n1  replace foostuff oldstuff --
./tmp/c converted
./a converted
./b converted

-- 
Ed Wilts, Mounds View, MN, USA
mailto:ewilts at ewilts.org
Member #1, Red Hat Community Ambassador Program





More information about the redhat-list mailing list