使用bash-shell去除文件名中的数字


配置SSL证书的时候遇到一个问题,certbot有时候会在证书的名字后面多加一个数字,需要使用shell去除,这里使用正则表达式的子匹配功能,选出第一个匹配的区域,在其他场景下可以根据需要更改BASH_REMATCH[]的值以截取需要的部分。

1
2
3
4
5
6
7
8
9
10
11
    if [[ $file =~ ^(.+)+[0-9].pem ]] ; then
# display original file name
echo "< $file"
# grab the rest of the filename from
# the regex capture group
newname="${BASH_REMATCH[1]}.pem"
echo "> $newname"
# uncomment to move
mv -i "$file" "$newname"
fi
done

该内容采用 CC BY-NC-ND 4.0 许可协议,著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。