Linux 如何比较两个目录的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3769206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:15:00  来源:igfitidea点击:

How to compare the size of two directories?

linuxbashcommand-line

提问by rafak

I want to compare the total size of two directories dir1and dir2on different file-systems so that if diff -r dir1 dir2returns 0then the total sizes will be equal. The ducommand returns the disk usage, and its option --apparent-sizedoesn't solve the problem. I now use something like

我想比较两个目录dir1dir2不同文件系统上的总大小,以便如果diff -r dir1 dir2返回,0那么总大小将相等。该du命令返回磁盘使用情况,其选项--apparent-size不能解决问题。我现在使用类似的东西

find dir1 ! -type d |xargs wc -c |tail -1

to know an approximation of dir1's size. Is there a better solution?

了解 dir1 大小的近似值。有更好的解决方案吗?

edit: for example, I have (diff -r dir1 dir2returns 0: they are equal):

编辑:例如,我有(diff -r dir1 dir2返回 0:它们相等):

du -s dir1 --> 540
du -s dir2 --> 166

du -sb dir1 --> 250815 (the -b option is equivalent to --apparent-size -B1)
du -sb dir2 --> 71495

find dir1 ! -type d |xargs wc -c --> 62399
find dir2 ! -type d |xargs wc -c --> 62399 

回答by Paused until further notice.

If your version of findhas -printfyou may find this to be quite a bit faster.

如果您的版本find有,-printf您可能会发现这要快得多。

find dir1 ! -type d -printf "%s\n" | awk '{sum += } END{print sum}'

There are at least two ways to avoid scientific notation for outputting large numbers in AWK.

至少有两种方法可以避免在 AWK 中输出大量数字的科学记数法。

END {OFMT = "%.0f"; print sum}

END {printf "%.0f\n", sum}

The .0truncates the decimal places since we're really dealing with an integer and gawk's %dseems to incorrectly act like %gin version 3.1.5 (but not 3.1.6 and later).

.0截断小数位,因为我们真正处理的是一个整数,GAWK的%d似乎错误地像%g在3.1.5版本(但不是3.1.6或更高版本)。

However, from the gawkdocumentation:

但是,从gawk文档中:

NOTE: When using the integer format-control letters for values that are outside the range of the widest C integer type, 'gawk' switches to the '%g' format specifier.

注意:当对最宽的 C 整数类型范围之外的值使用整数格式控制字母时,'gawk' 切换到 '%g' 格式说明符。

Beware of exceeding the maximum integer for your system/version of AWK.

当心超过系统/AWK 版本的最大整数。

回答by zjhui

i can't know what you want clearly. Maybe you want this?
diff <(du -sh dir1) <(du -sh dir2)

我无法清楚地知道你想要什么。也许你想要这个?
diff <(du -sh dir1) <(du -sh dir2)