Recursive Search and Replace with Perl
Few days ago, I was trying to search and replace a string in bunch of files nested in many subdirectories. Then I remembered, I can code. I wrote a script to do this for me, and I called it RName.
Actually I’m not planning to release the code. Few hours ago, I changed my mind. The code is now freely available at github. It takes 3 arguments, the string to search, the string to be replaced, and lastly the directory. It still needs a lot of changes and features, one of the reasons I released it. And to think of it, this is just a simple script. But when worked out, this could be one big project. I don’t assume anyway, we’ll see if other devs will fork it.
Coding it is simple, but when I decided to release it, I have to formalize. I separated the sub functions and placed it on a module. That’s the first time I did this by the way. I also refined my main script’s structure, which I will be using in my future scripts. Here’s what my basic skeleton script in Perl looks like:
#!/usr/bin/perl
use strict;
use warnings;
{
# ... main code
}
__END__
I also included exts.txt which holds all the valid extensions that the script can edit. It’s currently been separated by newlines.
I wish to develop more scripts, and hopefully my range will grow from simple projects to complex ones. That’s it for now, and just leave comments here if you have something to say.