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--
Editor to share with you how Linux shell uses sed to change file names in batches. I hope you will get something after reading this article. Let's discuss it together.
Example
Remove specific characters
Goal: change 2017-01-01.jpg, 2018-01-01.jpg to 20170101.jpg, 20180101.jpg
Method: replace all-with empty
For file in `ls | grep .jpg`do newfile= `echo $file | sed's Unibank G` ``mv $file $newfiledone
Here, sed is used for string substitution for standard output, and the common format is as follows:
Stdout | sed's _
In the above example, adding g at the end is used to replace all matches, not just the first match.
Intermediate insert character
Goal: change book01.txt, paper02.txt to book-01.txt, paper-02.txt
Methods: the strings on both sides of the position to be inserted are obtained by grouping matching, and then replaced by reverse reference.
For file in `ls | grep .txt`do newfile= `echo $file | sed's /\ ([a murz]\ +\)\ ([0-9]\ +\) /\ 1 -\ 2 mv '`txt`do `echo $newfiledone
Analysis.
The above example first gets the list of files to be renamed through the ls and grep commands, then replaces the strings with the sed command, and finally uses the mv command to change the file name.
There are many ways to get a list of files to be renamed, either through the find command or directly giving a string, which we'll talk about later.
Notice the ls | grep .txt after the for loop, which is enclosed in two anti-single quotes and has the same effect as $(ls | grep .txt). The enclosed string is executed as a command and the string result is returned.
The solution to the file name containing spaces
We can write the list of files directly into the for loop instead of getting it through a command, such as:
For file in "file1 file2 file3" do... done
You can see that the for loop splits strings by spaces, so if the file name to be changed contains spaces, it will be split into multiple file names, resulting in an error.
To solve this problem, we can set the IFS (internal field delimiter) to the newline character\ n, so that the for loop gets the value of the variable by line, ensuring that it does get a complete file name each time.
The command to set the IFS variable needs to be placed before the for loop:
IFS=$'\ n'for file in `ls`do... done
You can also directly use the while read command to read one line at a time into the variable file:
Ls | grep "* .txt" | while read filedo... done
Use find to get a list of files
In the previous example, we obtained the list of files through the ls command. This command can only get files from a directory and cannot filter for multiple conditions.
When it comes to finding files, we have to mention the powerful find command. This command can find files in multiple levels of directories, and can set a variety of conditions, such as creation time, file size, owner, and so on.
Using the find command to get the file list, and then using the sed command combined with regular expressions to modify the file name, the combination of these two commands can complete almost all the common batch renaming tasks.
For example, rename all files larger than 1m with the suffix txt or jpg from files such as book_20170101.txt or image_20170101.jpg to 20170101-book.txt, 20170101-image.jpg, with the following code:
For file in `find. -size + 1m-name "* _ * .txt"-o-name "* .jpg" `do newfile= `echo $file | sed's /\ ([aresorz]\ +) _\ ([0-9]\\). /\ 2 -\ 1.Only'mv $file $newfiledone has finished reading this article. I believe you have some understanding of "Linux shell's method of changing file names in batches using sed". If you want to know more about it, please follow the industry information channel. Thank you for reading!
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.