Shell practice: Using the For loop to bulk modify file name extensions

Source: Internet
Author: User

Shell practice: Using the For loop to bulk modify file name extensions


Description: (1) Under Linux batch modify the file name, the command shown in the "_linux" removed.

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9C/F6/wKiom1l4QmiwnTknAAAUJsh3q_4321.png "title=" 11. PNG "alt=" Wkiom1l4qmiwntknaaaujsh3q_4321.png "/>

(2) Use the For loop script.

Ideas: The basic problem-solving ideas, first to make a single file name, and then use the cycle to achieve batch renaming, which is more conventional practice, you can also use Rename Professional renaming tool.

  1. Method One:

    Description: Use the Cut, sed tool

    The script is as follows:

    #!/bin/bash

    Cd/test

    For i in ' Ls|grep. *.jpg '

    Do

    MV $i ' echo $i |cut-d '. '-f1|sed ' s#_linux## '. jpg

    Done

    Script execution, as shown in:

    650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9C/F6/wKiom1l4RrCSUverAAAXg_6ebbY342.png "title=" 11. PNG "alt=" Wkiom1l4rrcsuveraaaxg_6ebby342.png "/>

  2. Note: In order to perform the following experiments, we need to perform data recovery for the above files using the cut and awk tools.

    The script is as follows:

    #!/bin/bash

    #date: 2017-7-26

    Cd/test

    For i in ' Ls|grep. *.jpg '

    Do

    MV $i ' echo $i |cut-d '. '-f1|awk-f ' _ ' {print $ "_" $ "_" $ $} "_linux.jpg

    Done

    Script execution, as shown in:

    650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9C/F7/wKiom1l4SH3jXXLMAAAZmHYC0Xw771.png "title=" 11. PNG "alt=" Wkiom1l4sh3jxxlmaaazmhyc0xw771.png "/>

  3. We then use the second method to bulk modify the file name.

    Method Two: Use the cut, awk tool

    The script is as follows:

    #!/bin/bash

    #date: 2017-7-26

    Cd/test

    For i in ' Ls|grep. *.jpg '

    Do

    MV $i ' echo $i |cut-d '. '-f1|awk-f ' _ ' {print $ ' _ ' $ $ ' _ ' $ $} '. jpg

    Done


    Script execution, as shown in:

    650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9C/F6/wKiom1l4RrCSUverAAAXg_6ebbY342.png "title=" 11. PNG "alt=" Wkiom1l4rrcsuveraaaxg_6ebby342.png "/>

  4. Method Three: Use Cut, sed tools

    The script is as follows:

    #!/bin/bash

    Cd/test

    For i in ' Ls|grep. *.jpg '

    Do

    MV $i ' echo $i |cut-d '. '-f1|sed-r ' s# (. *) _linux#\1# '. jpg

    Done

  5. Method four: Using the SED tool

    The script is as follows:

    #!/bin/bash

    Cd/test

    For i in ' Ls|grep. *.jpg '

    Do

    MV $i ' echo $i |sed ' s#_linux## '

    Done

  6. Method Five: Use the Rename tool

    #!/bin/bash

    Cd/test

    For i in ' Ls|grep. *.jpg '

    Do

    echo $i |rename "_linux" "" "*.jpg

    Done


This article is from the "Hand of the Paladin Control" blog, please make sure to keep this source http://wutengfei.blog.51cto.com/10942117/1951146

Shell practice: Using the For loop to bulk modify file name extensions

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.