In our working lives, programmers and non-programmers alike have come across the need to rename a bunch of files. On Windows, there are many excellent programs that can help us accomplish this, while on Linux, we can do this by simply typing some code.

This article will cover the three most basic file renaming methods, so the old driver can leave it at that.

1. Rename command

As the name implies, the rename command is used to rename a file name. The rename command is very powerful and can be used to modify a variety of complex file names. However, this article covers only the most basic features of rename, and we’ll update it later with other powerful renaming features. The most basic format for rename is as follows:

Rename Source string Target string fileCopy the code

The source string indicates the string to be replaced by the original file name, which can be all or part of the original file name. The target string is the string you want to replace with; A file is a list of files whose names need to be changed. It can be one or more files.

CPP, atb_mod_02.cpp, atb_mod_03.cpp, atb_mod_04.cpp, atb_mod_03.cpp, atb_mod_04.cpp, atb_mod_02.cpp, atb_mod_03.cpp, atb_mod_04.cpp, etc.

[alvin@VM_0_16_centos exp3]$ ls atb_mod_01.cpp atb_mod_02.cpp atb_mod_03.cpp atb_mod_04.cpp [alvin@VM_0_16_centos exp3]$  rename mod adb * [alvin@VM_0_16_centos exp3]$ ls atb_adb_01.cpp atb_adb_02.cpp atb_adb_03.cpp atb_adb_04.cppCopy the code

2. Combine the mv command with the for loop mode

Suppose we now have a bunch of.txt files and we want to change their suffix to.cpp. Let’s start with the complete code:

#! /bin/bash for name in `ls *.txt` do mv $name ${name%.txt}.cpp doneCopy the code

As we all know, in Linux the mv command is used for renaming, so batch renaming naturally leads to nested MV commands in a loop.

In this case, we use ls *.txt to list all TXT files in the current directory, and then loop through the name variable one by one.

In the body of the loop, we rename it using the mv command. Here we use string manipulation ${name%.txt} to remove the smallest part of the.txt match from the end of name and return the rest. After that, add the.cpp suffix. By doing this, we can change the filename suffix from.txt to.cpp. Finally, we use the mv command to actually change the file name.

3. Run the sed command with the for loop

TXT, test02.txt, test03.txt, test04.txt. We now want to change the file name to the form test-01.txt. This time, we use the sed command to accomplish this requirement.

Let’s look at the complete code first.

#! /bin/bash for file in `ls *.txt` do newFile=`echo $file | sed 's/\([a-z]\+\)\([0-9]\+\)/\1-\2/'` mv $file $newFile doneCopy the code

Use ls \*.txt as before to get all.txt files. Then run the echo command to output them sequentially as the input of the sed command.

Now, here’s the big part. Sed’s commands may seem scary at first glance, but veteran drivers are used to them. Here’s the basic structure of the backquotes:

S/Original string/alternate string /Copy the code

Here we use grouping matching, that is, grouping the original string by a regular expression in parentheses followed by \1, \2, \3… To reference the previous grouping to piece together the appropriate format in an alternative string.

As mentioned above, the original file name is composed of the first part of English and the last part of digits. The English part can be represented by [A-z]+, and the number can be represented by [0-9]+. Be careful not to forget the plus sign, which indicates several repetitions of previous characters. Then, we use \1 and \2 to refer to the corresponding parts of the preceding sections respectively, and connect them with a bar, so that it looks like this:

s/([a-z]+)([0-9]+)/\1-\2/
Copy the code

Because parentheses and plus signs can have different meanings in different shells, you add an escape character to the front, and you get what you saw earlier.

After that, the mv command is also used to rename the file.

> < p style = “max-width: 100%; clear: both
#### read all is true love, a praise to walk again? Your “sanlian” is the biggest motivation for liang Xu’s continuous creation!
1\. Follow the ** original ** * public account “** Liangxu Linux**”, the first time to get the latest Linux dry goods!
2. Public account background reply [information] [interview] [resume] to get selected first-line big factory interview, self-improvement, resume and other information.
3\. Follow my blog: [lxlinux.net]([http://www.lxlinux.net]](http://www.lxlinux.net%29/)