LinuxSir.cn,穿越时空的Linuxsir!

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

批量更名脚本

[复制链接]
发表于 2005-11-9 21:50:23 | 显示全部楼层 |阅读模式
一个用来批量改名的脚本,支持通配符,可以指定前缀名和后缀数字的宽度,原有扩展名则保留不变。结束后在home目录会生成一个log文件。
这个脚本对付大量图片改名的时候很好用

刚学bash,小试钝刀,所有代码几乎都是幼稚粗陋的,不免贻笑大方。

# Sample: batren "~/home/user/*.jpg" mypic 3
# This sample command will rename the files(*.jpg) under "~/home/user/" to
# mypic001.jpg, mypic002.jpg ... mypic012.jpg ... mypic789.jpg ... etc.

比较恶心的是目标文件集要用双引号引起来

[php]
#!/bin/bash
# batren: A shell for renaming files in batches B-)
#         lolita@linuxsir.cn 2005
# ----------------------------------------------------------------------------
# Usage: batren <original files> <prefix of newfiles> <postfix width>
# ----------------------------------------------------------------------------
# original files: files to be renamed, wildcards like '*' '?'and paths are allowed.
# prefix of newfiles: you may guess its meaning ,right?
# postfix width: the width of numberic-postfix of the new filename
# ----------------------------------------------------------------------------
# Sample: batren "~/home/user/*.jpg" mypic 3
# This sample command will rename the files(*.jpg) under "~/home/user/" to
# mypic001.jpg, mypic002.jpg ... mypic012.jpg ... mypic789.jpg ... etc.
# ----------------------------------------------------------------------------
# enjoy yourself ~
# ----------------------------------------------------------------------------

origIFS=$IFS
IFS='
'
## this shell's executing name
myname=${0##*/}

## check parameters' amount
if [ $# -ne 3 ]
then
        echo "+----------------------------------------------------------------------------------+"
        echo "          Utility for batch-renaming files. by Lolita@linuxsir.cn 2005"
        echo "  Usage: ${myname} <\"original files\"> <newfile prefix> <newfile postfix width>"
        echo "  Sample:  ${myname} \"~/home/myhome/*.jpg\" mypic 3"
        echo "!!! <orig-file> SHOULD BE quoted by \"\" otherwise the result may be incorrect!!! "
        echo "+----------------------------------------------------------------------------------+"
        exit 1
fi


##  check numeric-postfix width
case "$3" in
[1-4])
        ;;
*)
        echo "ARAMETERS ERROR: $3 : postfix-with should be a number ranged from 1 to 4"
        exit 1;;
esac


prefix=$2
width=$3

##  get the original-files' path and names
destfiles=${1#\"}
destfiles=${destfiles%\"}

homeflag="~/"
destfiles_without_homeflag=${destfiles#$homeflag}

if [ "$destfiles_without_homeflag" != "$destfiles" ]; then
        destfiles=${HOME}/${destfiles_without_homeflag}
fi
destpath=${destfiles%/*}

if [ "$destpath" != "$destfiles" ] ; then
        destpath="${destpath}/"
else
        destpath="./"
fi

destfiles=${destfiles##$destpath}


## get the max-value of the numeric-postfix
maxpostfix=1
i=1
while [ $i -le $width ]
do
        maxpostfix=$(($maxpostfix*10))
        i=$(($i+1))
done       
maxpostfix=$(($maxpostfix-1))



## procedure for renames in batches :
i=1
count=0 ## counter for renames
logfile="${HOME}/${myname}-$(date +%Y-%m-%k-%M-%S).log"
for orig_file_with_path in $(find ${destpath} -maxdepth 1 -name "${destfiles}" -type f -exec ls -1 {} \;)
do
        postfix=$(printf "%0${width}d" "$i")
        orig_file="${orig_file_with_path##*/}"

        file_extension=${orig_file##*.}

        if [ "$orig_file" = "$file_extension" ]; then
                file_extension=""
        else
                file_extension=".${file_extension}"
        fi
       
        if [ -e  ${destpath}${prefix}${postfix}${file_extension} ] ; then
                echo "*** file \"${destpath}${prefix}${postfix}${file_extension}\" exists, skipped. ***"
                i=$(($i+1))
                continue
        fi
       
        mv -v "$orig_file_with_path"  "${destpath}${prefix}${postfix}${file_extension}"
        echo "${orig_file_with_path} ----> ${destpath}${prefix}${postfix}${file_extension}" >> $logfile
       
        i=$(($i+1))
        count=$(($count+1))
        if [ $i -gt $maxpostfix ] ; then
                break
        fi
done



echo "----------------------------------"
echo "There are $count file(s) renamed !"
echo "A log file is created: $logfile"

# restore the IFS
IFS=$origIFS

exit 0
[/php]
发表于 2005-11-9 22:36:54 | 显示全部楼层
支持下:%
回复 支持 反对

使用道具 举报

发表于 2005-11-10 02:06:16 | 显示全部楼层
Post by Freebird
支持下:%

版主支持的,我都支持!!! :% :% :%
回复 支持 反对

使用道具 举报

发表于 2005-11-10 12:01:30 | 显示全部楼层
支持下~
回复 支持 反对

使用道具 举报

发表于 2005-11-10 18:12:33 | 显示全部楼层
rename 功能就超强。
回复 支持 反对

使用道具 举报

发表于 2005-11-13 22:50:02 | 显示全部楼层
前一阵学FVWM时就想把网上下的图标都用convert 命令改为png格式的,然后用:
convert *.jpg *.png
结果出现一堆*_1.png, *_2.png, faint!
就拿楼主的参照一下,看看怎样改着,谢谢
回复 支持 反对

使用道具 举报

发表于 2005-11-14 09:54:33 | 显示全部楼层
推荐用rename
回复 支持 反对

使用道具 举报

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

本版积分规则

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