用shell/ target=_blank class=infotextkey>shell脚本进行文件名大小写的转换,这里分享4种方法,供大家参考。
方法1、
方法2、
方法3、
方法4、
#!/bin/sh
# this script will change file name recursively with option
# -u: locase to upcase
# -l: upcase to lowcase
hint () {
echo " Usage: $0 [-l|-u] DIR1 [DIR2 DIR3...]
-l to lowcase
-u to upcase"
exit 1
}
if test $# -lt 2; then
echo "Too few arguments."
hint
fi
while [ "$1" ]; do
case $1 in
-l) ACTION="lo"
shift 1
;;
-u) ACTION="up"
shift 1
;;
*) if test -d $1; then
DIR="$DIR $1"
else
echo "no such directory --- $1"
hint
fi
shift
;;
esac
done
# echo $ACTION
# echo $DIR
FOUND=`find $DIR | sort -r`
for i in $FOUND; do
DN=`dirname $i`
BS=`basename $i`
loBS=`echo $BS | tr '[A-Z]' '[a-z]'`
upBS=`echo $BS | tr '[a-z]' '[A-Z]'`
NAME1=$DN/$BS
if [ "$ACTION" = "lo" ]; then
NAME2=$DN/$loBS
elif [ "$ACTION" = "up" ]; then
NAME2=$DN/$upBS
fi
if [ "$NAME1" = "$NAME2" ]; then
echo "****: $NAME1 ---x--- $NAME2 identical!"
else
echo "- renaming $NAME1 --> $NAME2"
mv $NAME1 $NAME2
fi
done
注意:
find 得到结果并不能直接拿来处理。
经过排序后(先处理最深的目录,由里向外)也无法使用。
以上代码,仅供学习参考,不适用于生产环境。