linux脚本批量修改文件名称,探讨:批量修改文件名的shell脚本

代码如下:

复制代码 代码示例:

#!/bin/sh

# 批量修改文件名

# 需传入三个以上的参数 $1 $2 $3...

#先判断参数 参数要3个以上

# we have less than 3 arguments. Print the help text:

# edit by www.jquerycn.cn

if [ $# -lt 3 ] ; then

cat <

ren -- renames a number of files using sed regular expressions

USAGE: ren 'regexp' 'replacement' files...

EXAMPLE: rename all *.HTM files in *.html:

#这里使用ren 'HTM$' 'html' *.HTM  ...'HTM$' 这是指文件名的尾部,作者提示这样可以漂亮修改后缀名。

ren 'HTM$' 'html' *.HTM

HELP

exit 0

fi

#取前面两个字,替换旧文件名部分字符串 和 新的字符串

OLD="$1"

NEW="$2"

# The shift command removes one argument from the list of

# command line arguments.

#这里比较关键,两次shift就是把$3变成$1,下面才能正常使用$*,才可以正常取文件列表

shift

shift

# $* contains now all the files:

#处理过程

for file in $*; do

if [ -f "$file" ] ; then

#输出处理

newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`

if [ -f "$newfile" ]; then

echo "ERROR: $newfile exists already"

else

echo "renaming $file to $newfile ..."

mv "$file" "$newfile"

fi

fi

done

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值