【问题标题】:total size of group of files selected with 'find'使用“查找”选择的文件组的总大小
【发布时间】:2010-11-11 04:17:37
【问题描述】:

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

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

【问题讨论】:

    标签: shell scripting find grep filesize


    【解决方案1】:

    命令du 告诉您磁盘使用情况。您的具体情况的示例用法:

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

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

    【讨论】:

    • 非常好。虽然,记住“find”的残酷关键字(“--files0-from=”)实际上可能并不比记住“awk”序列更容易。
    • 使用du 8.13 版,结果相同:du -ch /rapidly_shrinking_drive/*offender1* | tail -n1
    • 我的机器不喜欢--files0-from= 选项。 ;-/
    • 另一种似乎可行的方法:find rapidly_shrinking_drive/ -name "offender1" -mtime -1 -print0 | xargs -0 du -hc | tail -n1
    • 注意:上面的标志--files0-from参数不是拼写错误。
    【解决方案2】:

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

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

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

    【讨论】:

    • 在查找中使用 -exec 的替代方法:find rapidly_shrinking_drive/ -name "offender1" -mtime -1 -exec du {} \; | awk '{ total += $1 }END{ print total }'
    • 它是否能够将最终输出的数字转换为更易于阅读的格式,例如 103M?
    • @zen 你可以使用numfmt(coreutils 的一部分)并做类似find rapidly_shrinking_drive/ -name "offender1" -mtime -1 | du | awk '{total+=$1} END{print total}' | numfmt --to=si
    【解决方案3】:

    使用 GNU 查找,

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

    【讨论】:

    • +1 明确提及 GNU find。 (不幸的是,这种方式不太便携)。
    【解决方案4】:

    我想将上面 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
    

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

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

        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
        

        【讨论】:

        • 有趣且仅使用find 功能!好的!你能解释一下 echo 的 $[..] 语法吗?
        • @CiprianTomoiagă 确定是 bash 数学语法 echo $[1+1] 将打印 2
        【解决方案7】:

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

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

        【讨论】:

        • 如果你要这样做,你需要使用xargs
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 2021-03-30
        • 2011-08-10
        • 1970-01-01
        相关资源
        最近更新 更多