Prename: A Better Rename

In this tutorial, I quickly run through how to get a better version of the rename utility in perl and use it to rename a series of files that have the same string that needs to be replaced.

The Problem:

I have a set of three files that need to have their names changed from having an “f” in them to a “br”. These files are:

f_dang.connect
f_dang.names
f_dang.paircoeffs

The Trivial Solution:

Obviously, with three files three simple mv commands would suffice; however, I was wondering what I would do if I needed to rename 100 files.

The Slightly-Less (But Still Pretty) Trivial Solution:

This is where I fell into the proverbial rabbit hole. On our HPC system we only had the regular linux rename utility, which is great when you have files named foo1, foo2, and foo3 and you need to make them foo001, foo002, and foo003 — but not great for a situation such as ours.

That is where prename comes in: prename is the perl version of the rename utility. It allows for a sed-like find replace interface for filenames. Better yet, it is possible to use the script without needing superuser access.

I downloaded the perl script (available as a gist here:). As a gist, this is available via a simple clone command.

Then, set your path environment variable to include the folder where you downloaded the perl script to. I made a folder called “prename”. This can be done by adding the following to your bashrc,

export PATH="~/prename:$PATH"

Once this is done, give the perl script execute permissions.

chmod +x prename.pl

Then you are good to go! Renaming is as simple as:

prename.pl 's/f_/br_/g' *