Itai Ganot
Itai Ganot

Reputation: 6305

How can I recursively replace a string with another in many files using bash?

I need to write a script that replaces all the occurrences of a certain string to "replaceWithThisNewString" in all the files in a directory and all its sub directories. How can it be done?

Upvotes: 0

Views: 127

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185095

Using :

shopt -s globstar # if not already enabled
sed -i '/certainString/s/certainString/replaceWithThisNewString/g' **

Upvotes: 0

Wrikken
Wrikken

Reputation: 70470

find /path/to/dir -type f -exec sed -i 's/original/replacement/g' {} \+

Or if your find doesn't support \+:

find /path/to/dir -type f -exec sed -i 's/original/replacement/g' {} \;

Upvotes: 3

Kent
Kent

Reputation: 195059

try this line:

find /aDir -type f |xargs sed -i 's/certainString/replaceWithThisNewString/g'

Upvotes: 0

Related Questions