[b]今天同事让我把一些文件发给她,并且把这些文件的后缀都改成html,考虑到文件数量较多,手工改肯定不行,所以就写了个脚本:[/b]
#!/bin/bash
#########################################
#
# 批量修改文件后缀
#
# 运行方式:changeFileSuffix.sh vm html
#
# diaocow 2012-08-02
#
#
#########################################
#原始后缀名
oriSuffix=$1
#新的后缀名
newSuffix=$2
if [ -z "$oriSuffix" ]; then
echo "请输入原始后缀名! eg. changeFileSuffix.sh vm html"
exit 1
fi
if [ -z "$newSuffix" ]; then
echo "请输入新的后缀名! eg. changeFileSuffix.sh vm html"
exit 1
fi
if [ "$oriSuffix" == "$newSuffix" ]; then
echo "新旧后缀名相同请重新输入! eg. changeFileSuffix.sh vm html"
exit 1
fi
for file in `ls`
do
echo $file | grep -q $oriSuffix
if [ -f $file -a "$?" == "0" ]; then
filename=${file%.*} # bash内建字符操作
newFilename="$filename.$newSuffix"
echo "mv $file $newFilename"
mv $file $newFilename
fi
done