shell统计指定目录下所有文件类型及数量
#!/bin/bash
#Synopsis:用于統(tǒng)計腳本當前所在目錄或者用戶指定目錄下的所有文件類型及數(shù)量
#若直接運行腳本而不接任何命令行參數(shù),則默認會統(tǒng)計腳本所在目錄下的文件
#Date:2016/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
#在給出的所有目錄中循環(huán)查詢
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關(guān)聯(lián)數(shù)組統(tǒng)計文件類型所對應(yīng)的數(shù)目
? awk '
? ? ? ?{ count[$0]++ }
? ? ? ?END{
? ? ? ? ? ?for(ftype in count)
? ? ? ? ? ?{ printf ("%s: %d\n",ftype,count[ftype]) }
? ? ? ?}' $testFile | sort?
? #刪除創(chuàng)建的臨時文件
? rm -rf $testFile
done?
轉(zhuǎn)載于:https://blog.51cto.com/11829889/1872038
總結(jié)
以上是生活随笔為你收集整理的shell统计指定目录下所有文件类型及数量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高斯消元法解方程matlab程序,用高斯
- 下一篇: header的安全配置指南