In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how linux quickly renames files in batches. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. Rename command
As the name implies, the rename command is used to rename a file name. The rename command is so powerful that we can use it to modify a variety of complex file names. However, this article only introduces its most basic functions, and will update other powerful renaming features of rename later. The most basic format of rename is as follows:
Rename source string target string file
Where 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 you want to replace; and the file is the list of files that need to be changed, which can be one or more.
Now if there are a bunch of files in the form of atb_mod_01.cpp,atb_mod_02.cpp,atb_mod_03.cpp,atb_mod_04.cpp in the directory, and our requirement is to change the mod in the file name to adb, the command to complete this requirement is as follows:
[alvin@VM_0_16_centos exp3] $lsatb_ 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] $lsatb_adb_01.cpp atb_adb_02.cpp atb_adb_03.cpp atb_adb_04.cpp
2. Mv command cooperates with for loop mode
If we have a bunch of .txt files now, we want to change their suffixes to .cpp. Let's take a look at the complete code first:
#! / bin/bash for name in `ls * .txt` do mv $name ${name%.txt} .CPP done
As we all know, renaming in Linux uses the mv command, and the batch renaming naturally comes to the idea of nesting mv commands with loop statements.
Here, we use ls *. Txt to list all the txt files in the current directory, and then loop them one by one in the name variable.
In the body of the loop, we use the mv command to rename. Here we use the string processing method of ${name%.txt}, which means that the smallest part matching .txt is deleted from the end of the name and the rest is returned. After that, add the .cpp suffix. By doing this, we can change the file name suffix from .txt to .cpp. Finally, we use the mv command to actually change the file name.
3. Sed command cooperates with for loop mode
If we have a pile of files now, the file name format is test01.txt,test02.txt,test03.txt,test04.txt, that is, the first half is in English, and the second half is numbers. We now want to change the file name to test-01.txt. This time, we use the sed command to accomplish 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 /\ ([amurz]\ +\)\ ([0-9]\ +\) /\ 1 -\ 2Candle '`mv $file $newFile done
As before, use ls\ *. Txt to get all the .txt files. It is then output sequentially with the echo command as input to the sed command.
Next, we have reached the key part. Sed's orders may seem scary at first glance, but veteran drivers are used to it. The content in the backquotation mark actually has this basic structure:
S / original string / alternate string /
Here we use grouping matching, that is, we use parentheses to group the original string according to a certain regular expression, followed by\ 1,\ 2,\ 3. To refer to the previous grouping to piece together the corresponding format in the alternative string.
As mentioned earlier, the original file name is made up of the first part of the English part and the latter part of the number. The English part can be expressed as [a murz] + and the number can be expressed as [0-9] +. Be careful not to forget the plus sign, which indicates several repetitions of the preceding characters. Then, we use\ 1 and\ 2 to refer to the corresponding parts in front, respectively, and then connect them with a crossbar, so it looks like this:
S / ([a murz] +) ([0-9] +) /\ 1 -\ 2 /
Because parentheses and plus signs may have different meanings in different Shell, you need to add an escape character in front of it, so it looks like what you saw earlier.
After that, the rename action is also done using the mv command.
This is the end of the article on "how to rename files in batches quickly by 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, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.