一个批量重命名文件名的shell脚本

 问题:

文件夹下有如下文件:

 

-rw-r--r--   1 root root 139000 Sep 19 09:25 code.mass.zip
-rw-r--r--   1 root root  71473 Jun  5  2007 code.tar.gz
-rw-r--r--   1 root root  17419 Sep  5  2000 index.html
drwxr-xr-x   2 root root   4096 Sep 27 11:49 test1 JLKJL
drwxr-xr-x   2 root root   4096 Sep 27 11:49 test2 kkkk
drwxr-xr-x   2 root root   4096 Sep 27 11:49 test3 llll
drwxr-xr-x   2 root root   4096 Sep 27 11:49 test4 uuu
-rw-r--r--   1 root root  22789 Jun  5  2007 workaround.html
drwxr-xr-x   2 root root   4096 Aug  2  2007 workaround_files

 

想把 test2 kkkk 这种文件重命名为 tt2_kkkk 这样的文件名,脚本如何处理?

 

结果:

for file in *; do echo $file | grep -q 'test[1-4].*'; if [ $? = 0 ]; then newname=`echo $file |sed 's/test/([1-4]/) */(.*/)/tt/1_/2/g'`; mv "$file" $newname; fi; done

 

即:

for file in *;

do

    echo $file | grep -q 'test[1-4].*';

    if [ $? = 0 ];

    then

        newname=`echo $file |sed 's/test/([1-4]/) */(.*/)/tt/1_/2/g'`;

        mv "$file" $newname;

    fi;

done

 

中间的难点是 文件名中本来有空格,这个需要处理。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值