shell 脚本 ---批量重命名

工作中遇到一个小需求,需要对一系列的文件进行重命名。

原始格式是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=".obj"
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"
posted on 2011-05-24 12:43  闲人草堂 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jtf-china/archive/2011/05/24/2055278.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值