user251395
user251395

Reputation: 73

Find string inside files and replace with a new string

I need to find all files containing certain text inside my project directory. This includes sub-directories.

I've managed to find all the files:

find . -type f -exec grep -H 'Rename' {} \;

enter image description here

Now I need to replace the keyword "Rename" with "XYZ" leaving the rest of text in each file intact.

Ideas?

Upvotes: 0

Views: 33

Answers (1)

Paul Hodges
Paul Hodges

Reputation: 15273

sed instead of grep.

find . -type f -exec sed -i 's/Rename/XYZ/g' {} \;

grep already scans all the lines of every file, so you aren't losing anything. This just makes the change when it finds it, instead of printing out the line.

Upvotes: 2

Related Questions