In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to write a Shell script to modify the suffix code in batches". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The code is as follows:
#! / bin/bash
Oldext= "JPG"
Newext= "jpg"
Dir=$ (eval pwd)
For file in $(ls $dir | grep. $oldext)
Do
Name=$ (ls $file | cut-d. -F1)
Mv $file ${name}. $newext
Done
Echo "change JPG= > jpg done!"
Here is a brief explanation for this program:
1. The variables oldext and newext specify the old and new extensions, respectively. Dir specifies the directory where the file is located
2. "ls $dir | grep. $oldext" is used to get all files with the old extension in the specified directory dir
3. In the loop, first use the cut command to put the file name in the "." The previous string is cut out and assigned to the name variable; the current file name is then renamed to the new file name.
Through this script, the extensions of all photos have been successfully modified. To make this script more generic, we can add a few read commands to enable interaction between the script and the user. The improved script is as follows:
The code is as follows:
#! / bin/bash
Read-p "old extension:" oldext
Read-p "new extension:" newext
Read-p "The directory:" dir
Cd $dir
For file in $(ls $dir | grep. $oldext)
Do
Name=$ (ls $file | cut-d. -F1)
Mv $file ${name}. $newext
Echo "$name.$oldext = = > $name.$newext"
Done
Echo "all files has been modified."
Attached: another version
The code is as follows:
#! / bin/sh
# file name: rename_suffix.sh
# author: zhouhh
# Email: ablozhou@gmail.com
# Date: 2008.4.1
Echo "input what suffix will be replaced:"
Read SUFFIX_SRC
Echo "input what suffix of file to rename to:"
Read SUFFIX_DST
For i in *. $SUFFIX_SRC
Do
If [- e $I]; then
Echo "mv $I to `basename $I. $SUFFIX_ SRC `.$ SUFFIX_DST"
Mv $I `basename $I. $SUFFIX_ SRC. $SUFFIX_DST
Else
Echo "file does not exist."
Exit-1
Fi
Done
This is the end of the content of "how to write a Shell script to modify the file suffix code in batches". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.