How to use the rename command in Linux

Contents

Linux laptop showing a bash prompt

Get acquainted with the file renaming power of the Linux world and provide mv—And yourself— a break. Rename it is flexible, fast and, sometimes, even easier. Here is a tutorial of this powerful command.

What about mv?

There is nothing wrong with mv . The command does a good job, and it is found in all Linux distributions, on macOS and other Unix-like operating systems. This is why it is always enabled. But every now and then you just need a bulldozer, not a shovel.

the mv The command has a purpose in life and it is to move files. It is a happy side effect that can be used to move an existing file within a new file, with a new name. The net effect is to rename the file, so we get what we want. But mv not a dedicated file renaming tool.

Rename a single file with mv

Use mv to rename a file type mv, a space, the file name, a space and the new name you want the file to have. Then press Enter.

You can use ls to check that the file name has been changed.

mv oldfile.txt newfile.txt
ls *.txt

Rename multiple files with mv

Things get complicated when you want to rename multiple files. mv has no ability to rename multiple files. You must resort to using some nifty bash tricks. That's fine if you know of any mid-grade command line fu, but the complexity of renaming multiple files with mv contrasts with ease of use mv to rename a single file.

Things escalate quickly.

Let's say we have a directory with a range of files, of different kinds. some of these files have an extension “.Prog”. we want to rename them on the command line so that they have an extension “.prg”.

How do we fight? mv to do that for us? Let's take a look at the archives.

ls * .prog -l

Here's a way to do it that doesn't resort to writing an actual bash script file.

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"

It worked? Let's go through the files and see.

ls *.pr*

Then, Yes, it worked. Now they are all files “.prg” and no files “.Prog” in the directory.

What just happened?

What did that long command really do? Let's analyze it.

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"

the first part starts a cycle that will process each file “.Prog” in the directory, at the same time.

The next part tells what the processing will do. do. Is using mv to move each file to a new file. the new file will be named with the name of the original file excluding the part “.Prog”. Instead, a new extension of “.prg”.

There must be an easier way

Definitely. Is he rename command.

rename not part of a standard Linux distribution, so you will need to install it. It also has a different name in different Linux families, but they all work the same way. You just have to substitute the appropriate command name according to the type of Linux you are using.

on Ubuntu and Debian derived distributions that I install rename like this:

sudo apt-get install rename

In distributions derived from Fedora and RedHat, install prename like this. Please note the “p” initial, which means Perl.

sudo dnf install prename

To install it on Manjaro Linux use the following command. Note that the rename command is called perl-rename.

sudo pacman -Syu perl-rename

Let's do it again

And this time we will use rename. We will turn back the clock so that we have a set of files “.Prog”.

ls * .prog

Now let's use the following command to rename them. Later we will check it with ls yes it worked. Remember to substitute rename with the appropriate command name for your Linux if you are not using Ubuntu or a Debian derived Linux.

rename 's/.prog/.prg/' *.prog
ls *.pr*

That worked, now they are all files “.prg” and no files left “.Prog” in the directory.

What happened at this time?

Let's explain that little bit of magic, in three parts.

the first part is the name of the command, rename (O prename O perl-rename , for the other distributions).

the latest part is *.prog, what does it say rename to trade on all files “.Prog”.

the half part sets the work we want to do on each filename. the s means substitute. The first term (.prog) what is it rename will search each filename and second term (.prg) is what will be replaced.

The central part of the command, or central expression, is a Perl ‘regular phrase‘And is what gives the rename master your flexibility.

Change other parts of a file name

We have changed the filename extensions so far, let's modify other parts of the file names.

There are many C source files in the directory. All file names have the prefix “slang_”. We can check this with ls.

ls sl*.c

We are going to replace all occurrences of “slang_” with “sl_”. The format of the command is already familiar to us. We are only changing the search term, the replacement term and file type.

rename 's/slang_/sl_' *.c

This time we are looking for files “.c” and we look for “slang_”. Whenever you meet “slang_” in a file name, is replaced by “sl_”.

We can check the result of that command by repeating the ls command from above with the same parameters:

ls sl*.c

Delete part of a file name

We can delete a part of a filename by replacing the search term with nothing.

ls *.c
rename 's/sl_//' *.c
ls *.c

We can see from the ls command that all our files “.c” are preceded by “sl_”. Let's get rid of it completely.

the rename The command follows the same format as before. We will search for files “.c”. The search term is “sl_”, but there is no substitution term. Two backslashes with nothing between them mean nothing, an empty string.

rename will process each file “.c” at the same time. Seek “sl_” in the file name. If found, will not be replaced by anything. In other words, search term is removed.

The second use of ls The command confirms that the prefix “sl_” has been deleted from all files “.c”.

Limit changes to specific parts of file names

Let's use ls to view files that have the string “Param” in your file name. Then we will use rename to replace that string with the string “parameter”. we will use ls one more time to see the effect rename command had in those files.

ls * param *
rename 's/param/parameter' *.c
ls * param *

there are four files that have “Param” in your file name. param.c, param_one.c and param_two.c available “Param” at start of his name. third_param.c has “Param” at end of his name, just before the extension.

the rename The command will search “Param” everywhere in the file name and replace it with “parameter” in all cases.

The second use of ls The command shows us that that is exactly what has happened. And “Param” was at the beginning or end of the file name, has been replaced by “parameter”.

We can use Perl metacharacters to refine the behavior of the medium expression. Metacharacters are symbols that represent positions or sequences of characters. As an example, ^ it means “beginning of a string”, $ it means “end of a chain” and . means any single character (apart from a newline character).

We are going to use the start of the string metacharacter ( ^ ) to narrow our search to the beginning of the file names.

ls *stop*.c
rename 's/^parameter/value/' *.c
ls *stop*.c
ls value*.c

The files that we renamed previously are listed and we can see that the string “parameter” is at the beginning of three file names and at the end of one of the file names.

Our rename the command uses the beginning of the line (^) metacharacter before search term “parameter”. This says rename to consider the search term found only if it is at the beginning of the file name. The “parameter” of the search string will be ignored if it is located elsewhere in the file name.

Checking with ls, we can see that the file name it had “parameter” at end the file name has not been modified, but the three file names they had “parameter” at start of their names the search string has been replaced by the substitute term “value”.

The power of rename lies in the power of Perl. All the power of Perl is at your disposal.

Search with clusters

rename has even more tricks up its sleeve. Let's consider the case where you can have files with similar strings in their names. They are not exactly the same chains, so a simple search and replace will not work here.

In this example we use ls to check which files we have that begin with “str”. There are two of them, string.cy strangle.c. We can rename both strings at the same time using a technique called grouping.

The central expression of this rename the command will search for strings within the file names that have the character sequence “Stri” O “Stra” where those sequences are immediately followed by “ng”. In other words, our search term will search “chain” and “Strangle”. The substitution term is “Bang”.

ls str*.c
rename 's/(stri|stra)of/bang/' *.c
ls you*.c

Using ls a second time confirms that string.c has become bang.c and strangle.c is now bangle.c.

Use of translations with name change

the rename The command can perform actions on filenames called translations. A simple example of a translation would be to force a set of filenames to uppercase.

At rename command below note that we are not using one s/ to start the central expression, we are using y/. This says rename we are not making a substitution; we are doing a translation.

the a-z term is a Perl expression that means all lowercase characters in the sequence from a to z. In the same way, the A-Z term represents all capital letters in the sequence from A to Z.

the central expression of this command could be paraphrased as “if any of the lowercase letters from a to z are in the file name, replace them with the corresponding characters in the uppercase character sequence from A to Z”.

To force file names for all files “.prg” to capital letters, use this command:

rename 'y / the / AZ /’ * .prg

ls * .PRG

the ls The command shows us that all file names “.prg” are now in capital letters. Actually, to be strictly precise, are no longer files “.prg”. They are files “.PRG”. Linux is case sensitive.

We can reverse that last command by reversing the position of the a-z and A-Z terms in the central expression.

rename 'y / THE / the /’ * .PRG

ls * .prg

You don't learn Perl in five minutes

Getting familiar with Perl is time well spent. But to start using the time-saving capabilities of the rename command, you don't need a lot of Perl knowledge to get great potential benefits, simplicity and time.

setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.