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)