In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of batch modification of file names under linux. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Question: see the demand of a brother, the picture I took below.
Special note, some brothers are very careful, the above screenshot said to remove _ finished my answer is for the removal of finished, but does not affect the overall situation, please pay attention.
Answer:
[prepare test data]
Mkdir / oldboy cd / oldboy
Touch stu_102999_1_finished.jpg
Touch stu_102999_2_finished.jpg
Touch stu_102999_3_finished.jpg
Touch stu_102999_4_finished.jpg
Touch stu_102999_5_finished.jpg
Method 1:shell script for loop combined with sed implementation
[check data]
[root@oldboy oldboy] # ll
Total 4
-rw-r--r-- 1 root root 85 Oct 2 12:54 oldboy_modi_file.sh
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_1_finished.jpg
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_2_finished.jpg
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_3_finished.jpg
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_4_finished.jpg
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_5_finished.jpg
[development script]
[root@oldboy oldboy] # cat oldboy_modi_file.sh
#! / bin/sh
For file in `ls. / * .jpg`
# shell script for loop, with file as a variable to get the result file name of ls. / * .jpg in turn
Do
Mv $file `echo $file | sed's Universe finishedUniverse G '`
# use the mv command to change the file, and the new file name string concatenation is the focus of this topic.
Done
# Special Tip: through this question, you can learn about the use of the for loop and the use of mv, sed and other commands
[execute script]
[root@oldboy oldboy] # sh oldboy_modi_file.sh
[inspection results]
[root@oldboy oldboy] # ll
Total 4
-rw-r--r-- 1 root root 85 Oct 2 12:54 oldboy_modi_file.sh
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_1_.jpg
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_2_.jpg
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_3_.jpg
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_4_.jpg
-rw-r--r-- 1 root root 0 Oct 2 12:50 stu_102999_5_.jpg
Method 2: partial interception of for loop plus variables in shell script
[root@oldboy oldboy] # ls-l
Total 4
-rw-r--r-- 1 root root 114 Nov 14 00:21 change_file_name.sh
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_1_finished.jpg
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_2_finished.jpg
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_3_finished.jpg
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_4_finished.jpg
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_5_finished.jpg
[root@oldboy oldboy] # cat change_file_name.sh
#! / bin/sh
# oldboy QQ:31333741
For file in `ls. / * .jpg`
Do
/ bin/mv $file `echo "${file%finished*} .jpg" `# this is the new method of intercepting variables, which will be discussed in the old boy's shell programming course.
Done
[root@oldboy oldboy] # sh change_file_name.sh
[root@oldboy oldboy] # ls-l
Total 4
-rw-r--r-- 1 root root 114 Nov 14 00:21 change_file_name.sh
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_1_.jpg
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_2_.jpg
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_3_.jpg
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_4_.jpg
-rw-r--r-- 1 root root 0 Nov 14 00:23 stu_102999_5_.jpg
Special hint: this script and files are in the same directory.
Method 3: ls combined with awk.
[root@oldboy oldboy] # ll
Total 0
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_1_finished.jpg
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_2_finished.jpg
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_3_finished.jpg
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_4_finished.jpg
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_5_finished.jpg
[root@oldboy oldboy] # ls | awk-F 'finished'' {print $0}'
Stu_102999_1_finished.jpg
Stu_102999_2_finished.jpg
Stu_102999_3_finished.jpg
Stu_102999_4_finished.jpg
Stu_102999_5_finished.jpg
[root@oldboy oldboy] # ls | awk-F 'finished'' {print $1}'
Stu_102999_1_
Stu_102999_2_
Stu_102999_3_
Stu_102999_4_
Stu_102999_5_
[root@oldboy oldboy] # ls | awk-F 'finished'' {print $2}'
.jpg
.jpg
.jpg
.jpg
.jpg
[root@oldboy oldboy] # ls | awk-F 'finished'' {print $1 $2}'
Stu_102999_1_.jpg
Stu_102999_2_.jpg
Stu_102999_3_.jpg
Stu_102999_4_.jpg
Stu_102999_5_.jpg
[root@oldboy oldboy] # ls | awk-F 'finished'' {print "mv" $0 "> [root@oldboy oldboy] # ll
Total 0
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_1_.jpg
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_2_.jpg
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_3_.jpg
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_4_.jpg
-rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_5_.jpg
Method 4: through the professional renaming command rename.
[root@oldboy oldboy] # ll
Total 0
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_1_finished.jpg
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_2_finished.jpg
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_3_finished.jpg
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_4_finished.jpg
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_5_finished.jpg
[root@oldboy oldboy] # rename "finished"* = = > perform batch renaming
[root@oldboy oldboy] # ll
Total 0
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_1_.jpg
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_2_.jpg
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_3_.jpg
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_4_.jpg
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_5_.jpg
Extension: the way to change the extension
[root@oldboy oldboy] # rename .jpg .oldboy *
[root@oldboy oldboy] # ll
Total 0
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_1_.oldboy
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_2_.oldboy
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_3_.oldboy
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_4_.oldboy
-rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_5_.oldboy
Special note: if you need to answer questions, blog can leave a message for me. We communicate with each other, learn from each other and make progress together.
This is the end of the example analysis of batch modification of file names under linux. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can 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.