专栏名称: ShawnMagic
什么时候可以毕业ಥ_ಥ
今天看啥  ›  专栏  ›  ShawnMagic

『Shell Script 练习』批量替换文件名中的字符

ShawnMagic  · 简书  ·  · 2020-08-04 18:16

文章预览

为啥搞这个

我的项目比较复杂,又好多个材料,时期,组学的数据,公司回来的结果五花八门,特别是result里面的文件名字更是又臭又长。之前老写类似:

for i in `ls *xxx*; do mv $i `echo $i | gsed 's/aaa/bbb/g'`;done

单行确实很方便。
不过前几天玩了玩R的 getopt 现在又想练习下shell的 getopts ,就把这个替换文件名字符写成了个小脚本。当然现在还很傻瓜,以后估计会完善... 练练手吧,可能是把简单问题复杂化了...

脚本

#! /bin/bash
set -e ## 报错打断,防止一直错下去
## 帮助内容
func(){
    echo "Usage:"
    echo "changName.sh [-c conf] [-s string] [-r replace] [-b backup] "
    echo "Description"
    echo "[-c]:conf,    The file name list in one config file, one file per line"
    echo "[-s]:string,  The string in your file name you want to remove"
    echo "[-r]:replace,     The string in your file name you want to add"
    echo "[-b]:backup,  Do you want backup for you original files?"
    exit -1
}
## 是否备份原始文件,如果不写-b默认不备份
backup="no"
## 设置各个参数
while getopts 'h:c:s:r:b' OPT;
do
    case $OPT in
        c) conf="$OPTARG";;
        s) string=`echo "$OPTARG"`;;
        b) backup="yes";;
        r) replace=`echo "$OPTARG"`;;
        h) func;;
        ?) func;;
    esac
done
## 提示你的参数设置
echo "Your setting:"
echo "The strings you want remove:" ${string}
echo "The strings you want to replace the removed strings:" ${replace}
echo "The original file was retained:" ${backup}
while read id
do
    file=$(basename $id) 
    old=`echo $file` ## 这里要提取的是字符,echo你上面文件名赋值的结果就是stirng
    new=`echo $old | gsed "s/$string/${replace}/g"` ## 你改后的文件名 linux的话把gsed改成sed,如果sed中有变量的话需要双引号
    echo "The old filename" "##"$old"##" "has been replaced as" "##"$new"##"
    ## 执行
    if [[ $backup == "yes" ]]; then
        #do not backup
        cp ${file} ${new}; else
        #backup
        mv ${file} ${new}
    fi
done<$conf

嗯 本来一行的事写了这来长ಥ_ಥ....不过这里多了个是否备份的选项...毕竟我之前干过一下把文件搞丢了的情况.... bash下一顿操作很可能让你的文件找不到了...所以新手还是加上 -b 吧。

## 写个快捷方式
echo "alias cname='sh ~/08.MyScript/shell/changeName.sh'" >> ~/.bash_alias
source ~/.bash_alias
## 看下帮助
cname -h 
/Users/sirus/08.MyScript/shell/changeName.sh: option requires an argument -- h
Usage:
changName.sh [-c conf] [-s string] [-r replace] [-b backup] 
Description
[-c]:conf,  The file name list in one config file, one file per line
[-s]:string,    The string in your file name you want to remove
[-r]:replace,   The string in your file name you want to add
[-b]:backup,    Do you want backup for you original files?
## 结构
tree 
.
├── adf.xxx.xls
├── ba.xxx.xls
├── qwed.xxx.xls
├── wed.xxx.xls
└── wesd.xxx.xls

实际操作

## 将xxx换成yyy并且保留原始文件
ls *xxx* >config ## 做一个配置文件
cname -c config -s 'xxx' -r 'yyy' -b
Your setting:
The strings you want remove: xxx
The strings you want to replace the removed strings: yyy
The original file was retained: yes
The old filename ##adf.xxx.xls## has been replaced as ##adf.yyy.xls##
The old filename ##ba.xxx.xls## has been replaced as ##ba.yyy.xls##
The old filename ##qwed.xxx.xls## has been replaced as ##qwed.yyy.xls##
The old filename ##wed.xxx.xls## has been replaced as ##wed.yyy.xls##
The old filename ##wesd.xxx.xls## has been replaced as ##wesd.yyy.xls##

看下结果

tree
.
├── adf.xxx.xls
├── adf.yyy.xls
├── ba.xxx.xls
├── ba.yyy.xls
├── config
├── qwed.xxx.xls
├── qwed.yyy.xls
├── wed.xxx.xls
├── wed.yyy.xls
├── wesd.xxx.xls
└── wesd.yyy.xls
## 将xxx删掉,而且不保留原始文件
cname -c config -s 'xxx' -r '' -b
tree
Your setting:
The strings you want remove: xxx
The strings you want to replace the removed strings:
The original file was retained: no
The old filename ##adf.xxx.xls## has been replaced as ##adf..xls##
The old filename ##ba.xxx.xls## has been replaced as ##ba..xls##
The old filename ##qwed.xxx.xls## has been replaced as ##qwed..xls##
The old filename ##wed.xxx.xls## has been replaced as ##wed..xls##
The old filename ##wesd.xxx.xls## has been replaced as ##wesd..xls##

当然是支持正则表达式的。就不举例子了。可以自己玩一下

………………………………

原文地址:访问原文地址
快照地址: 访问文章快照
总结与预览地址:访问总结与预览