In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to use the rename command in Linux to rename in batches. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
How to tell which version of the rename command is in the system?
Enter man rename to see that the first line is
RENAME (1) Linux Programmer's Manual RENAME (1)
So this is the C language version.
And if what appears is:
RENAME (1) Perl Programmers Reference Guide RENAME (1)
This is the Perl version!
Grammatical differences between the two versions:
C language, according to the notes above in man
The syntax format of rename is:
Rename fromtofile
This command takes three parameters, from: what name to change, to: what name to change, and which files file needs to modify.
Usage example:
For example, there is a batch of files that all start with log, log001.txt, log002.txt... . All the way to log100.txt
Now you want to replace all the log of these files with history.
Rename log history log*
The meaning of this command is clear. Replace the log characters in all files that begin with log with history.
The replaced files are: history001.txt, history002.txt... .. All the way to history100.txt
Another man example of the rename C language version is to modify the suffix in batches.
For example, we want to change all the suffix name image files of jpeg to jpg files.
Rename .jpeg.jpg * .jpeg
In this way, all suffixes extended with .jpeg are modified to .jpg
Now summarize what the rename C version can do: modify the file name in batches, and the result is that each file will be replaced with the same string! That is, there is no way to implement things such as looping and renaming by number!
The advantage of the batch renaming of the Perl version with Perl is that you can use regular expressions to do very strange functions.
The parameter format of the perl version:
Rename perlexprfiles
Note that the perl version of rename has only two parameters, the first parameter is the perl regular expression, and the second parameter is the file to be processed
Examples of help for man rename:
1) there are a batch of files that end with .bak, and now I want to get rid of them all.
Rename's /\ .bak $/ /'* .bak
This command is simple, because I haven't learned perl systematically, and I don't know if substitution strings in perl do this, but sed does, so if you have a sed or tr foundation, it's easy to understand that this substitution is exactly the same as the regular syntax in sed.
2) change all file names that contain upper and lowercase letters to lowercase letters.
Rename'yUnix Amurazapa Muhamzubo'*
It is still the same as the replacement grammar of sed, there is no need to explain, if you do not understand, you can systematically learn sed first.
There are a few more practical examples:
Remove the spaces in the file name in batch
Linux file names originally do not support spaces, I do not know when allowed, of course, when the command line calls the file, spaces are very problematic, for example, you can directly mv oldfile newfile but not spaces, you have to add double quotes: mv "oldfile"newfile" or use the backslash to transfer\ [], this is fine, but if you directly introduce the picture name containing spaces into the Latex document When Latex generates pdf, it will print out the file name directly. This question has bothered me for a long time before. Why does the file name always appear in my generated pdf? Later, it was found that it was the problem of spaces in the file name! Windows system generated under the file name is born with spaces, although very annoying, but some Hewlett-Packard scanners generated images to add spaces by default, there is no way, had to remove him, in the system to study the rename command, I used mv to remove spaces.
Two unblanked versions of the online process:
1) tr version:
The code is as follows:
Find. -type f-name "* *"-print |
While read name; do
Na=$ (echo $name | tr'_')
If [[$name! = $na]]; then
Mv "$name" $na
Fi
Done
This version I have been using before, I do not know which online search came from, at that time did not systematically learn the tr/sed/awk command.
Note, it's easy to understand, find. Type f-name "* *"-print this sentence is to find the current directory of all types of ordinary files and the name contains spaces in the file, and print out, in fact, find default is to print this-print redundant, and then through the pipeline to while loop to read, the file name into the name variable, with the tr command to replace the space for the underscore. The following judgment is that if the name after execution is different, use the mv command to rename. But this if judgment is optional, because find has queried all the filenames with spaces, so after the tr command, the $na variable is definitely not equal to the $name variable.
So this code can be simplified:
The code is as follows:
Find. -type f-name "* *" |
While read name; do
Na=$ (echo $name | tr'_')
Mv "$name"$na"
Done
Tr can be seen as a stripped-down version of sed, and tr replaces spaces with underscores.
Another is the sed version implementation:
For f in *; do mv "$f" `echo "$f" | sed's / []\ + / _ / g``; done
The sed expression here can also be written like this:
Sed's / [[: space:]]\ + / _ / g'
Remember, however, that one or more plus signs in sed require the addition of a backslash. That is:\ +
That's all right.
Well, both of these methods are too fucking wordy, let's take a look at the rename implementation:
Rename's / [] + / _ / g'*
OK is as simple as that.
The space in square brackets can be replaced by [: space:]
It can be written as's / [[: space:]] + / _ / g'
Note here that rename uses standard perl regular syntax, so there is no need to convert a plus sign to a backslash plus sign. That is, + cannot be changed to\ +, otherwise the replacement fails.
There are a few other interesting examples:
For example, add hello to the header of the file.
Rename's / ^ / hello/' *
Uniformly modify the .html extension to .htm
Rename's _
Uniformly append the .zip suffix to the tail:
Rename's Placement Universe. Zipplink'*
Remove the .zip suffix uniformly:
Rename's Universe.ziphammer Universe'*
Regularized numeric number names, such as 1.jpg, 2.jpg... .. 100.jpg, now make all three digits of the file name, that is, 1.jpg... . 001.jpg
Run the command twice:
The code is as follows:
Rename's / ^ / 00Compact'[0-9] .jpg
# this step makes 1.jpg. 9.jpg changes to 001.jpg. 009.jpg
Rename's / ^ / 0Compact'[0-9] [0-9] .jpg
# this step makes 10.jpg. 99.jpg changed into 010.jpg. 090.jpg
Ok, rename have studied so much that I don't know how to introduce dynamic variables into rename for the time being, such as $iTunes +
I have tested iTun0; rename-n "s / ^. * $/ $((+ + I)) /" * I was incremented by 1 after execution. It is not like I imagined that one file can be incremented in each operation. I guess it may be due to the batch implementation of rename, resulting in + I only calculating once!
-n is used to test the rename process and does not run directly. You can check the test results and then run it.
The above is how to use the rename command to rename in batches in Linux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.