Shell script: Bulk Modify file name (delete characters in file name)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.