Shell batch file renaming

Source: Internet
Author: User


[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 05:33 A.log
-rw-r--r--1 root root from Dec 05:39 mv.sh
-rw-r--r--1 root root 0 Dec 05:34 stu_102999_1_finished.jpg
-rw-r--r--1 root root 0 Dec 05:34 stu_102999_2_finished.jpg
-rw-r--r--1 root root 0 Dec 05:34 stu_102999_3_finished.jpg
-rw-r--r--1 root root 0 Dec 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 05:33 A.log
-rw-r--r--1 root root from Dec 05:39 mv.sh
-rw-r--r--1 root root 0 Dec 05:34 stu_102999_1_.jpg
-rw-r--r--1 root root 0 Dec 05:34 stu_102999_2_.jpg
-rw-r--r--1 root root 0 Dec 05:34 stu_102999_3_.jpg
-rw-r--r--1 root root 0 Dec 05:34 stu_102999_4_.jpg

Ideas:

[Email protected] 1]# k= "Stu_102999_1_finished.jpg"

[Email protected] 1]# echo ${k%finished*}.jpg

Stu_102999_1_.jpg

2. Modify the extension with ${string/%substring/replace}

[[email protected] 201126_02]# ls
1.log 1.sh Camz. HTML ICPC. HTML Leco. HTML WJB. HTML Zshu. Html

Change the suffix to html to 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


Shell batch file renaming

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.