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:
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:
Script 2:
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