LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 763|回复: 0

贴一段文件批量命名的脚本,处理大量的数码照片很方便!

[复制链接]
发表于 2005-10-12 19:08:17 | 显示全部楼层 |阅读模式
#!/bin/bash
#
#        comcat  <jiankemeng@gmail.com>
#        usage: rn [-c start_count_number] [-p prefix_string] target_dir
###############################################################

COUNT=1000

usage()
{
        echo "Usage: $0 [-c START_COUNT_NUMBER][-p PREFIX_STRING ] target_dir"
}

if [ $# -lt 1 ]
then
        usage
        exit 1
fi

while getopts ":c : p:" opt
do
        case $opt
        in
                c)
                        COUNT="$OPTARG";;
                p)
                        PREFIX="$OPTARG";;
                \?)
                        usage
                        exit 1;;
        esac
done

if [ $OPTIND -gt $# ]
then
        echo "No target directory specified!"
        exit 2
fi

shift $(($OPTIND-1))

TARGET=$1

for file in $(find $TARGET -type f)
do

        if [ -f $file ]
        then
                if [ -n "$PREFIX" ]
                then
                        new_name=`echo $file | sed -n 's|\(.*/\).\+|\1|p'`$PREFIX
                        new_name=$new_name\_$COUNT`echo $file | awk '{gsub(/^.+\./,".");print;}'`
                else
                        new_name=`echo $file | sed -n 's#\(.*/\).\+#\1#p'`$COUNT`echo $file | awk \
                                '{gsub(/^.+\./,".");print;}'`
                fi
                mv $file $new_name
                COUNT=`expr $COUNT + 1`
        elif [ -d $file ]
        then
                echo "$file is a directory"
        fi

done
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表