Answers:
可以从命令'find'中直接获取带有参数'-ls'的文件大小和近似磁盘使用率
function lsdu() (
export SEARCH_PATH=$*
if [ ! -e "$SEARCH_PATH" ]; then
echo "ERROR: Invalid file or directory ($SEARCH_PATH)"
return 1
fi
find "$SEARCH_PATH" -ls | gawk --lint --posix '
BEGIN {
split("B KB MB GB TB PB",type)
ls=hls=du=hdu=0;
out_fmt="Path: %s \n Total Size: %.2f %s \n Disk Usage: %.2f %s \n Compress Ratio: %.4f \n"
}
NF >= 7 {
ls += $7
du += $2
}
END {
du *= 1024
for(i=5; hls<1; i--) hls = ls / (2^(10*i))
for(j=5; hdu<1; j--) hdu = du / (2^(10*j))
printf out_fmt, ENVIRON["SEARCH_PATH"], hls, type[i+2], hdu, type[j+2], ls/du
}
'
)
一些示例命令和输出:
-bash-3.00# lsdu test_sloccount/
Path: test_sloccount/
Total Size: 30.90 MB
Disk Usage: 1.43 MB
Compress Ratio: 21.6250
此单行代码应产生期望的结果:
find $DIRECTOY_TREE_ROOT -type d -exec ls -l '{}' \; | awk '/^total\ .[0-9]+$/ { sum+=$(NF) }END{ print sum }'
我没有要测试的ZFS分区,但是在ext4分区上,它输出的结果与相同du -ks
。
满都可能会在这里有所帮助:
--apparent-size
print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be larger
due to holes in (`sparse') files, internal fragmenta-
tion, indirect blocks, and the like