Posted March 7, 2013 in Mac, OSX, Terminal

Replacing Spaces In File Names On OSX

Problem: we need to change the naming conventions for an entire directory of files.
I recently ran into a situation where I needed to remove spaces from the names of about 200 files and replace them with hyphens. There are several ways to accomplish this, but if you don’t want to brush up on your PERL or bother with Automator, you can easily use OSX Terminal to run a sed command.

We started with a directory of files named like this:

2003 Don Cherry.JPG
2003 Larry Young Mint.JPG
2007 Commodity Jovontae.JPG
Brad The Mack.JPG
Polyphonic Raul.JPG

Since URLs don’t technically allow spaces, spaced filenames can cause encoding / decoding issues amongst the various browsers, which usually replace the spaces with the ASCII code %20. To avoid this mess, we’d rather have something like this:

2003-Don-Cherry.JPG
2003-Larry-Young-Mint.JPG
2007-Commodity-Jovontae.JPG
Brad-The-Mack.JPG
Polyphonic-Raul.JPG

To solve this problem on Mac OS (and we’re assuming the reader doesn’t have any command line experience), place the folder of files on your Desktop and make a backup of your files, because there is no “undo” on the command line. In our case, we’ll say the folder is named Photos. Once you’ve got Photos backed up, navigate to Go -> Utilities, and double-click Terminal. This will open up a fresh Terminal window (a.k.a. shell). Then type cd ~/Desktop && ls and press return. Note that first character is a tilda, not a hyphen.

The cd ~/Desktop part of the command takes us to the Desktop, and ls lists all files and directories on that level. If everything went well, you should see the contents of your Desktop listed in the Terminal window. Next, type cd Photos and press return again. This takes you into the Photos directory, and now we’re ready to do the heavy lifting. Note that the following command will only replace spaces with hyphens. If you need a different substitution you’ll have to modify the command accordingly.

In the Terminal window, type for i in *.JPG; do mv "$i" "`echo $i | sed -e 's, ,-,g'`"; done and then press return. Pay special attention to the difference between the backticks and apostrophes. Also note that the .JPG file extension is uppercase in our situation (in yours it might not be). In plain English, this command reduces to something like, “For every file in the current directory with the extension .JPG, replace all spaces with hyphens.” That’s basically it.

The question might arise, “Why not use underscores instead?” If you wanted underscores or any other character instead of a hyphen, you’d simply switch the s, ,-,g part of the above command to s, ,_,g. If for some reason you wanted an X instead of an underscore, then just switch the s, ,_,g part of the above command to s, ,X,g, and hit return. Make sense?

For some background discussion on the SEO implications of hyphens vs. underscores, check out the following video from Google’s Matt Cutts. In short, hyphens currently have a minor ranking advantage over underscores, but nothing worth recoding existing URLs over.

Happy coding!

Got Something To Say:

Your email address will not be published.