#!/bin/bash
set -x
if [ $# -lt 2 ];then
echo "Usage:rename oldfilename newfilename"
exit 1;
fi
oldfilename=`dirname "$1"`/`basename "$1"`
newfilename=`dirname "$2"`/`basename "$2"`
if [ ! -f "$oldfilename" ];then
echo "input filename please"
exit 1;
fi
ls `dirname "$newfilename"`|grep "^`basename $newfilename`$">/dev/null 2>&1
if [ $? == 0 ];then
echo "the file already have ,do you want continue"
while true;do
read x
if [ "$x" = Y -o "$x" = y ]; then
mv -f "$oldfilename" "$newfilename"
break;
else
while true ;do
echo "please input another newfilename"
read ofilename
ls `dirname $ofilename` |grep "^`basename $ofilename`$">/dev/null 2>&1
if [ $? == 0 ];then
echo "the file already have ,do you want continue"
read y
if [ "$x" = 'Y' -o "$x" = 'y' ];then
mv -f "$oldfilename" "$ofilename"
break;
else
continue;
fi
else
mv "$oldfilename" "$ofilename"
break;
fi
done
break;
fi
done
else
mv "$oldfilename" "$newfilename"
fi
bash 脚本 实现文件的重命名
最新推荐文章于 2023-08-11 12:03:30 发布