Most people are familiar with using the mv (move) command to rename a file in Linux. Often times this is sufficient and gets the job done. As with any work, you need to have the right tool for the job. In this article we will explore the rename command.

You might be wondering why we need two commands to rename a file. Well, the mv command has some limitations when it comes to renaming files, we will touch on those later in this article. To make things more convoluted, there are several different versions of the rename command. Which one you are using depends mostly on what distribution you run. In the next few minutes we will try to clear all this up. So sit back, grab some refreshments and bear with me while I try to demystify the rename command. Alternatively, you can skip right to "How to use the rename command".

rename Command vs mv Command

Most people know that it is possible to rename a file with the mv command. However, it has limitations.

The most glaring limitation of the mv command, as it pertains to renaming files, is you can only rename a single file. You cannot use it to rename multiple files without running it through a loop of some kind.

$ ls -l
 -rw-rw-r--. 1 savona savona 0 May 20 19:55 1.txt
 -rw-rw-r--. 1 savona savona 0 May 20 19:55 2.txt
 -rw-rw-r--. 1 savona savona 0 May 20 19:55 3.txt
 -rw-rw-r--. 1 savona savona 0 May 20 19:55 4.txt

 $ mv *.txt *.sql
 mv: target '*.sql' is not a directory

Let's create a for loop to rename all the .txt files to .sql:

$ for i in *; do mv -v "$i" "${i%.txt}.sql"; done
 '1.txt' -> '1.sql'
 '2.txt' -> '2.sql'
 '3.txt' -> '3.sql'
 '4.txt' -> '4.sql'

Simple right? Not so much. This is where the rename command comes in. Let's use the same example, but this time use the rename command.

$ rename -v txt sql *.txt
 1.txt' ->1.sql'
 2.txt' ->2.sql'
 3.txt' ->3.sql'
 4.txt' ->4.sql'

Or, if you using the OTHER rename...

$ rename -v s/txt/sql/ *.txt
 1.txt renamed as 1.sql
 2.txt renamed as 2.sql
 3.txt renamed as 3.sql
 4.txt renamed as 4.sql

Ahh, that's better. But now you're probably wondering what is the "other" rename and how do I know which one I am using? In the next section we discuss the two different rename commands and how to switch between the two.

The Rename Command(s) Explained

As we mentioned above there are two main versions of rename running around in the wild. Which one is the default for your system depends largely on which Linux distribution you are running. Below we will try to explain the differences and show you how to use the file command to figure out which one you are using.

Rename from util-linux package

The rename utility that comes with util-linux package is pre-installed and default on most rpm based Linux distributions like CentOS, Red Hat, and Fedora. It can change multiple files (as seen above) but does not support the use of regular expressions.

You can use the following commands to identify the location of the which command, check if it is a symlink, and find out what type of file it is.

$ which rename
 /usr/bin/rename

$ ls -l /usr/bin/rename 
 -rwxr-xr-x. 1 root root 27256 Apr  9 10:15 /usr/bin/rename

$ file /usr/bin/rename
 /usr/bin/rename: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=bfe3c03e21d01c102f19c7797340cb3be8e319dd, stripped

Rename - The Perl Script

The rename that is used by default in Debian based distributions, like Ubuntu and Linux Mint, is actually a perl script. As a result it supports the use of perl style regular expressions. In my opinion, this makes it more robust than the util-linux version. Especially if you are familiar with perl regular expressions.

You can use the the following commands to identify which rename is being used on your Debian based system.

$ which rename
 /usr/bin/rename

$ ls -l /usr/bin/rename
 lrwxrwxrwx 1 root root 24 May 19 09:06 /usr/bin/rename -> /etc/alternatives/rename

$ ls -l /etc/alternatives/rename
 lrwxrwxrwx 1 root root 20 May 19 09:06 /etc/alternatives/rename -> /usr/bin/file-rename

$ file /usr/bin/file-rename 
 /usr/bin/file-rename: Perl script text executable

Picking Your Rename Poison

Luckily this is Linux, so you have options. Debian based systems like Ubuntu also come with the util-linux version installed. It can be found at /usr/bin/rename.ul and easily made to be default. If you are completely unfamiliar with regular expressions and want to use the util-linux version, simply change the target of the rename symlink.

sudo ln -fns /usr/bin/rename.ul /usr/bin/rename

If you are on a Red Hat based distribution (CentOS, Fedora, etc) and want to use the perl script it's just as easy. Just install the prename package with yum or dnf.

sudo yum install prename

Once it's installed you can use prename by invoking it directly or set an alias in ~/.bashrc so when you type rename it calls the prename perl script instead of the rename utility.

alias rename='prename'

How to Use the Rename Command(s)

Since there are two rename commands, we will have to split this section and touch on both.

Using the Rename Utility from util-linux

As we stated above, the util-linux rename utility does not support the use of regular expressions. You can only use basic strings as "expressions" to be changed.

To rename multiple files, you need to find some commonality between the filenames so you can address them as an argument. For example, you can rename all files with a common extension using a wildcard ( *.txt). Or you can use globbing to match files with similar names.

In the first example, we will change all files with the .sql extension to .txt.

$ rename -v sql txt *.sql
 a.sql' ->a.txt'
 b.sql' ->b.txt'
 c.sql' ->c.txt'

In this next example, we can get a little creative and change three files by adding 'er' to the end of their names. Since we cannot use regular expressions we find a commonality (all ending in p) in the file and use it to our advantage. Instead of adding "er" we take the last letter (p) and change it to per, thus changing the names of all the files. We also use the question mark wildcard (matches any character) to select the files we want to change.

$ ls -l
 -rw-rw-r--. 1 savona savona 0 May 23 09:05 camp
 -rw-rw-r--. 1 savona savona 0 May 23 09:06 tamp
 -rw-rw-r--. 1 savona savona 0 May 23 09:06 damp

$ rename -v p per ?amp
 camp' ->camper'
 damp' ->damper'
 tamp' ->tamper'

Although the rename command doesn't support regular expressions or globbing in it's expressions, with a little creativity and thinking outside the box, it can be a useful tool.

Verbose Output - Print Each Action

As you see above, we used the -v option so rename will print each action it takes. By default, rename just does the work without showing you the output of each action.

Testing rename Command

You can use the -n ( --no-act ) option to test run the rename on multiple files without actually making changes. This is most useful when used in conjunction with the -v ( --verbose) option.

$ rename -vn txt sql *.txt
 1.txt' ->1.sql'
 2.txt' ->2.sql'
 3.txt' ->3.sql'
 4.txt' ->4.sql'

$ ll
 total 0
 -rw-rw-r--. 1 savona savona 0 May 23 11:08 1.txt
 -rw-rw-r--. 1 savona savona 0 May 23 11:08 2.txt
 -rw-rw-r--. 1 savona savona 0 May 23 11:08 3.txt
 -rw-rw-r--. 1 savona savona 0 May 23 11:08 4.txt

As you can see above, the -n option did not actually make the changes. It just showed us what the changes would be.

Using the Rename Perl Script

The rename perl script allows for the use of perl style regular expressions. In my opinion this makes it a lot more flexible that the binary version in util-linux. We will show you some examples of how to use it, but explaining regular expressions in general is outside the scope of this article. You will find links to resources at the end of this post if you are interested in learning more.

A regular expression, regex or regexp (sometimes called a rational expression) is a sequence of characters that define a search pattern.

- Wikipedia - Regular Expressions

Using a regex we can do a lot more complex substitutions with the rename command. Here are some simple examples.

In this example, we will change the filename from lowercase to capital letters.

$ rename -v y/a-z/A-Z/ *
 camper renamed as CAMPER
 damper renamed as DAMPER
 tamper renamed as TAMPER

In perl the y function is similar to the tr command.

We can also use substitution ( s ), for example here we are changing "er" in the filenames to "ing".

$ rename -v s/er/ing/g *
 camper renamed as camping
 damper renamed as damping
 tamper renamed as tamping

Testing rename Expressions

The -n ( -nono / No action) option is available to the perl script as well. If you want to ensure your expressions are going to match what you expect, you can pass the -n option to see the changes in standard output without making actual changes.

$ rename -n s/txt/sql/ *.txt
 rename(1.txt, 1.sql)
 rename(2.txt, 2.sql)
 rename(3.txt, 3.sql)
 rename(4.txt, 4.sql

Conclusion

In this article we discussed the multiple versions of the rename command, how to find which one you are using and how each functions. You would think that something and simple and renaming files in Linux wouldn't need so much discussion. We thought it was important (and interesting) to cover all this information so you know your options.

If you have any thing to add we would love to hear it. Just drop a comment below and start the discussion!

Learning Resources