我试图find/ export / home目录中最大的文件,并加起来(总和)它们的大小。
脚本:
#!/bin/bash filename=hostnames > export_home.log while read -r -a line do hostname=${line//\"} echo $hostname":" >> export_home.log ssh -n -t -t $hostname "sudo find /export/home -type f -mtime +180 -size +250000k -exec du -hsk {} \;" >> export_home.log done < "$filename"
示例输出:
server-34: 210M /export/home/john/142933-02/failsafe_archive 178M /export/home/john/137137-09/failsafe_archive server-35: server-36: 142M /export/home/marc/bdb/db-5.2.36.tar 446M /export/home/marc/sunfreeware/git/git-1.7.6-sol10-x86-local 1.4G /export/home/marc/mysql/mysql-5.5.16-solaris10-x86_64.tar 1.1G /export/home/marc/mysql/mysql-5.5.16-solaris10-i386.tar server-37:
这个脚本完全可以做它应该做的事情,但是现在我怎样才能得到基于export_home结果find的所有文件的总大小呢?
我打算对这个脚本进行一些调整,以便find日志目录和本地备份目录的总大小,以便更好地了解多个服务器上的磁盘使用情况。 我不知道我将如何能够find一个总的磁盘使用情况。
当我使用-k ,我不会得到“M”和“G”后缀。 如果你得到后缀,那么你应该使用jeffatrackaid的build议,使尺寸缩放到相同的幅度。
有很多方法可以将价值总和。 这里有一个:
#!/bin/bash filename=hostnames # no need to clear the file, just move the output redirection to the end of the loop # are you reading into an array to split the line (hostname would be in ${line[0]})? while read -r -a line do hostname=${line//\"} echo "$hostname:" ssh -n -t -t "$hostname" "sudo find /export/home -type f -mtime +180 -size +250000k -exec du -hsk {} \;" done < "$filename" | tee export_home.log | awk '{t += $1} END {print "grand total:", t}'
如果您的find版本支持,请尝试使用+而不是\; – 速度更快
ssh -n -t -t "$hostname" "sudo find /export/home -type f -mtime +180 -size +250000k -exec du -hsk {} +"