下午一个同事咨询如何计算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
本文转自斩月博客51CTO博客,原文链接http://blog.51cto.com/ylw6006/634433如需转载请自行联系原作者
ylw6006