[[email protected] 1]# cat mv.sh
#!/bin/bash
for file in `ls *.jpg`
do
/bin/mv $file `echo "${file%finished*}.jpg"`
done
[[email protected] 1]# ll
total 8
-rw-r--r-- 1 root root 106 Dec 26 05:33 a.log
-rw-r--r-- 1 root root 90 Dec 26 05:39 mv.sh
-rw-r--r-- 1 root root 0 Dec 26 05:34 stu_102999_1_finished.jpg
-rw-r--r-- 1 root root 0 Dec 26 05:34 stu_102999_2_finished.jpg
-rw-r--r-- 1 root root 0 Dec 26 05:34 stu_102999_3_finished.jpg
-rw-r--r-- 1 root root 0 Dec 26 05:34 stu_102999_4_finished.jpg
[[email protected] 1]# sh mv.sh
[[email protected] 1]# ll
total 8
-rw-r--r-- 1 root root 106 Dec 26 05:33 a.log
-rw-r--r-- 1 root root 90 Dec 26 05:39 mv.sh
-rw-r--r-- 1 root root 0 Dec 26 05:34 stu_102999_1_.jpg
-rw-r--r-- 1 root root 0 Dec 26 05:34 stu_102999_2_.jpg
-rw-r--r-- 1 root root 0 Dec 26 05:34 stu_102999_3_.jpg
-rw-r--r-- 1 root root 0 Dec 26 05:34 stu_102999_4_.jpg
思路:
[[email protected] 1]# k="stu_102999_1_finished.jpg"
[[email protected] 1]# echo ${k%finished*}.jpg
stu_102999_1_.jpg
2、用${string/%substring/replace} 修改扩展名
[[email protected] 201126_02]# ls
1.log 1.sh camz.HTML icpc.HTML leco.HTML wjb.HTML zshu.HTML
将后缀为HTML改为TH
[[email protected] 201126_02]# vim 1.sh
#!/bin/bash
for f in `ls *.HTML`
do
mv $f `echo ${f/%HTML/TH}`
done
[[email protected] 201126_02]# sh -x 1.sh
++ ls camz.HTML icpc.HTML leco.HTML wjb.HTML zshu.HTML
+ for f in ‘`ls *.HTML`‘
++ echo camz.TH
+ mv camz.HTML camz.TH
+ for f in ‘`ls *.HTML`‘
++ echo icpc.TH
+ mv icpc.HTML icpc.TH
+ for f in ‘`ls *.HTML`‘
++ echo leco.TH
+ mv leco.HTML leco.TH
+ for f in ‘`ls *.HTML`‘
++ echo wjb.TH
+ mv wjb.HTML wjb.TH
+ for f in ‘`ls *.HTML`‘
++ echo zshu.TH
+ mv zshu.HTML zshu.TH
[[email protected] 201126_02]# ls
1.log 1.sh camz.TH icpc.TH leco.TH wjb.TH zshu.TH