|
一个用来批量改名的脚本,支持通配符,可以指定前缀名和后缀数字的宽度,原有扩展名则保留不变。结束后在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] |
|