shell 脚本 ---批量重命名
时间:2011-05-24
来源:闲人草堂
在手机上看
手机扫描阅读
工作中遇到一个小需求,需要对一系列的文件进行重命名。
原始格式是string_1.obj,现在想把文件名中的数字变成定长的,不足位数往前补零,即变成string_001.obj这种形式。
自己学着写了个脚本,很菜的,贴上来请大家指点一下。
#! /bin/bash
# this script is used to rename files
# the orignal filename is as XXXX_1.obj
# and the final filename is as XXX_0001.obj the width of the num is given as a parameter
rm result.txt
export suffix=".png"
find . -name "*$suffix" > result.txt
cat result.txt | while read oneline
do
export orifilename=${oneline##*/} #cut off the path of filename
export prefilename=${orifilename%_*} #without "_"
export numfilename=${oneline##*_}
export numfilename=${numfilename%.*} #get the num in filename
((i=$1-${#numfilename})) #get the num of zero which is needed
export zeros=`echo $numfilename | awk '{printf "%0'"$i"'d",0}{print}'`
export finalfilename=$prefilename"_"$zeros$suffix
#echo "$orifilename, $finalfilename"
# rename "s/$orifilename/$finalfilename/" ./$finalfilename
mv $orifilename $finalfilename
done
echo "Done"
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28















