0
点赞
收藏
分享

微信扫一扫

shell脚本之给文件按照扩展名归类

#!/bin/bash

#装扩展名的数组
arr_ext=()

ProjPath=`cd $(dirname "$0");pwd`
dirPath="$ProjPath/process"
#包含所有文件的根目录
#echo $dirPath

#创建分类目录
descPath="$ProjPath/desc"
if test -e $descPath
then
rm -rf $descPath
else
mkdir $descPath
fi

read_dir(){
for file in `ls -a $1`
do
if [ -d $1"/"$file ]
then
if [[ $file != '.' && $file != '..' ]]
then
read_dir $1"/"$file
fi
else
FILE=$1"/"$file
echo ${FILE}
#获取文件的扩展名
EXT="${FILE##*.}"

#判断元素在素组中已经存在
isexist=0
for var in ${arr_ext[@]}
do
if [[ $var == $EXT ]]
then
isexist=1
fi
done

if [[ $isexist == 0 ]]
then
#向数组中追加元素
arr_ext[${#arr_ext[*]}]=$EXT

#创建分类目录
mkdir $descPath"/"$EXT
fi

#拷贝文件到分类目录
cp $FILE $descPath"/"$EXT
echo -e "拷贝成功!\n"

fi
done
}

read_dir $dirPath

#便利数组
for var in ${arr_ext[@]}
do
echo "扩展名:"$var
done

 


举报

相关推荐

0 条评论