51

例如,我有一个大型文件系统,它的填充速度比我预期的要快。所以我寻找正在添加的内容:

find /rapidly_shrinking_drive/ -type f -mtime -1 -ls | less

我发现,嗯,很多东西。上千个六七类文件。我可以挑出一个类型并计算它们:

find /rapidly_shrinking_drive/ -name "*offender1*" -mtime -1 -ls | wc -l

但我真正想要的是能够获得这些文件在磁盘上的总大小:

find /rapidly_shrinking_drive/ -name "*offender1*" -mtime -1 | howmuchspace

我愿意为此使用 Perl 单行,如果有人有的话,但我不会使用任何涉及多行脚本或 File::Find 的解决方案。

4

7 回答 7

75

该命令du会告诉您有关磁盘使用情况的信息。您的具体情况的示例用法:

find rapidly_shrinking_drive/ -name "offender1" -mtime -1 -print0 | du --files0-from=- -hc | tail -n1

(以前我写过du -hs,但在我的机器上似乎忽略了find的输入,而是总结了 cwd 的大小。)

于 2009-07-15T21:43:53.727 回答
15

该死,Stephan202 是对的。我没有考虑 du -s (总结),所以我使用了 awk:

find rapidly_shrinking_drive/ -name "offender1" -mtime -1 | du | awk '{total+=$1} END{print total}'

不过,我更喜欢另一个答案,而且几乎可以肯定它更有效。

于 2009-07-15T21:45:31.693 回答
8

使用 GNU 查找,

 find /path -name "offender" -printf "%s\n" | awk '{t+=$1}END{print t}'
于 2009-07-16T13:00:10.590 回答
5

我想将上面 jason 的评论提升为回答状态,因为我相信它是最容易记忆的(虽然不是最通用的,如果你真的需要指定的文件列表find):

$ du -hs *.nc
6.1M  foo.nc
280K  foo_region_N2O.nc
8.0K  foo_region_PS.nc
844K  foo_region_xyz.nc
844K  foo_region_z.nc
37M   ETOPO1_Ice_g_gmt4.grd_region_zS.nc
$ du -ch *.nc | tail -n 1
45M total
$ du -cb *.nc | tail -n 1
47033368  total
于 2013-02-02T19:40:11.090 回答
1

我已经尝试了所有这些命令,但没有运气。所以我找到了这个给我答案的答案:

find . -type f -mtime -30 -exec ls -l {} \; | awk '{ s+=$5 } END { print s }'
于 2011-01-08T15:38:26.803 回答
1

最近我遇到了同样的(几乎)问题,我想出了这个解决方案。

find $path -type f -printf '%s '

它将以字节为单位显示文件大小,来自man find

-printf format
    True; print format on the standard output, interpreting `\' escapes and `%' directives.  Field widths and precisions can be spec‐
    ified as with the `printf' C function.  Please note that many of the fields are printed as %s rather than %d, and this  may  mean
    that  flags  don't  work as you might expect.  This also means that the `-' flag does work (it forces fields to be left-aligned).
    Unlike -print, -printf does not add a newline at the end of the string.
    ...
    %s  File's size in bytes.
    ...

为了得到一个总数,我使用了这个:

echo $[ $(find $path -type f -printf %s+)0] #b
echo $[($(find $path -type f -printf %s+)0)/1024] #Kb
echo $[($(find $path -type f -printf %s+)0)/1024/1024] #Mb
echo $[($(find $path -type f -printf %s+)0)/1024/1024/1024] #Gb
于 2020-07-21T13:53:54.447 回答
-1

您还可以使用ls -l来查找它们的大小,然后awk提取大小:

find /rapidly_shrinking_drive/ -name "offender1" -mtime -1 | ls -l | awk '{print $5}' | sum
于 2009-07-15T21:48:15.493 回答