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.
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 "/>
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 "/>
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 "/>
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
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
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