在Linux系统中,经常会遇到某个目录下文件很多,要统计这些文件的空间大小。可以采用awk来实现。如下是实现这个功能的例子。
vim sum.sh
#!/bin/bash
# sum.sh
cd //backup/dbbak
ls -l -t *.tar.bz2|awk -F" " ‘{print $5}‘ >1.txt
# cat 1.txt |head
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
a=`echo 1024*1024*1024 |bc `
sum=`awk ‘BEGIN{sum=0}{sum+=$1}END{print sum}‘ 1.txt`
sum_MB=`echo "$sum"/"$a"|bc`
echo "$sum_MB" G
# 操作实例
[[email protected] tmp]# cat sum.sh
#!/bin/bash
# sum.sh
cd /mnt/backup/yhzdbbak/dbbak_hgame/
ls -l -t *.tar.bz2|awk -F" " ‘{print $5}‘ >1.txt
# cat [[email protected] dbbak_hgame]# cat 1.txt |head
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
#1.1G
a=`echo 1024*1024*1024 |bc `
sum=`awk ‘BEGIN{sum=0}{sum+=$1}END{print sum}‘ 1.txt`
sum_MB=`echo "$sum"/"$a"|bc`
echo "$sum_MB" G
[[email protected] tmp]# sh sum.sh
216 G
[[email protected] tmp]#