教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 探讨:批量修改文件名的shell脚本

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

发布时间:2014-10-14   编辑:jiaochengji.com
本文介绍下,在linux下,用于批量修改文件名的一个简单的shell脚本。有需要的朋友,参考下吧。

代码如下:

复制代码 代码示例:
#!/bin/sh
# 批量修改文件名
# 需传入三个以上的参数 $1 $2 $3...
#先判断参数 参数要3个以上
# we have less than 3 arguments. Print the help text:  
# edit by www.jiaochengji.com
   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

您可能感兴趣的文章:
linux批量修改文件名的shell脚本
探讨:批量修改文件名的shell脚本
shell批量修改文件后缀名
批量下载人人网好友的某一相册的shell脚本
批量创建用户并设置密码的脚本
shell批量重命名目录下的文件(修改为从某个数字开始的数字)
批量修改密码的shell脚本
一个批量修改密码的shell脚本
awk 单行命令批量修改文件名
Linux批量重命名文件的五个例子

[关闭]
~ ~