您的位置:首页 > 运维架构 > Shell

利用shell计算find命令查出后的总文件大小

2011-08-08 17:37 701 查看
下午一个同事咨询如何计算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
done

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

rm -rf    /tmp/tmp.txt
rm -rf    /tmp/count.txt
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
本文出自 “斩月” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: