0

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?

3 Answers 3

3
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' {} \;
Sign up to request clarification or add additional context in comments.

Comments

0

try this line:

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

1 Comment

Ḧm, if going the xargs route, I'd use find .... -print0 | xargs -0 ... just to be safe.
0

Using :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.