Heal Your Church WebSite


Teaching, rebuking, correcting & training in righteous web design.

Global replace using find & xarg

I’m part of an lively Yahoo group run by Tony Whittaker called ChurchSite-Chat.
There, I learned about Rachel Cunliffe who runs a very cool bbs that offers Church Website Critiques. And from both sites, I’ve learned just how big of a plank I have to yank out of my own eye before I can go about piling heavy burdens burdens upon the shoulders of other church web servants.

One of the problems pointed out to me were some broken links on my own church’s page, RedlandBaptist.org. I originally built this site using DreamWeaver 3, and being the code-monkey type of guy I am, relied heavily on server-side includes (SSI) to reduce my work. As I’ve stated in an earlier article, SSI’s can save you beaucoup time because you can write a file once, and include it often. Similarly, using DreamWeaver, you can create stock templates so all your pages look and feel the same.

There’s only one big fat caveat with this approach …

… if something breaks in your template or your SSI’s – then it breaks everywhere. Not so bad in the case of the SSI, it’s only one file to fix, but in the case of the template, not so much a problem, until 2 or 3 years pass and you no longer have DreamWeaver nor the templates on your PC!

Fortunately, I organized all my template-driven pages in a subdirectory called /pages. And all the files possess the same file extension – .shtml. So the trick is finding a way to globally search and replace all the files for the error. Now there are several ways to skin this cat. One approach is to FTP all the files down to my PC and use an ascii or html editor, preferably one with that can effect changes to an entire subdirectory or file extension, and employ regular expressions.

Yeah, okay, I said that dirty word that freaks so many of you out, but regular expressions can be your friend if you’re willing to take the time to learn them. Moreover, rather than FTP, my solution was to SSH onto the server itself, and use one of the numerous text editing features found on Linux. Here’s what I did …

After logging in, I changed directory to the target subdir and before ANYTHING ELSE, I backed-up all the target content using the following commands:

cd $HOME/www/pages
tar -zcvf mypages.tar.gz *.shtml

Afterwards, I copied one of the target files to a test file, issued my global replace command. Good thing to, I had a typo that would have mangled everything. After creating another test subject, I issued the command correctly, tested the test page, then once I was satisfied with the results, modified the command to change all the target files. Here are the commands in order:

cp mypage.shtml testpage.shtml
find $HOME/www/pages -name “test.shtml” |
     xargs perl -pi -e ‘s/(href=”)(\/tracks)/$1\/pages$2/gi’;
echo “
At this point, use your browser to test test.shtml”

find $HOME/www/pages -name “*.shtml” |
     xargs perl -pi -e ‘s/(href=”)(\/tracks)/$1\/pages$2/gi’;
echo “
At this point, use your browser to test everthing else”

Please Note, the lines beginning with “xargs” are actually line-wrapped from the line above.
WARNING * use at your own risk, proceed with caution * you are playing with fire here * this stuff will blow your foot clean off! Please read my Terms of Use before proceeding. I cannot and will not accept any responsibility for damages to your dat aor systems.

Scary stuff aside, find and xarg can be combined to change ALL your files in ALL your directories or a particular subdirectory tree with one simple command. Done right, it’s fast and efficient and keeps me from having to FTP my brains out.

For those of you are brave enough and savvy enough to give this a shot, I suggest practicing somewhwere where it doesn’t count. Create a new subdirectory and copy and practice your commands there. And if you do happen to mangle things even after tesing the first time around … you can always

tar -zcvf mypages.tar.gz *.shtml

Comments are closed.