How to I find and replace every occurrence of:
subdomainA.example.com
with
subdomainB.example.com
in every text file under the /home/www/
directory tree (recursive find/replace).
How to I find and replace every occurrence of:
subdomainA.example.com
with
subdomainB.example.com
in every text file under the /home/www/
directory tree (recursive find/replace).
cd /home/www && find . -type f -print0 |
xargs -0 perl -i.bak -pe 's/subdomainA\.example\.com/subdomainB.example.com/g'
cd /home/www
find . -type f -print0 | xargs -0 sed -i 's/subdomainA.example.com/subdomainB.example.com/g'
UPD.
-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 you files can contain blanks or other special character. It is recommended to use the -print0 argument to find if you use -exec command or xargs (the -0 argument is needed in xargs.).
find /home/www/ -type f -exec perl -i.bak -pe 's/subdomainA\.example\.com/subdomainB.example.com/g' {} +
find /home/www/ -type f will list all files in /home/www/ (and its subdirectories). The "-exec" flag tells find to run the following command on each file found.
perl -i.bak -pe 's/subdomainA.example.com/subdomainB.example.com/g' {} +
is the command run on the files (many at a time). The "{}" gets replaced by file names. The + at the end of the command tells find to build 1 command for many filenames. From the find man page: "The command line is built in much the same way that xargs builds its command lines."
Thus it's possible to achieve your goal without using xargs -0, or -print0 . .
find /home/www/ -type f -exec \
sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g' {} +
Compared to other answers here, this is simpler than most and uses sed instead of perl, which is what the original question asked for.
muxh more simple way is to use the below on the command line
find /home/www/ -type f|xargs perl -pi -e 's/subdomainA\.example\.com/subdomainB.example.com/g'
You may also:
Search & replace with find & ed
http://codesnippets.joyent.com/posts/show/2299
(which also features a test mode via -t flag)