Shell script: Batch modify file name (add character in file name)

Source: Internet
Author: User

Examples are as follows: Batch create 10 random string of files, require each file name after adding _aaa, suffix name unchanged;

[[email protected] goodboy]# ls

adddbbdedf.html baacjaiija.html bhcfaabcfh.html dgjdcdfbca.html efejadfdji.html

agdhcdeaje.html bgffbffjcg.html cbbiebdafh.html diadebbhag.html jcajafgejf.html

Script 1:

[email protected] ~]# cat 02.sh#!/bin/bash#written by [email protected]path=/goodboy[-D $path] && CD $pathfor F ile in ' ls ' does mv $file ' echo $file |sed ' s/\ (. *\) \.\ (. *\)/\1_aaa.\2/g ' done


Explanatory notes:

Replace with SED, the 1th () bracket of the regular expression represents the file name \1; Use \ to take off the meaning, representing the delimiter;

The 2nd parenthesis inside represents the suffix HTML content namely \2;

Use this method to add a. symbol in the substitution.


The effect of the change is as follows:

[[Email protected] goodboy]# ll-rw-r--r-- 1 root root 0 2 Month    17 17:40 adddbbdedf_aaa.html-rw-r--r-- 1 root root 0 2 Month   17  17:40 agdhcdeaje_aaa.html-rw-r--r-- 1 root root 0 2 Month   17 17:40  baacjaiija_aaa.html-rw-r--r-- 1 root root 0 2 Month   17 17:40  Bgffbffjcg_aaa.html-rw-r--r-- 1 root root 0 2 Month   17 17:40  Bhcfaabcfh_aaa.html-rw-r--r-- 1 root root 0 2 Month   17 17:40  Cbbiebdafh_aaa.html-rw-r--r-- 1 root root 0 2 Month   17 17:40  Dgjdcdfbca_aaa.html-rw-r--r-- 1 root root 0 2 Month   17 17:40  Diadebbhag_aaa.html-rw-r--r-- 1 root root 0 2 Month   17 17:40  Efejadfdji_aaa.html-rw-r--r-- 1 root rooT 0 2 Month   17 17:40 jcajafgejf_aaa.html 


Script 2:

#!/bin/bash#written by [email protected]path=/goodboy[-D $path] && cd $pathfor file in ' ls ' do mv $file ' echo $fi Le|sed ' s/\ (. *\) \ (\.. *\)/\1_aaa\2/g ' done


Explanatory notes:

Also use SED to replace, the regular expression, and the above is the difference between the 2nd parentheses inside the content, representing the. html delimiter and suffix name as a whole, replace the contents of the words do not need to be added separately. The delimiter also needs to use \ to take off the meaning;


You can use the Sed-r parameter, it seems to be a lot more refreshing, do not need to remove the meaning;

mv $file ' echo $file |sed-r ' s/ (.*)(\.. *)/\1_aaa\2/g '


Everyone has a better way, welcome to share knowledge ~

This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://mofansheng.blog.51cto.com/8792265/1743016

Shell script: Batch modify file name (add character in file name)

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.