Use the rename Command on Linux - - Windows Tips and Tricks with Geek

Wednesday, January 12, 2022

Use the rename Command on Linux


Linux laptop showing a bash prompt

Renaming a Single File With mv

To use mv to rename a file type mv, a space, the name of the file, a space, and the new name you wish the file to have. Then press Enter.

You can use ls to check the file has been renamed.

mv oldfile.txt newfile.txt
ls *.txt

Renaming Multiple Files with mv

Things get trickier when you want to rename multiple files. mv has no capability to deal with renaming multiple files. You must resort to using some nifty Bash tricks. That’s fine if you know some medium-grade command-line fu, but the complexity of renaming multiple files with mv stands in stark contrast to the ease of using mv to rename a single file.

Things escalate quickly.

Let’s say we’ve got a directory with a variety of files in it, of differing types. Some of these files have a “.prog” extension. We want to rename them at the command line so that they have a “.prg” extension.

How do we wrangle mv into doing that for us? Let’s take a look at the files.

ls *.prog -l

Here’s one 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"

DId that work? Let’s check the files and see.

ls *.pr*

So, yes, it worked. They’re all “.prg” files now, and there are no “.prog” files in the directory.

What Just Happened?

What did that long command actually do? Let’s break it down.

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

The first part starts a loop that is going to process each “.prog” file in the directory, in turn.

The next part says what the processing will do. It is using mv to move each file to a new file. The new file is going to be named with the original file’s name excluding the  “.prog” part. A new extension of “.prg” will be used instead.

There Has to be a Simpler Way

Most definitely. It is the rename command.

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

in Ubuntu and Debian-derived distributions you install rename like this:

sudo apt-get install rename

In Fedora and RedHat-derived distributions you install prename like this. Note the initial “p,” which stands for Perl.

sudo dnf install prename

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

sudo pacman -Syu perl-rename

Let’s Do That Again

And this time we’ll use rename. We’ll roll back the clock so that we have a set of “.prog” files.

ls *.prog

Now let’s use the following command to rename them. We’ll then check with ls whether it worked. Remember to substitute rename with the appropriate command name for your Linux if you’re not using Ubuntu or a Debian-derived Linux.

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

That worked, they’re now all “.prg” files, and there are no “.prog” files left in the directory.

What Happened This TIme?

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

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

The last part is *.prog, which tells rename to operate on all “.prog” files.

The middle part defines the work we want to be done on each filename. The s means substitute. The first term (.prog) is what rename will search for in each filename and the second term (.prg)  is what it will be substituted with.

The middle part of the command, or central expression, is a Perl ‘regular expression‘ and it is what gives the rename command its flexibility.

Changing Other Parts of a Filename

We’ve changed filename extensions so far, let’s amend other parts of the filenames.

In the directory are a lot of C source code files. All of the filenames are prefixed with “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’re just changing the search term, the replacement term, and the file type.

rename 's/slang_/sl_' *.c

This time we are looking for “.c” files, and searching for “slang_”. Whenever “slang_” is found in a filename it is substituted with “sl_”.

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

ls sl*.c

Deleting Part of a Filename

We can remove a part of a filename by substituting the search term with nothing.

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

We can see from the ls command that our “.c” files are all prepended with “sl_”. Let’s get rid of that altogether.

The rename command follows the same format as before. We’re going to be looking for “.c” files. The search term is “sl_”, but there is no substitution term. Two backslashes without anything between them means nothing, an empty string.

rename will process each “.c” file in turn. It will search for “sl_” in the filename. If it is found, it will be replaced by nothing. In other words, the search term is deleted.

The second use of the ls command confirms that the “sl_” prefix has been removed from every “.c” file.

Limit Changes to Specific Parts of Filenames

Let’s use ls to look at files that have the string “param” in their filename. Then we’ll use rename to replace that string with the string “parameter”. We’ll use ls once more to see the effect the rename command has had on those files.

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

Four files are found that have “param” in their filename. param.c, param_one.c, and param_two.c all have “param” at the start of their name. third_param.c has “param” at the end of its name, just before the extension.

The rename command is going to search for “param” everywhere in the filename, and replace it with “parameter” in all cases.

The second use of the ls command shows us that that is exactly what has happened. Whether “param” was at the start or at the end of the filename, it has been replaced by “parameter.”

We can use Perl’s metacharacters to refine the behavior of the middle expression. Metacharacters are symbols that represent positions or sequences of characters. For example, ^ means “start of a string,” $ means “end of a string,” and . means any single character (apart from a newline character).

We’re going to use the start of string metacharacter ( ^ ) to restrict our search to the start of the filenames.

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

The files we renamed earlier are listed, and we can see the string “parameter” is at the start of three filenames and it is at the end of one of the filenames.

Our rename command uses the start of line (^)  metacharacter before the search term “parameter.” This tells rename to only consider the search term to have been found if it is at the start of the filename. The search string “parameter” will be ignored if it is anywhere else in the filename.

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

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

Searching With Groupings

rename has yet more tricks up its sleeve. Let’s consider the case where you might have files with similar strings in their names. They’re not exactly the same strings, so a simple search and substitution won’t work here.

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

The central expression of this rename command will search for strings within filenames that have the character sequence “stri” or “stra” where those sequences are immediately followed by “ng”. In other words, our search term is going to look for “string” and “strang”. The substitution term is “bang”.

ls str*.c
rename 's/(stri|stra)ng/bang/' *.c
ls ban*.c

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

Using Translations With rename

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

In the rename command below notice that we’re not using an s/ to start the central expression, we’re using y/. This tells rename we’re not performing a substitution; we’re performing a translation.

The a-z term is a Perl expression that means all lowercase characters in the sequence from a to z. Similarly, the A-Z term represents all uppercase letters in the sequence from A to Z.

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

To force the filenames of all “.prg” files to uppercase, use this command:

rename ‘y/a-z/A-Z/’ *.prg

ls *.PRG

The ls command shows us that all of the “.prg” filenames are now in uppercase. In fact, to be strictly accurate, they’re not “.prg” files anymore. They’re “.PRG” files. 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/A-Z/a-z/’ *.PRG

ls *.prg

No comments:

Post a Comment