Teach you three ways to quickly rename files in batches

In our work life, whether programmers or non-programmers, we will encounter a requirement, that is, to rename a bunch of files. There are many excellent softwares under Windows that can help us fulfill this requirement, and under the Linux environment, we can simply type some code to complete this requirement.

This article Liang Xu will introduce the three most basic file renaming methods, because it is relatively basic, so old drivers can stop here.

1. rename command

As the name implies, the rename command is used to rename the file name. The rename command has a very powerful function, and we can use it to modify various complex file names. However, this article only introduces its most basic functions, and rename other powerful renaming functions will be updated later. The most basic format of rename is as follows:

rename 源字符串 目标字符串 文件

Among them, the source string represents the string that the original file name needs to be replaced, which can be all or part of the original file name; the target string is the string to be replaced with; the file is a list of files whose file name needs to be changed, which can be one or more One.

If the directory is now a pile of atb_mod_01.cpp,atb_mod_02.cpp,atb_mod_03.cpp,atb_mod_04.cppdocuments and other forms of our needs is the file name modchanged adb, then the command to complete this requirement as follows:

[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.cpp

2. mv command with for loop

If we now have a bunch of .txtfiles, we want to change their suffix .cpp. First look at the complete code:

#!/bin/bash

for name in `ls *.txt`
do
    mv $name ${name%.txt}.cpp
done

We all know that the mv command is used to rename in Linux, so the batch renaming will naturally think of nesting mv commands with loop statements.

Here, we use ls *.txtall the txt files in the current directory to list them all, one by one and then go on the name variable cycle operation.

In the loop body, we use the mv command to rename. Here we use ${name%.txt}this approach string indicating the start of the tail removed from the name with .txtthe smallest partial match, and returns the remaining portion. After that, coupled with the .cppsuffix. Through this operation, we can change the file name suffix from .txt to .cpp. Finally, we use the mv command to really change the file name.

3. sed command with for loop

If we now have a bunch of files, the file name format test01.txt,test02.txt,test03.txt,test04.txtis the first part in English, the second half is digital. We now want to rename the file to test-01.txtthis form. This time, we use the sed command to complete this requirement.

Let's take a 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
done

As before using ls \*.txtto get all the .txtfiles. Then use the echo command to output them sequentially as the input of the sed command.

Next, the key part is reached. At first glance, sed's command may seem a bit scary, but the old driver has long been used to it. The content in the backticks is actually this basic structure:

s/ 原字符串 / 替代的字符串 /

Here we use the matched packet, i.e. the original string expressions in brackets are grouped according to a certain timing, and then back \1,\2,\3……to the front of the reference packet, whereby in the alternative string together into the appropriate format.

Qian Wenyi told, the original file name is part English and part number before after constituted, English can be [a-z]+expressed with numbers can be [0-9]+represented. Be careful not to forget the plus sign, which means several repetitions of the preceding characters. Then, we use \1 and \2 to quote the corresponding parts in front, and then connect them with crossbars, so it is like this:

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

Because the brackets and the plus sign may have different meanings in different Shells, an escape character must be added in front, so it becomes what you saw before.

After that, use the mv command to complete the rename action.

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108520953