300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > shell统计指定目录下所有文件类型及数量

shell统计指定目录下所有文件类型及数量

时间:2020-06-04 07:57:54

相关推荐

shell统计指定目录下所有文件类型及数量

#!/bin/bash

#Synopsis:用于统计脚本当前所在目录或者用户指定目录下的所有文件类型及数量

#若直接运行脚本而不接任何命令行参数,则默认会统计脚本所在目录下的文件

#Date:/10

#Author:Jian

#Usage:sh fileStat.sh /path1 /path2

testFile=$(mktemp /tmp/testfile.XXX)

#如果没有指定查询目录,则使用默认的当前脚本所在目录

if [ $# -eq 0 ]; then

path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"

else

path="$@"

fi

#在给出的所有目录中循环查询

for dir in $path

do

if [ ! -d $dir ]; then

#若给出错误路径则用红色字体打印出来

echo -e "\e[1;31m Error:wrong path [$dir] \e[0m"

continue

fi

#find命令递归查找指定目录下面所有文件及目录

find $dir -mindepth 1 -print | while read line

do

ftype="$(file -b $line)"

echo $ftype

done>$testFile

echo "==========Directory [$dir] File type and counts=========="

#使用awk关联数组统计文件类型所对应的数目

awk '

{ count[$0]++ }

END{

for(ftype in count)

{ printf ("%s: %d\n",ftype,count[ftype]) }

}' $testFile | sort

#删除创建的临时文件

rm -rf $testFile

done

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。