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?
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?
Note: Do not run this command on a folder including a git repo - changes to .git could corrupt your git index.
Compared to other answers here, this is simpler than most and uses sed instead of perl, which is what the original question asked for.
If you do not mind using
vim
together withgrep
orfind
tools, you could follow up the answer given by user Gert in this link --> How to do a text replacement in a big folder hierarchy?.Here's the deal:
recursively grep for the string that you want to replace in a certain path, and take only the complete path of the matching file. (that would be the
$(grep 'string' 'pathname' -Rl)
.(optional) if you want to make a pre-backup of those files on centralized directory maybe you can use this also:
cp -iv $(grep 'string' 'pathname' -Rl) 'centralized-directory-pathname'
after that you can edit/replace at will in
vim
following a scheme similar to the one provided on the link given::bufdo %s#string#replacement#gc | update
This is the best all around solution I've found for OSX and Windows (msys2). Should work with anything that can get the gnu version of sed. Skips the .git directories so it won't corrupt your checksums.
On mac, just install coreutils first and ensure gsed is in the path -
Then I stick this function in my zshrc/bashrc ->
The simplest way for me is
For Qshell (qsh) on IBMi, not bash as tagged by OP.
Limitations of qsh commands:
Thus the solution in qsh:
Caveats: