shell script: batch edit file names (add / delete the file name characters)

Add character

For example as follows: Bulk create random string file 10, every file name behind _aaa added, the same suffix;

[root@localhost goodboy]# ls

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

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

Script 1:

[root@localhost ~]# cat 02.sh
#!/bin/bash
path=/goodboy
[ -d $path ] && cd $path
for file in `ls`
do
 mv $file `echo $file|sed 's/\(.*\)\.\(.*\)/\1_aaa.\2/g'`
done

explain:

Alternatively sed use, of a regular expression () represents the file name is the brackets \ 1; intermediate using \ deprotection intended, on behalf of the separator;

The first two brackets represents the suffix html content that is \ 2;

Using this method needs to be added in alternative symbols.;

 

After the effect of the changes are as follows:

[root@localhost goodboy]# ll
-rw-r--r-- 1 root root 0 2月  17 17:40 adddbbdedf_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 agdhcdeaje_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 baacjaiija_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 bgffbffjcg_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 bhcfaabcfh_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 cbbiebdafh_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 dgjdcdfbca_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 diadebbhag_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 efejadfdji_aaa.html
-rw-r--r-- 1 root root 0 2月  17 17:40 jcajafgejf_aaa.html

  

Script 2:

#!/bin/bash
path=/goodboy
[ -d $path ] && cd $path
for file in `ls`
do
 mv $file `echo $file|sed 's/\(.*\)\(\..*\)/\1_aaa\2/g'`
done
 

 

explain:

Similarly with sed Alternatively, regular expressions, with the difference that the content of the above two brackets, representing .html integral separator and suffix, then replace the contents do not need to add a separate point;. Separator also requires the use of \ carried off Italy;

 

You can use sed -r parameter, a lot of looks cool, it does not need to \ off Italy;

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

 

To delete a character

For example: batch rename, delete extra characters in the file name

Directory file name as required to remove _finished.

stu_102999_1_finished.jpg

stu_102999_2_finished.jpg

stu_102999_3_finished.jpg

stu_102999_4_finished.jpg

stu_102999_5_finished.jpg

 

The method can be achieved There are many:

Method a: for binding loop replacement sed

[baby@localhost ~]$ for file in `ls *.jpg`;do mv $file `echo $file|sed 's/_finished//g'`;done;

  

Method two: ls binding awk, bash output to perform

[baby@localhost ~]$ ls *.jpg |awk -F "_finished" '{print "mv "$0" "$1$2""}'|bash

   

Commands actually executed as follows, as a delimiter to _finished, mv and variable needs double quotes

[baby@localhost ~]$ ls *.jpg |awk -F "_finished" '{print "mv "$0" "$1$2""}'
mv stu_102999_1_finished.jpg stu_102999_1.jpg
mv stu_102999_2_finished.jpg stu_102999_2.jpg
mv stu_102999_3_finished.jpg stu_102999_3.jpg
mv stu_102999_4_finished.jpg stu_102999_4.jpg
mv stu_102999_5_finished.jpg stu_102999_5.jpg

  

Method three: rename renamed (need to install the rename)

[baby@localhost ~]$ rename "_finished" "" *.jpg

 

Method four: for cyclic loading and variable portion taken

[baby@localhost ~]$ for file in `ls *.jpg`;do mv $file `echo ${file%_finished*}.jpg`;done;

  

Can be achieved without the use of echo

[baby@localhost ~]$ for file in `ls *.jpg`;do mv $file ${file%_finished*}.jpg;done;

  

After changing the results are as follows:

stu_102999_1.jpg
stu_102999_2.jpg
stu_102999_3.jpg
stu_102999_4.jpg
stu_102999_5.jpg

Guess you like

Origin www.cnblogs.com/xuange306/p/12030129.html