我们先了解下rename命令:
定义:批量重命名文件
用法:rename from to file
英语描述:rename will rename the specified files by replacing the first occurrence of from in their name by to.
译:rename是将"file"中的"from" 替换成"to"
描述:file指原文件文件名
from指原文件名的某个字符串
to 指将要替换成的字符串
例1:
#ls
hunk.txt
#rename txt TXT hunk.txt
#ls
hunk.TXT
例二:
rename .htm .html *.htm #将原文件中以.htm 结尾的文件全部更改名称为以.html结尾的文件
注:rername 结合循环结构或者通配符,则可以达到批量修改文件名
接下来,我们来解决题目:
要求:将文件名中的hunk批量替换成redhat,并且扩展名改成大写。
例如:GHC_hunk_7.doc替换成GHC_redhat_7.DOC
[root@localhost ~]# ls /data/hunk/
GHC_hunk_7.doc Mq_hunk_6.doc Rfa_hunk_1.doc tM_hunk_4.doc VY_hunk_2.docho_hunk_8.doc PA_hunk_1.doc slZ_hunk_4.doc Tq_hunk_5.doc zDmz_hunk_2.doc[root@localhost ~]# vim file1.sh
#!/bin/bash
path1=/data/hunkcd ${path1}ls1=`ls | grep hunk `for i in ${ls1}do/usr/bin/rename 'hunk' 'redhat' $idonels1=`ls | grep redhat`for i in ${ls1}do/usr/bin/rename 'doc' 'DOC' $idone[root@localhost ~]# bash file2.sh
[root@localhost ~]# ls /data/hunk/
GHC_redhat_7.DOC Mq_redhat_6.DOC Rfa_redhat_1.DOC tM_redhat_4.DOC VY_redhat_2.DOC
ho_redhat_8.DOC PA_redhat_1.DOC slZ_redhat_4.DOC Tq_redhat_5.DOC zDmz_redhat_2.DOC