Mass replace a string in multiple files in Linux
Recently, I needed to mass replace a string in multiple files which was a defunct URL in a website that was archived using httrack. If you are familiar with httrack, then you would know that the entire site would be saved as a static html page. So if you, for example, need to make any changes to a URL in the menu, you would need basically replace the old URL string in all of the html files that httrack has generated.
Editing it one by one was not possible for me as the total number of html pages that was generated was just under 7000 pages. It was a pretty huge site that ran for many years. If you have to know, it was TechARP.com‘s old site which is now archived at http://archive.techarp.com/.
When I did a quick search on the web how this could be done, I found a utility called RPL. Seems to do what I wanted, but after trying to use it, it just didn’t work for me. So I decided to try another method that I found, which is to both find to locate just html files and return me the file name, and then use perl to find and replace the specific string for the html file. So, I’m documenting this bit of quick research here on my site so that (1) I won’t forget how to do it and (2) for anyone else that might need to do just the same.
Command to mass replace a string
The following is the command you use.
sudo find . -name '*.' -type f -print0 | sudo xargs -0 perl -i -pe 's###g'
Example use of the command to mass replace the string default.html with http://www.techarp.com/.
sudo find . -name '*.html' -type f -print0 | sudo xargs -0 perl -i -pe 's#default.html#http://www.techarp.com/#g'
But before you go ahead and run this command, I highly recommend that you backup all the files you intend to replace, just in case. ?