shell 用'find'选择的文件组的总大小

axr492tv  于 2022-11-16  发布在  Shell
关注(0)|答案(8)|浏览(129)

例如,我有一个很大的文件系统,它的填充速度比我预期的要快。

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

bwntbbo3

bwntbbo31#

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

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

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

aemubtdh

aemubtdh2#

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

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

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

vjhs03f7

vjhs03f73#

有了GNU find,

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

fgw7neuy4#

我想把jason上面的评论提升到answer的地位,因为我相信它是最容易记忆的(虽然不是最通用的,如果你真的要用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
ih99xse1

ih99xse15#

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

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
dfddblmv

dfddblmv6#

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

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

vuv7lop37#

既然OP明确地说:
如果有人有Perl一行程序,我愿意使用它,但我不会使用任何涉及多行脚本或File::Find的解决方案。
......而且还没有,下面是perl的一行程序:

find . -name "*offender1*" | perl -lne 'BEGIN { $t = 0 }; $t += -s $_; END { print $t }'

这不是use strict模式,因此可以省略BEGIN块中的$t声明:

find . -name "*offender1*" | perl -lne '$t += -s $_; END { print $t }'
wh6knrhe

wh6knrhe8#

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

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

相关问题