用着中端,显示磁盘空间不足,因此写了这个脚本,统计各个用户所用磁盘空间大小。
将该文件放在179的/home目录下,进行执行,在/home目录下将生成4_25.txt文件。
注:总空间1T, 可以用 “ df "命令统计磁盘总空间大小。
file: x.sh usage: sudo ./x.sh
#! /bin/sh tmpfilename=/tmp/4.txt storefilename=4_25.txt leftspace=0 rm -f ${tmpfilename} rm -f ${storefilename} for user in `ls -l | awk '/^d/{print $NF}'` do total=`du --max-depth=0 -s -m ./${user}/ 2>/dev/null | awk 'BEGIN{sum = 0;} {sum += $1} END{print sum}'` echo ${user} ' ' ${total} >> ${tmpfilename} let "leftspace=${leftspace} + ${total}" done ((leftspace = 1024 * 1024 - leftspace)) echo leftspace' '${leftspace} >> ${tmpfilename} #echo "UNIT:M" > ${storefilename} sort -k 2 -n -r ${tmpfilename} | awk '{printf "%-20s %-10s ", $1, $2}' >> ${storefilename} rm -f ${tmpfilename} #python plot.py
图形化显示各用户空间的百分比。
注意179以不能自动更新,若要运行该脚本,请放置本地运行。
file: plot.py usage: ./plot.py
plot.py 与4_25.txt同目录。
#! /usr/bin/python import matplotlib.pyplot as plt labels = [] quants = [] for line in file('4_25.txt'): info = line.split() labels.append(info[0]) quants.append(float(info[1])) plt.figure(1, figsize = (6, 6)) def explode(label, target = 'leftspace'): if label == target: return 0.1 else: return 0 expl = map(explode, labels) colors = ["pink", "coral", "yellow", "orange"] plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%', pctdistance=0.8, shadow=True) plt.title('179 FULL SPACE 1T', bbox={'facecolor':'0.8', 'pad':5}) plt.savefig('4_25.png') plt.show()