How to Rename Multiple Files at Once in Linux

How to Rename Multiple Files at Once in Linux

Preface

In our daily work, we often need to rename a batch of files, such as changing all jpg files to bnp, changing the 1 in the name to one, and so on.

You might already know that we use mv command to rename or move files and directories in Unix-like operating systems. However, the mv command does not support renaming multiple files at once. Don't worry. In this tutorial, we will learn to rename multiple files at once using mmv command in Linux. This command is used to move, copy, append, and rename files in bulk using standard wildcards in Unix-like operating systems.

Rename Multiple Files at Once in Linux

The mmv program is available in the default repositories of Debian based systems. To install it on Debian, Ubuntu, Linux Mint, run the following command:

$ sudo apt-get install mmv

We assume that you have the following files in your current directory.

$ ls
a1.txt a2.txt a3.txt

Now, you want to rename all files that begin with the letter "a" to begin with "b". Of course, you can do this manually in seconds. But do you have hundreds of files that you want to rename? This is a very time-consuming process. This is where the mmv command comes in handy.

To rename all files that begin with the letter "a" to begin with the letter "b", just run:

$ mmv a\* b\#1

Let's check that the files have been renamed.

$ ls
b1.txt b2.txt b3.txt

As you can see, all the files starting with letter “a” (i.e. a1.txt, a2.txt, a3.txt) are renamed to b1.txt, b2.txt, b3.txt.

explain

In the example above, the first argument (a\*) is the "from" pattern, and the second argument is the "to" pattern (b\#1). According to the above example, mmv will find any file name starting with the letter "a" and rename the matching files according to the second parameter, which is the "to" pattern. We can use wildcard characters such as *,? and [] to match one or more arbitrary characters. Note that you must escape wildcard characters, otherwise they will be expanded by the shell and mmv will not understand them.

The #1 in the "to" pattern is a wildcard index. It matches the first wildcard character in the "from" pattern. The #2 in the "to" pattern will match the second wildcard character (if any), and so on. In our case, we only have one wildcard character (the asterisk), so we write a #1. Also, the # symbol should be escaped. Alternatively, you can enclose the pattern in quotes.

You can even rename all files with a specific extension to a different extension. For example, to rename all .txt files in the current directory to .doc file format, just run:

$ mmv \*.txt \#1.doc

Here is another example. We assume you have the following files.

$ ls
abcd1.txt abcd2.txt abcd3.txt

You want to replace the first occurrence of "abc" with "xyz" in all files in the current directory. What would you do?

It's very simple.

$ mmv '*abc*' '#1xyz#2'

Note that in the above example, the pattern is enclosed in single quotes.

Let's check whether "abc" is actually replaced by "xyz".

$ ls
xyzd1.txt xyzd2.txt xyzd3.txt

Did you see it? The files abcd1.txt, abcd2.txt, and abcd3.txt have been renamed xyzd1.txt, xyzd2.txt, and xyzd3.txt.

Another noteworthy feature of mmv command is that you can use the -n option to print the output instead of renaming the files as shown below.

$ mmv -na\* b\#1
a1.txt -> b1.txt
a2.txt -> b2.txt
a3.txt -> b3.txt

This way, you can easily verify what the mmv command actually does before renaming the files.

See the man page for more details.

$ man mmv

Update: Thunar File Manager

Thunar File Manager has a built-in batch rename option by default. If you are using Thunar, renaming files is much easier than using the mmv command.

Thunar is available in the default repositories of most Linux distributions.

To install it on Arch based systems, run:

$ sudo pacman -S thunar

On RHEL, CentOS:

$ sudo yum install thunar

On Fedora:

$ sudo dnf install thunar

On openSUSE:

$ sudo zypper install thunar

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install thunar

Once installed, you can launch Bulk Renamer from the menu or application launcher. To launch it from the terminal, use the following command:

$ thunar -B

The batch renaming method is as follows.

Click "+" and select the list of files you want to rename. Batch renaming can rename the file name, the file suffix, or rename the file name and suffix at the same time. Thunar currently supports the following batch renames:

  • Insert date or time
  • Insert or overwrite
  • serial number
  • Deleting Characters
  • Search and Replace
  • Uppercase or lowercase

When you select one of these conditions from the list of options, you’ll see a preview of the changes in the New Name column, as shown in the screenshot below.

After selecting the conditions, click on the "Rename File" option to rename the file.

You can also open the Batch Renamer from Thunar by selecting two or more files. With the file selected, press F2 or right-click and select Rename.

Happy Birthday!

via: https://www.ostechnix.com/how-to-rename-multiple-files-at-once-in-linux/

Author: SK Topic: lujun9972 Translator: Flowsnow Proofreader: wxy

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • How to Rename a Group of Files at Once on Linux

<<:  Optimization of MySQL thread_stack connection thread

>>:  js implements some functions of the input component in Element and encapsulates it into a component (example code)

Recommend

HTML Tutorial: DOCTYPE Abbreviation

When writing HTML code, the first line should be ...

About Docker security Docker-TLS encrypted communication issues

Table of contents 1. Security issues with Docker ...

How to install pyenv under Linux

Prerequisites Need to install git Installation St...

Introduction and analysis of three Binlog formats in MySQL

one. Mysql Binlog format introduction Mysql binlo...

Multiple ways to change the SELECT options in an HTML drop-down box

After the form is submitted, the returned HTML pag...

Deployment and configuration of Apache service under Linux

Table of contents 1 The role of Apache 2 Apache I...

Introduction to MySQL <> and <=> operators

<> Operator Function: Indicates not equal t...

Detailed explanation of various HTTP return status codes

When a request is sent to your server to display ...

MySQL uses inet_aton and inet_ntoa to process IP address data

This article will introduce how to save IP addres...

How to implement element floating and clear floating with CSS

Basic Introduction to Floating In the standard do...

Native js to achieve accordion effect

In actual web page development, accordions also a...

Uniapp uses Baidu Voice to realize the function of converting recording to text

After three days of encountering various difficul...

How to disable ads in the terminal welcome message in Ubuntu Server

If you are using the latest Ubuntu Server version...

Detailed explanation of using grep command in Linux

Linux grep command The Linux grep command is used...