Find & Replace string

David Jenkins david.jenkins at gmail.com
Thu Dec 9 13:50:01 PST 2004


On Thu, 9 December, 2004 18:50, antenneX said:
> No, I want to interrogate several hundred thousand files throughout
> several thousand directories to find/replace a single string within
> each
> file found. The string may appear more than once in a file.

Try the following (make sure you have a backup first ;))

perl -pi -e 's/STRING_TO_FIND/STRING_TO_REPLACE_WITH/g' filename

e.g. to replace all instances of foo with bar in a file called test
you'd do:

perl -pi -e 's/foo/bar/g' test

You'd need to write a shell script to recursively run this on in each
subdirectory.

Cheers,
David


More information about the freebsd-questions mailing list