天天看點

利用shell計算find指令查出後的總檔案大小

  下午一個同僚咨詢如何計算find指令查詢出來的檔案總大小,想了想自己隻能用shell腳本實作,于是就實踐了下,腳本内容如下:

#! /bin/bash  

rm -rf    /tmp/tmp.txt  

rm -rf    /tmp/count.txt  

rm -rf    /tmp/1.sql  

find /root/* -type f -name "*.txt" > /tmp/tmp.txt  

NUM=$(cat /tmp/tmp.txt | wc -l)  

for (( i=1; i<=$NUM; i=i+1));  

        do  

        LINE=$(sed -n "$i"p  /tmp/tmp.txt)  

        ls -l $LINE |cut -d ' ' -f 5 >> /tmp/count.txt  

done  

NUM1=$(cat /tmp/count.txt | wc -l)  

for (( i=1; i<=$NUM1; i=i+1));  

do  

   A=$(sed -n "$i"p  /tmp/count.txt)  

   echo -n $A + >> /tmp/1.sql  

SUM=$(echo "$(sed 's/.$//' /tmp/1.sql)" |bc)  

echo "$SUM" 

rm -rf    /tmp/1.sql 

[root@rhel6 ~]# sh 1.sh 

277109

其實也可以簡單使用awk指令實作,不過不太懂awk,隻能使用shell實作了,哈哈!

[root@rhel6 ~]# find ./* -name "*.txt" -exec ls -lh {} \;| awk 'BEGIN {SUM7=0}{ SUM7+=$5} END {print SUM7}'

272.1

本文轉自斬月部落格51CTO部落格,原文連結http://blog.51cto.com/ylw6006/634433如需轉載請自行聯系原作者

ylw6006