Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to rename files in batch under Linux

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to rename files in batches under Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In Linux, we usually use the mv command to rename files, which is very convenient when renaming individual files. However, if we want to rename a set of files, mv is a bit weak.

Rename multiple files at a time in Linux

The mmv program can be used in the default repository of Debian-based systems. To install it on Debian, Ubuntu, Linux Mint, run the following command:

$sudo apt-get install mmv

Let's assume that you have the following files in the current directory.

$lsa1.txt a2.txt a3.txt

Now, you want to rename all files that start with the letter "a" to start with "b". Of course, you can do this manually in a few seconds. But think about whether you have hundreds of files you want to rename? This is a very time-consuming process. The mmv command is very helpful at this point.

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

$mmv a\ * b\ # 1

Let's check to see if the files have been renamed.

$lsb1.txt b2.txt b3.txt

As you can see, all files that start with the letter "a" (that is, a1.txt, a2.txt, a3.txt) are renamed to b1.txt, b2.txt, b3.txt.

explain

In the above example, the first parameter (a *) is the "from" mode, and the second parameter is the "to" mode (bread1). Based on the above example, mmv will look for any file name that starts with the letter "a" and rename the matching file, the "to" pattern, based on the second parameter. We can use wildcards, such as *,? And [] to match one or more arbitrary characters. Please note that you must escape using wildcards, or they will be extended by shell and will not be understood by mmv.

# 1 in "to" mode is a wildcard index. It matches the first wildcard in the "from" pattern. # 2 in "to" mode will match the second wildcard, if any, and so on. In our example, we only have one wildcard character (asterisk), so we write a # 1. And the # symbol should also be escaped. In addition, you can enclose the pattern in quotation marks.

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

$mmv\ * .txt\ # 1.doc

This is another example. Let's assume that you have the following documents.

$lsabcd1.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 simple.

$mmv'* abc*''# 1xyzroom2'

Notice that in the example above, the pattern is enclosed in single quotation marks.

Let's check to see if "abc" is actually replaced with "xyz".

$lsxyzd1.txt xyzd2.txt xyzd3.txt

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

Another noteworthy feature of the mmv command is that you can print the output using the-n option instead of renaming the file, as shown below.

$mmv-n a\ * b\ # 1a1.txt-> b1.txta2.txt-> b2.txta3.txt-> b3.txt

This way, you can simply verify what the mmv command actually does before renaming the file.

See the man page for more details.

$man mmv

Updating: Thunar file manager

The Thunar File Manager has a built-in bulk 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 repository for most Linux distributions.

To install it on an Arch-based system, 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

After installation, you can start the batch rename program from the menu or from the application launcher. To start it from the terminal, use the following command:

$thunar-B

The batch renaming method is as follows.

Img

Click +, and then select the list of files 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 renaming:

Insert date or time insert or overwrite number delete character search and replace uppercase or lowercase

When you select one of the conditions from the list of options, you will see a preview of the changes in the New name column, as shown in the screenshot below.

After selecting the condition, click the rename File option to rename the file.

You can also open the batch rename from Thunar by selecting two or more files. After selecting the file, press F2 or right-click and choose rename.

Thank you for reading! This is the end of the article on "how to rename files in batches under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report