قالب وردپرس درنا توس
Home / Tips and Tricks / Using the rename command on Linux

Using the rename command on Linux



  Linux Laptop Displays a Bash Prompt
Fatmawati Achmad Zaenuri / Shutterstock.com

Familiarize yourself with the powerhouse to rename files in the Linux world and enter mv ̵

1; and yourself – a break. Renaming is flexible, fast and sometimes even easier. Here is a tutorial for this powerhouse of a command.

What's wrong with mv?

With mv nothing is wrong. The command does a good job and can be found on all Linux distributions, in macOS and in other Unix-like operating systems. It is always available. But sometimes you just need a bulldozer, not a shovel.

The command mv has a purpose in life, and that is moving files. It is a nice side effect that it allows an existing file to be moved to a new file with a new name. The net effect is to rename the file so we get what we want. mv is not a special tool for renaming files.

Renaming a single file with mv

To rename a file type by mv use mv a space, the name of the file, a space, and the new name that the File should have. 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

Renaming multiple files with mv

If you want to rename multiple files , it gets harder. mv can not handle renaming multiple files. You have to resort to some sophisticated bash tricks. This is fine if you know some intermediate command-line features, but the complexity of renaming multiple files with mv is in stark contrast to simply using mv to rename a single file.

Things are escalating fast.

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

How can we get mv to do that for us? Let's take a look at the files.

  ls * .prog -l 

  ls * .prog -l in a terminal window

Here is one way to do this, which does not rely on writing a command actual bash script file ,

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

  for f in * .prog; do mv -

Did that work? Let's look at the files and see if.

  ls * .pr * 

  ls * .pr * in a terminal window

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

What happened just now?

What did this long command really do? Let us break it down.

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

The first part launches a loop that sequentially processes all ".prog" files in the directory.

The next part says what the processing will do . With mv each file is moved to a new file. The new file is named with the name of the original file without the part ".prog". Instead, a new extension of ".prg" is used.

There must be an easier way

For sure. It is the command to rename .

Rename is not part of a standard Linux distribution, so you need to install it. It also has a different name in different Linux families, but they all work the same way. You only need to replace the appropriate command name according to the Linux variant you are using.

In Ubuntu and Debian-derived distributions that you install, rename as follows:

  sudo apt-get install rename 

 sudo apt-get install rename in a terminal window </p>
<pre>

In Fedora and RedHat-derived distributions, install prename as follows. Note the initial "p" that stands for Perl.

  sudo dnf install name 

 sudo dnf install name in a terminal window

Use the following command to install in Manjaro Linux. Note that the rename command is perl-rename .

  sudo pacman-yu perl-rename 

 sudo pacman-yu perl-rename in a terminal window

Let's do it again

And this time we use rename . We will reset the clock so we have a bunch of ".prog" files.

  ls * .prog 

 ls * .prog in a terminal window

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

  Name & # 39; s / .prog / .prg / & # 39; s. * .prog [19659011] ls * .pr * 

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

What happened during this time?

Let us explain this bit of magic in three parts.

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

The last part is *. Prog The Tell s rename to edit all the ".prog" files.

The middle part defines the work we want to do for each filename. The s means replacement. After the first term ( .prog ), is renamed in each file name and replaced by the second term ( .prg ).

The middle part of the command or the central expression is a regular Perl expression which gives the Rename command its flexibility.

Changing Other Parts of a File Name

We have changed the file name extensions so far. Let's change other parts of the filenames.

There are many C source code files in the directory. All filenames are prefixed with "slang_". We can do this with ls .

  ls sl * .c 

<img class = "alignnone size-full wp-image-423248" data-pagespeed-lazy-src = "https://www.howtogeek.com/wp-content/uploads /2019/05/rename_11.png.pagespeed.ce.JNffpw1sVZ.png Check "alt =" 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 [1965] Replace all occurrences of "slang_" with "sl_" The format of the command We're already familiar with this, we just change the search term, the replacement term, and the file type.

  Rename & # 39; s / slang_ / sl _ & # 39; * .c. 

 Rename & # 39 ; s / slang_ / sl _ & # 39; * c into a terminal window "width =" 646 "height =" 77 "src =" /pagespeed_static/1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; page speed. lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);

This time we search for ".c" files and search for "slang_". If "slang_" is found in a file name, it will be replaced by "sl _".

You can check the result of this command by repeating the command ls from the top with the same parameters:

  ls sl * .c 

 ls sl * .c in one 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);

Deleting part of a filename

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

  ls * .c 
  names & # 39; s / sl _ // & # 39; * .c 
  ls * .c 

 names & # 39; s / sl _ // & # 39; * .c in a at terminal window "width =" 646 "height =" 352 "src =" /pagespeed_static/1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyL .loadIfVisibleAndMaybeBeacon (this); [19659006] The command ls indicates that our ".c" files are prefixed with "sl_". Let's get rid of that completely.

Rename the command has the same format as before. We will search for ".c" files. The search term is "sl_", but there is no substitution term. Two backslashes without anything in between mean nothing, an empty string.

rename processes each ".c" file in turn. It searches for "sl_" in the file name. If found, nothing will replace it. In other words, the search term is deleted.

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

Restrict Changes to Certain Parts of Filenames

Let's look at ls files whose filenames contain the string "param". Then we use rename to replace this string with the string "parameter". We will use ls again to see the effect of renaming on these files.

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

 renames / 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); [In1965herfilenameparamcparam_onecundparam_twochabenall"param"at start of her name. third_param.c has "param" at end of its name, just before the extension.

The command rename looks for "param" everywhere in the filename. and replace it with "parameter" in all cases.

The second use of the command ls shows us that's exactly what happened. Regardless of whether "param" was at the beginning or end of the filename, it was replaced by "parameter".

We can use Perl's metacharacters to refine the behavior of the middle term. Metacharacters are symbols that represent positions or sequences of characters. Example: ^ means "beginning of a string", $ means "end of a string" and . means any single character (except a newline character).

We will use the character for the beginning of the string ( ^ ) to limit our search to the beginning of the file name.

  ls * param * .c 
  names the / ^ parameter / value / * at * .c 
  ls * param * .c 
  ls value * .c 

 names the / ^ parameter at / 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 previously renamed files are listed, and we can see that the string "parameter" is at the beginning of three filenames and at the beginning of three filenames is the end of a filename.

Renaming our command uses the newline metacharacter (^ ) before the search term "parameter". This partitions rename [19459010mit] only consider th The search term was found if it is at the beginning of the file name. The search string "parameter" is ignored if it is somewhere else in the file name.

If you check this with ls you may find that the filename at the end of had "parameters" of the filename was not changed, but in the case of the three filenames whose name "parameter" contained in beginning the search string was replaced by the replacement term "value".

Rename the potency of lies in the power of Perl. All the power of Perl is at your disposal.

Browsing with Groups

Renaming got more tricks under control. Consider the case where you may have files with similar strings in the name. They are not exactly the same strings, so a simple search and replace does not work here.

In this example, we use ls to check which files begin with "str". There are two of them, string.c and strangle.c. We can rename both strings simultaneously using a technique called grouping.

Rename the central expression of this command searches for strings in filenames with the string "stri" or . "Stra", with these sequences immediately followed by "ng". In other words, our search string is "string" and "stranded". The substitution term is "bang".

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

Name s / (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 this string has been confirmed a second time .c and strangle.c are now bangle.c.

Using Translations with Renaming

The Rename command can perform actions on file names called translations. A simple example of a translation would be to put a series of file names in uppercase.

Note the following command rename that we do not use s / to start central expression, we use y / . This informs that no substitution is performed. We carry out a translation.

The term a-z is a Perl expression that means all lowercase letters in the order from a to z. Similarly, the expression AZ stands for all uppercase letters in order from A to Z.

The central expression in this command could be rewritten as: "If lowercase letters from a to z are in Replace Use the command to force the uppercase letters of all ".prg" files:

rename & # 39; y / az / AZ / & 39; * .prg

  ls * .PRG 

<img class = "alignnone size-full wp-image-423258" data-pagespeed-lazy-src = " https://www.howtogeek.com/wp-content/uploads/2019/05/rename_14.png.pagespeed.ce.gA_DJSxQOt.png "alt =" rename & # 39; 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.loadIfVisibleA 19659006] The command ls shows us that all" .prg "filenames are now in capital letters, but strictly speaking, they are not anymore ".prg" files These are ".PRG" files, Linux is case-sensitive.

We can reverse this last command by specifying the location of the terms az and Rename AZ in the central expression.

y / AZ / az / & # 39; * .PRG

  ls * .prg 

<img class = "alignnone size-full wp-image-423259 "data-pagespeed-lazy-src =" https://www.howtogeek.com/wp-content/uploads/2019/05/rename_15.png.pagespeed.ce.X6VOGrIodV.png " alt = "name & # 39; 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.loadIfVisibleAndMayLoadImages] You (Wo | Do) do not learn Perl in five minutes.

It's worth getting acquainted with Perl. However, to use the time-saving features of the rename command, you do not have to have much Perl knowledge to get great performance, simplicity, and time advantages.




Source link