一个简单的遍历文件目录下文件类型数量的shell

#!/bin/bash  
txt_count=0
xml_count=0
map_count=0
other_count=0

DIRECTORY=$1
if [ "`ls -A $DIRECTORY`" = "" ]; then
  echo "$DIRECTORY is empty"
else
  echo "$DIRECTORY is not empty"
fi

    for file in ` ls $DIRECTORY `
    do
        if [ -f $DIRECTORY"/"$file ]
        then
        fileType=${file##*.}
        #后缀名大小写兼容,统一转换为小写进行判断
        fileType=$(echo $fileType | tr '[A-Z]' '[a-z]')
                        if [ "$fileType" = "txt" ];
                         then
                          txt_count=$(($txt_count+1))
                        elif  [ "$fileType" = "xml" ];
                         then
                          xml_count=$(($xml_count+1))
                        elif  [ "$fileType" = "map" ];
                         then
                          map_count=$(($map_count+1))
                        else
                          other_count=$(($other_count+1))
                        fi
                fi
    done
echo 'txt file count:'$txt_count
echo 'xml file count:'$xml_count
echo 'map file count:'$map_count
echo 'other file count:'$other_count

猜你喜欢

转载自yang-min.iteye.com/blog/2376539