Simple Batch Renaming or Renaming a Large Number of Similarily Named Files Very Easily

Off topic a bit, not that I’ve gone off films – I just haven’t watched any that have inspired me to write anything worth uploading in a while. Anyway, this one’s on batch renaming in Linux – specifically Fedora 18.

So, I know I’m not alone in occasionally finding myself with a directory of full of files that have a lot of similar names that for one reason or another are a hassle when being used in a specific way. For me this happens most with videos I watch via my TV. But it could just as easily relate to chat logs, music files or photo archives.

Maybe you have a lot of mp3s named Artist – Title – #.mp3 and for some reason you need to remove all the spaces for compatibility with a specific application or device. It’s a hassle to go through hundreds of tracks manually deleting every space.

Fortunately, util-linux has a utility for that! For some this is preinstalled, if not, the util-linux package should be available in your distribution’s standard repositories. If you’re unsure, it’s easy enough to find out, open a terminal session and type the following command:-

$ rename -V

I receive the following output:-

rename from util-linux 2.22.2

-V (uppercase V) is the option to return the version number of the program, -v (lowercase V) tells the program to run in ‘verbose’ mode, meaning it’ll print to the terminal anything it does – this is useful if you want to make sure you know what a program’s actually done. So my installed version of rename is from the util-linux package version 2.22.2

My first instinct when using any terminal application for the first time (and tbh, most gui apps) is to check for a man page. Manpages is one of the most useful Linux applications available, is run from the command line and usually (I believe) installed by default by most distros. The syntax to check if a program has a man page or not just type

$ man <program name>

into a free terminal session, if it does, this’ll bring the page straight up and most follow a quite logical and standard format that lists the various options available when executing a program and how to use them.

Unfortunately, rename’s manpage is a little unclear – especially if you’ve already googled and found various bash scripts to rename large numbers of files. If you have rename installed, writing a whole bash script is likely unnecessary. However, be warned, rename is both extremely powerful and also without any safeguards, it will not ask you before it starts overwriting or deleting files. Also, the manpage does not cover the full capabilities of the program, as I accidently discovered! So be careful about using wildcards (such as *) or other special characters in your arguments.

If you are intending to use any utility to automate the renaming of a large number of files, I would always encourage anyone to test it first on some ‘fake’ or unimportant data. It’s worth creating a test directory with some empty files in and mucking about with those before you start renaming files you actually care about. Chances are it’ll save you time in the long run.

So where was I? Oh yes, we have a bunch of files to rename. Let’s pretend we’re an avid photographer who’s been storing our photos on a shared computer and we’ve been using the following convention for our files:

ourname.filedescription<number of photo in series>.cameramodel.raw

So a typical photo directory might be full of files looking like this:

JohnSmith.Party001.OlympusBlah.rawJohnSmith.Party002.OlympusBlah.raw

all the way up to

JohnSmith.Party999.OlympusBlah.raw

Now, we’ve moved the files onto our own home computer, we know we were the photographer, so we no longer require the prefix and the camera model is held in the metadata anyway, so that’s also redundant. The only part of the file name we’re interested in is the Party999 section and the file extension. If we were to rename all 999 files it’d take us an hour, probably and we’d end up with very sore wrists. This is where rename comes in handy.

Rename uses the following syntax:-

rename [options] string to be changed replacement string string to select files to act on

I’ve tried to explain the command in plain english, which for me seems really confusing, the man page however, describes it like this:

rename [options] expression replacement file...

So how do we rename our photos? Well, I’d do it in 2 steps, like this:-

$ rename JohnSmith.Party Party *.raw
$ rename OlympusBlah.raw raw *.raw

The first step above tells rename to select any files that end in .raw and change any instances of JohnSmith.Party to Party. Effectively removing the JohnSmith prefix.

The second step does the same thing. Selects all files that end in .raw, removes any instance of OlympusBlah.raw and replaces it with raw. Because we’ve left the 001. part untouched, the raw extension will just be appended to that and our filenames will appear as expected:-

Party001.rawParty002.raw
Party003.raw

and so forth.

This way we ignore the part of the filename that contains the unique identifier, such as 001, 002, etc and only operate on the parts of the strings we know are constant. This is important because it removes the need to use special characters such as * and occidentally tell rename to do something unexpected. Which was a trap I fell into, and started renaming files with the filename next in the list. I don’t fully understand why rename did this, or how the command was working, so I won’t go into it here. It’s just good practice to try and operate only on known constants, where possible. Unless of course you really know what you’re doing. 😉

So anyway, with just two terminal commands we’ve renamed 999 files, there’s no way we could have done that manually without a lot of effort and time.

There are GUI applications out there that I’ve heard of that are supposed to be very powerful, while reading up I read that there’s a KDE application called Krename available. However, I can’t imagine a GUI interface being more efficient, especially when the rename utility is that simple to use.

Happy renaming!