How to do a recursive find/replace of a string with awk or sed?


Question

How do I find and replace every occurrence of:

subdomainA.example.com

with

subdomainB.example.com

in every text file under the /home/www/ directory tree recursively?

1
618
5/12/2018 3:36:07 PM

find /home/www \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g'

From man find:

-print0 (GNU find only) tells find to use the null character (\0) instead of whitespace as the output delimiter between pathnames found. This is a safer option if your files can contain blanks or other special characters. It is recommended to use the -print0 argument to find if you use -exec <command> or xargs (the -0 argument is needed in xargs.)

796
6/6/2019 3:09:04 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon