Shell script: Batch modify file name (add character in file name)
The previous article on the bulk modification of the file name (the file name of the addition of characters), the work also exists such a requirement, batch modify the file name, delete some characters in the file name;
For example: Batch rename, delete extra characters in file name
The file name below is listed below, which requires removing _finished.
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
There are a number of possible ways to do this:
Method one: For loop combined with SED replacement
[[email protected] ~]$ for file in ' ls *.jpg ';d o mv $file ' echo $file |sed ' s/_finished//g ';d one;
Method two: LS combined with awk, the output is given to bash execution
[[email protected] ~]$ ls *.jpg |awk-f "_finished" ' {print "MV" $ "" $1$2 ""} ' |bash
The commands are actually executed as follows, with _finished as separators, MV and variables need double quotes
[[email protected] ~]$ ls *.jpg |awk-f "_finished" ' {print "MV" $ "" $1$2 ""} ' MV Stu_102999_1_finished.jpg stu_102999_1.jp GMV stu_102999_2_finished.jpg STU_102999_2.JPGMV stu_102999_3_finished.jpg STU_102999_3.JPGMV stu_102999_4_ Finished.jpg STU_102999_4.JPGMV stu_102999_5_finished.jpg stu_102999_5.jpg
Method Three: Rename renaming
[Email protected] ~]$ rename "_finished" "*.jpg
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Method four: For loop plus variable partial intercept
[[email protected] ~]$ for file in ' ls *.jpg ';d o mv $file ' echo ${file%_finished*}.jpg ';d one;
Not using echo can also be implemented
[[email protected] ~]$ for file in ' ls *.jpg ';d o mv $file ${file%_finished*}.jpg;done;
The result of the change is as follows:
Stu_102999_1.jpg
Stu_102999_2.jpg
Stu_102999_3.jpg
Stu_102999_4.jpg
Stu_102999_5.jpg
This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://mofansheng.blog.51cto.com/8792265/1743432
Shell script: Bulk Modify file name (delete characters in file name)