在linux下,用于批量修改文件名的一个简单的shell脚本。 代码如下:
复制代码代码示例:
#!/bin/sh
# 批量修改文件名 # 需传入三个以上的参数 $1 $2 $3... #先判断参数 参数要3个以上 # we have less than 3 arguments. Print the help text: 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 |