(edit 12/11/2012 to add homebrew alternative)
So you you want the powerful Perl-based rename utility that comes with many Linux and *BSD distributions, but sadly not on Apple’s mighty OS X? Well so did I. (Go away you fanboys of all types. I hear not your rants about which operating system is for the win)
Thinking about it for a moment, I realize there’s nothing really stopping me from just taking the script, and thankfully it’s free software and we can pretty much do whatever we need to with it.
The rename utility is tucked away under /usr/bin/ on my Ubuntu machine. To get it to your OS X machine, it’s just a matter of copying /usr/bin/rename on a Linux box and dropping it in /usr/local/bin/ or somewhere else that’s in your PATH on your OS X box. If you’re really feeling the need for the man page, you can grab it from /usr/share/man/man1/rename.1.gz. I stuck the man page in /usr/local/share/man/man1/.
And that’s it! You can now go about using Perl regular expressions to rename multiple files and get lost in everything that is regexp.
Don’t have access to a Linux machine, you say? Oh fine. Here’s what I pulled from my Ubuntu 9.04 distro:
If you download the rename utility from here, you’ll probably need to run ‘chmod a+x’ to give execute permissions to the file after copying it to the appropriate place but otherwise things should be the same as above.
Examples
Wondering how it works? Say you’d like to rename a bunch of .wav files and prepend the phrase ‘iheartrobots’ to the front of each filename (practical, I know). Well you’d type the following:
rename -v 's/^/iheartrobots/' *.wav
The ‘-v’ parameter prints the names of the files that are being renamed. Helpful and I use it most of the time.
Or how about replacing all instances of the word ‘cat’ with the word ‘dog’ in the filename of all .rtf files in a folder? Bam.
rename -v 's/cat/dog/i' *.wav
The ‘i’ at the end of the regular expression makes the search case-insensitive.
Finally, say you had a bunch of files that looked like this: Waltzing.With.Famous.People.S01E09.HDTV.XviD-SYS.[bztv].avi
And you wanted to get rid of everything between S01E09 and .avi, you could use the following:
rename -v 's/HDTV.*avi/avi/i' *.avi
The Homebrew Alternative
Now, thanks to Homebrew, you can skip all this nonsense and just do:
brew install rename
If you’re not using Homebrew, you should be.
More Information
If you’d like to get more information on the rename utility described here, the most obvious place is the man page (man rename) or online here.
Additionally, since it’s a Perl utility, more information on the Perl regular expression pattern-matching language can be found straight from the horse’s mouth. Regular expressions are a madness unto themselves, but once you get the hang of it, most powerful stuff.
3 replies on “Linux ‘rename’ utility on OS X”
Just what I needed, thank you! Also, thanks for the direct file downloads.
-Joel
Nothing to do with Linux, Larry & Randall , source: chapter 18.10 of Unix Power Tools
Excellent! Thanks a lot for the binaries!
Been searching for this for a while now, you made my day!