您好,登錄后才能下訂單哦!
[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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。