Code I Like: Batch Subversion Rename (Replace Underscore with Hyphen), Bash Script

That’s an unwieldy title, if I ever saw (wrote?) one. Still it describes the code in question exactly, so unwieldy will have to do for this post.

Anyway, for SEO purposes I’ve wanted to rename some of my files from underscore delineated (_) to hyphen delineated (-) for a couple of years now. I chose wrong when I originally launched this site, and since it’s huge (something like 400 static pages), I never wanted to actually go through with the renaming. And that was before I got the site into Subversion. With Subversion in place I couldn’t even use one of the many little file renaming apps out there. I’d have to do the work within SVN or else things would be bad. Very bad.

A disheartening problem.

Well, tonight I decided I was going to attack the problem head on. Thinking about it for a few minutes I realized that there must be a relatively easy way to automate it with Bash and the Subversion command line, I just had to figure it out.

Two hours later I’m glad to report success. While I still need to do some 301 redirections and need to rewrite some links, the lion’s share of the work on this project is already done.

To celebrate, I figured I’d share the Bash script in question. My solution is based on the code found here. Tweaking it for SVN, creating in my particular find and replace, and adding the global flag, I came up with this

for i in 'find *.php';
     do    svn mv $i 'echo $i | sed s/_/-/g';
done

How does that work? The first line:

for i in 'find *.php';

simply loops through the result of the command find *.php. In my case that returns a list of the targeted PHP files in the working directory.

The second line is a little more complicated

do    svn mv $i 'echo $i | sed s/_/-/g';

svn mv, is the Subversion rename command. It takes two arguments- the source file and the destination. In this case we use the variable $i as the first argument (it being one of the PHP files found by find) and the result of another compound Bash command as the second argument.

echo $i | sed s/_/-/g

That uses echo to pipe (|) the variable $i into sed.

Sed is where the find and replace happens. This particular usage is broken down like this

  • s indicates that a substitution should occur
  • the search /_/-/ is structured like /RegExp Search Pattern/Replacement String/
  • g is the global flag, indicating that all instances should be replaced throughout the string.

And there you have it. Please feel free to point out anything that might be improved with the above code (or any code on my site, really.) I’m a dangerous amateur when it comes to Bash scripting, so I’m always to improve.

Posted in Web

One thought on “Code I Like: Batch Subversion Rename (Replace Underscore with Hyphen), Bash Script

  1. Awesome post. I had to use back ticks ( ` ) to get this to work on my OSX terminal. I was trying to remove the twi extension from some partials and ended up with this:

    for i in `find *.twig`; do svn mv $i `echo $i | sed s/.twig//g`; done

Leave a Reply

Your email address will not be published. Required fields are marked *