[root@PXE 1]# cat mv.sh
#!/bin/bash
for file in `ls *.jpg`
do
/bin/mv $file `echo "${file%finished*}.jpg"`
done
[root@PXE 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
[root@PXE 1]# sh mv.sh
[root@PXE 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
思路:
[root@PXE 1]# k="stu_102999_1_finished.jpg"
[root@PXE 1]# echo ${k%finished*}.jpg
stu_102999_1_.jpg
2、用${string/%substring/replace} 修改扩展名
[root@PXE 201126_02]# ls
1.log 1.sh camz.HTML icpc.HTML leco.HTML wjb.HTML zshu.HTML
将后缀为HTML改为TH
[root@PXE 201126_02]# vim 1.sh
#!/bin/bash
for f in `ls *.HTML`
do
mv $f `echo ${f/%HTML/TH}`
done
[root@PXE 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
[root@PXE 201126_02]# ls
1.log 1.sh camz.TH icpc.TH leco.TH wjb.TH zshu.TH
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。