bash - 所有文件的脚本
问题描述
我有一个计算哪个文件更大的 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 的最大文件或目录,因为它可以有很多而不是只有两个
解决方案
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.
推荐阅读
- java - 试图回答一个数学难题,但我的程序不会退出这个循环
- python-3.x - 在 python 中将文本文档转换为 ASCII 数字的 numpy 数组
- android - 2 个布局在一个片段中
- sql - 如何在组内使用 SQL Server 中的 LAG?
- python - Pygame中的键盘输入不起作用
- node.js - LernaJS Typescript 找不到模块
- http - Arduino 向 Slack WebHook 发送 POST
- c++ - 来自二维数组的特征图
- vim - 在 VIM 中,为什么光标移动 4yk 而不是 4yj?
- date - 在 Dart/Flutter 中创建 DateTime 对象时如何指定 TimeZone?