Detailed in Linux rename command (rename multiple files)

Use the mv command to rename multiple files and directories can be a tedious process, because it involves the use of pipes, circulation and other write complex commands.

This is the rename command comes in handy. It is a search expression by replacing the name of the specified replacement item to rename the given file.

In this tutorial, we will explain how to use the command rename batch rename files.

Installation rename

rename command comes in two versions, with different syntax and functionality. We will use the Perl version of the rename command.

If this version is not installed on your system, use the Linux distribution's package manager to install:

  • And Debian

sudo apt update
sudo apt install rename

  • with

Detailed in Linux rename command (rename multiple files)

  • Arch Linux

yay perl-rename

Use rename

The following is the general syntax rename command:

rename [OPTIONS] perlexpr files

rename command is basically a Perl script. It will rename the regular expression given file according to the specified perlexpr. You can read about Perl regular expressions here.

For example, the following command all the .css file extension changed to .scss:

Detailed in Linux rename command (rename multiple files)

Let us explain in more detail what this command:

  • s / search_pattern / replacement / - replacement operator.
  • .css - search mode. It is the first parameter of the alternative operator. rename command searches for this mode in a given file name, if it will be found to replace it with the replace parameter.
  • .scss - replace. Alternatively the operator of the second parameter.
  • * .Css - all files with the extension ".css" of. Wildcard (*) is used to represent zero, one or more characters of symbols.

Prior to running the actual command and rename files and directories, it is best to use the -n option to perform "preview" and displays the renamed file:

Output is as follows:

Detailed in Linux rename command (rename multiple files)

By default, rename command does not overwrite an existing file. Use the -f option to tell rename overwrite the existing file:

If you want to rename the file to print the name of the successful renaming, use the -v (verbose) option:

Output is as follows:

Detailed in Linux rename command (rename multiple files)

rename example

Here are some common examples of how to use the rename command:

Replace the file name spaces with underscores

rename 'y/ /_/' *

Convert file names to lowercase

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

The file names are converted to uppercase

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

.Bak deleted from the file name

rename 's / \. behind $ //' * .bak

The .jpeg .JPG file name and rename the .jpg

rename 's/\.jpe?g$/.jpg/i' *

to sum up

rename command allows you to use Perl regular expressions to rename multiple files at once.

If you have any questions or feedback, please feel free to comment.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160328.htm