首页 > 解决方案 > 所有文件的脚本

问题描述

我有一个计算哪个文件更大的 shell 脚本。在这里我展示我所拥有的

p1="$HOME"
p2="backup.tar.gz"
p3="curl"

size1=`du $p1/$p2 | awk '{print $1}'`
size2=`du $p1/$p3 | awk '{print $1}'`

if [ $size1-gt $size2]
then
    echo "high is $p2"
else
    echo "high is $p3"
fi

我想确定哪些是 $HOME 的最大文件或目录,因为它可以有很多而不是只有两个

标签: bashshell

解决方案


Your problem reduces to

du "$HOME"/* | sort -n

Like most sane tools, du accepts a list of file name arguments; and repeated pairwise comparisons are extremely wasteful when you can simply sort. A good sorting algorithm eliminates most pairwise comparisons - it knows that if A is bigger than B and B is bigger than C, A must be bigger than C, too.


推荐阅读