使用“查找”选择的文件组的总大小

时间:2009-07-15 21:38:53

标签: shell scripting find grep filesize

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

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的解决方案。

7 个答案:

答案 0 :(得分:63)

命令du告诉您磁盘使用情况。特定案例的用法示例:

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

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

答案 1 :(得分:14)

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

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

我更喜欢其他答案,而且几乎肯定更有效率。

答案 2 :(得分:7)

使用GNU find,

 find /path -name "offender" -printf "%s\n" | awk '{t+=$1}END{print t}'

答案 3 :(得分:3)

我想将jason的上述评论推广到答案的状态,因为我认为它是最助记的(尽管不是最通用的,如果你真的必须有find指定的文件列表): / p>

$ 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

答案 4 :(得分:1)

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

find . -type f -mtime -30 -exec ls -l {} \; | awk '{ s+=$5 } END { print s }'

答案 5 :(得分:0)

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

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

答案 6 :(得分:-1)

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

find /rapidly_shrinking_drive/ -name "offender1" -mtime -1 | ls -l | awk '{print $5}' | sum
相关问题