linux脚本生成随机文件名,shell脚本:批量修改文件名(文件名中添加字符)

举例如下:批量创建10个随机字符串的文件,要求每个文件名后面添加_aaa,后缀名不变;

[root@localhost goodboy]# ls

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

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

脚本1:[root@localhost ~]# cat 02.sh

#!/bin/bash

#written by mofansheng@2016-02-17

path=/goodboy

[ -d $path ] && cd $path

for file in `ls`

do

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

done

解释说明:

使用sed替换,正则表达式第1个()括号里面代表文件名即\1;中间. 使用\进行脱意,代表分隔符;

第2个括号里面代表后缀html内容即\2;

使用此方法需要在替换中添加.符号;

更改后的效果如下:[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

脚本2:#!/bin/bash

#written by mofansheng@2016-02-17

path=/goodboy

[ -d $path ] && cd $path

for file in `ls`

do

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

done

解释说明:

同样使用sed替换,正则表达式,与上面的区别在于第2个括号里面的内容,代表.html 分隔符和后缀名为一体,替换内容的话不需要再单独加.点;.分隔符同样需要使用\进行脱意;

可以使用sed -r参数,看起来就清爽很多,不需要\脱意;

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

大家有更好的方法,欢迎分享知识~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值