How to Use the rename Command on Linux

Linux laptop displaying a bash promptFatmawati Achmad Zaenuri / Shutterstock.com

Familiarize yourself with renaming files from the Linux world and give mv – and yourself – some rest. Renaming is flexible, fast and sometimes even easier. Here is a tutorial at this command station.

What is the problem with mv?

There is nothing wrong with mv. The order is made a beautiful job, and it is found on all Linux distributions, MacOS and other Unix-like operating systems. So, he is always available. But sometimes, you just need a bulldozer, not a shovel.

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

Rename a single file with mv

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

You can use ls to verify that the file has been renamed.

mv oldfile.txt newfile.txt
ls * .txt

mv oldfile.txt newfile.txt in a terminal window

Rename multiple files with mv

Things get complicated when you want to rename multiple files. mv can not rename multiple files. You have to resort to some clever tips from Bash. This is fine if you know a medium-level command-line fu, but the complexity of changing the name of multiple files with mv contrasts sharply with the ease of use of mv to rename a single file.

Things are degenerating quickly.

Let's say we have a directory with a variety of files, of different types. Some of these files have a ".prog" extension. We want to rename them in the command line so that they have a ".prg" extension.

How can we manage to do it for ourselves? Let's look at the files.

ls * .prog -l

ls * .prog -l in a terminal window

Here is one way to do it without having to write a Bash script file.

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

Did it work? Let's see the files and see.

ls * .pr *

ls * .pr * in a terminal window

So, yes, it worked. These are all ".prg" files now, and there are no ".prog" files in the directory.

What has just happened?

What does this long order really do? Let's break it down.

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

The first part begins a loop that will process each ".prog" file in the directory.

The next part says what the treatment will do. It uses mv to move each file to a new file. The new file will be named with the name of the original file, except for the ".prog" part. A new extension of ".prg" will be used instead.

There must be a simpler way

Certainly. This is the rename command.

renaming is 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. All you need to do is substitute the appropriate command name for the version of Linux you are using.

In Ubuntu and Debian derived distributions, you install the renamed installation like this:

sudo apt-get install rename

sudo apt-get install rename in a terminal window

In Fedora and RedHat derived distributions, you install named like this. Note the initial "p" which means Perl.

sudo dnf install firstname

sudo dnf install prename in a terminal window

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

sudo pacman -Syu perl-rename

sudo pacman -Syu perl-rename in a terminal window

Let's do it again

And this time, we will use rename. We will go back to have a set of ".prog" files.

ls * .prog

ls * .prog in a terminal window

Now, let's use the following command to rename them. We will then check with ls if it worked. Do not forget to replace rename with the appropriate command name for your Linux if you do not use Ubuntu or a Debian-derived Linux.

rename & # 39; / .prog / .prg / & 39; * .prog
ls * .pr *

rename & # 39; / .prog / .prg / & 39; * .prog in a terminal window "width =" 646 "height =" 147 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

It worked, they are now all ".prg" files and there is no ".prog" file left in the directory.

What happened after that time?

Let's explain this piece of magic in three parts.

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

The last part is * .prog, which tells you to rename to run on all ".prog" files.

The central part defines the work we want to do on each file name. The s stands for substitute. The first term (.prog) is what rename will look for in each file name and the second term (.prg) will be replaced by.

The central part of the command, or central phrase, is a Perl & # 39;regular expression"And that's what gives the command rename its flexibility.

Change other parts of a file name

Until now, we have changed the file name extensions. Let's change the other parts of the file name.

The directory contains many C source files. All filenames have the prefix "slang_". We can check this with ls.

ls sl * .c

ls sl * .c in a terminal window "width =" 646 "height =" 212 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this. onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

We will replace all occurrences of "slang_" with "sl_". The format of the order is already familiar to us. We are only changing the search term, the replacement term and the file type.

rename it s / slang_ / sl _ & nbsp; * .c

rename it s / slang_ / sl _ & nbsp; * .c in a terminal window "width =" 646 "height =" 77 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

This time, we are looking for ".c" and "slang_" files. Whenever "slang_" is found in a file name, it is replaced by "sl_".

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

ls sl * .c

ls sl * .c in a terminal window "width =" 646 "height =" 197 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this. onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

Delete part of a file name

We can delete part of a file name by substituting the search term with nothing.

ls * .c
rename it s / sl _ // & nbsp; * .c
ls * .c

rename it / sl _ // & # 39; & # 39; .c in a terminal window "width =" 646 "height =" 352 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

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

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

renaming will process each ".c" file in turn. It will look for "sl_" in the file name. If it's found, it will not be replaced by anything. In other words, the search term is deleted.

The second use of the ls command confirms that the prefix "sl_" has been removed from each ".c" file.

Limit changes to specific parts of file names

Let's use ls to look at files that have the string "param" in their file name. Then we will use rename to replace this string with the string "parameter". We will use ls again to see the effect of the rename command on these files.

ls * param *
rename the / param / parameter & # 39; * .c
ls * param *

rename & # 39; / param / parameter & # 39; * .c in a terminal window "width =" 646 "height =" 197 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

Four files are found that have "param" in their file name. param.c, param_one.c and param_two.c all have param at the beginning of their name. third_param.c has "param" at the end of its name just before the extension.

The rename command will look for "param" everywhere in the file name and replace it with "parameter" in all cases.

The second use of the ls command shows us that this is exactly what happened. If "param" was at the beginning or the end of the file name, it was replaced by "parameter".

We can use Perl metacharacters to refine the behavior of the intermediate expression. Metacharacters are symbols representing positions or sequences of characters. For example, ^ means "beginning of a string", $ means "end of a string" and. means any single character (with the exception of a newline character).

We will use the beginning of string metacharacter (^) to limit our search at the beginning of file names.

ls * param * .c
rename the / parameter / value / & # 39; * .c
ls * param * .c
ls value * .c

rename '/ ^ parameter / value / & # 39; * .c in a terminal window "width =" 646 "height =" 247 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

The files 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 command uses the leading line metacharacter (^) before the search term "parameter". This tells rename to consider that the search term was found at the beginning of the file name. The search string "parameter" will be ignored if it is elsewhere in the file name.

By checking with ls, we can see that the file name that had "parameter" at the end of the filename was not changed, but that the three file names that had "parameter" at the beginning of their name were been replaced by the term "value" research chain.

The power to rename lies in the power of Perl. The whole of the Perl's power is at your disposal.

Search with groupings

renaming has even more tricks in his sleeve. Take the case where you could have files with similar strings in their names. These are not exactly the same strings, so simple search and substitution will not work here.

In this example, we use ls to check for files that start with "str". There are two, string.c and strangle.c. We can rename both strings at the same time using a technique called grouping.

The central expression of this name change command will look for the strings contained in the file names whose strings are stri or stra, these sequences being immediately followed by ng. In other words, our search term will search for "string" and "strang". The substitution term is "bang".

ls str * .c
rename it / (stri | stra) ng / bang / & # 39; * .c
the ban * .c

rename it / (stri | stra) ng / bang / & # 39; * .c in a terminal window "width =" 646 "height =" 197 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages .loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

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

Use renamed translations

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

In the rename command below, note that we do not use s / to start the central expression, but y /. This indicates to rename that we do not perform substitution; we perform a translation.

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

The central expression of this command can be paraphrased as follows: "If one of the lowercase letters from a to z is in the file name, replace them with the corresponding characters in the A capital sequence. to Z. "

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

rename 'y / a-z / A-Z /' * .prg

ls * .PRG

rename it to 'y / az / AZ / & # 39; * .prg in a terminal window "width =" 646 "height =" 147 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

The ls command shows us that all ".prg" filenames are now in uppercase. In reality, to be strictly accurate, they are no longer ".prg" files. These are ".PRG" files. Linux is case sensitive.

We can invert this last command by inverting the position of the terms a-z and A-Z in the central expression.

rename 'y / A-Z / a-z /' * .PRG

ls * .prg

rename 'y / AZ / az / & # 39; * .PRG in a terminal window "width =" 646 "height =" 147 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

You (Wo | Do) do not learn Perl in five minutes

Getting acquainted with Perl, it's time well spent. However, to start using the time-saving features of the rename command, you do not need to have a lot of Perl knowledge to gain significant benefits in terms of power, simplicity, and time.

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.