30题之第二题
19.1.2 企业Shell面试题2:批量改名特殊案例
将以上面试题19.1.1中结果文件名中的oldboy字符串全部改成oldgirl(最好用for循环实现),并且将扩展名html全部改成大写。
rename oldboy.html oldgirl.HTLM *_oldboy.html
[root@jenkins scripts]# ls
2.sh daargnrenb_oldboy.html expect_test.sh mqqbylcynk_oldboy.html test1_2.sh tt.sh
3.sh eqtaoyqljo_oldboy.html hjnovpxobc_oldboy.html qxtacetuas_oldboy.html test1.sh vfrmwfbyqq_oldboy.html
cdfkdshngs_oldboy.html expect2_test.sh kpgmywjidh_oldboy.html test tgpmpmufdv_oldboy.html yneepywxaw_oldboy.html
[root@jenkins scripts]# rename oldboy.html oldgirl.HTLM *_oldboy.html
[root@jenkins scripts]# ls
2.sh daargnrenb_oldgirl.HTLM expect_test.sh mqqbylcynk_oldgirl.HTLM test1_2.sh tt.sh
3.sh eqtaoyqljo_oldgirl.HTLM hjnovpxobc_oldgirl.HTLM qxtacetuas_oldgirl.HTLM test1.sh vfrmwfbyqq_oldgirl.HTLM
cdfkdshngs_oldgirl.HTLM expect2_test.sh kpgmywjidh_oldgirl.HTLM test tgpmpmufdv_oldgirl.HTLM yneepywxaw_oldgirl.HTLM
上面是rename的方法一
=============================================
#!/bin/bash dir=/oldboy/ for n in `find $dir -type f -name "*oldboy.html"` do mv $n ${n//oldboy.html/oldgirl.HTML} done
方法二:
[root@jenkins scripts]# cat 2.sh #!/bin/bash ############################################################## # File Name: 2.sh # Version: V1.0 # Author: gaobo # Organization: 641627690@qq.com # Created Time : 2017-12-04 21:56:38 # Description: ############################################################## for i in `ls *_oldgirl.HTLM` do echo $i|sed -rn "s#(.*_)oldgirl.HTLM# mv & \1oldboy.html#gp"|bash done
[root@jenkins scripts]# sh 2.sh
mv cdfkdshngs_oldgirl.HTLM cdfkdshngs_oldboy.html
mv daargnrenb_oldgirl.HTLM daargnrenb_oldboy.html
mv eqtaoyqljo_oldgirl.HTLM eqtaoyqljo_oldboy.html
[ ! -d /oldboy ] && mkdir -p /oldboy cd /oldboy for i in `seq 10` do suiji=`tr -dc "a-z" </dev/urandom|head -c10` touch ${suiji}_oldboy.html done ls -l /oldboy #!/bin/bash dir=/oldboy [ -d $dir ] || mkdir -p $dir cd $dir for i in {1..10} do Mz=`uuidgen |tr '0-9-' 'a-z'|cut -c -10` touch ${Mz}_oldboy.txt done