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

发布于 2024-07-29 01:18:56 字数 490 浏览 7 评论 0原文

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

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

For instance, I have a large filesystem that is filling up faster than I expected. So I look for what's being added:

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

And I find, well, lots of stuff. Thousands of files of six-seven types. I can single out a type and count them:

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

but what I'd really like is to be able to get the total size on disk of these files:

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

I'm open to a Perl one-liner for this, if someone's got one, but I'm not going to use any solution that involves a multi-line script, or File::Find.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

跨年 2024-08-05 01:18:56

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

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

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

The command du tells you about disk usage. Example usage for your specific case:

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

(Previously I wrote du -hs, but on my machine that appears to disregard find's input and instead summarises the size of the cwd.)

娇柔作态 2024-08-05 01:18:56

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

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

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

Darn, Stephan202 is right. I didn't think about du -s (summarize), so instead I used awk:

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

I like the other answer better though, and it's almost certainly more efficient.

2024-08-05 01:18:56

通过 GNU 查找,

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

with GNU find,

 find /path -name "offender" -printf "%s\n" | awk '{t+=$1}END{print t}'
穿越时光隧道 2024-08-05 01:18:56

我想将上面杰森的评论提升为答案的状态,因为我相信它是最容易记忆的(虽然不是最通用的,如果你真的需要 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

I'd like to promote jason's comment above to the status of answer, because I believe it's the most mnemonic (though not the most generic, if you really gotta have the file list specified by 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
夜清冷一曲。 2024-08-05 01:18:56

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

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

Recently i faced the same(almost) problem and i came up with this solution.

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

It'll show files sizes in bytes, from 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.
    ...

And to get a total i used this:

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
琉璃繁缕 2024-08-05 01:18:56

由于OP明确表示:

如果有人有的话,我愿意为此使用 Perl 语句,但我
不会使用任何涉及多行脚本的解决方案,或者
文件::查找。

...还没有,这是 perl 一行:

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

Since OP specifically said:

I'm open to a Perl one-liner for this, if someone's got one, but I'm
not going to use any solution that involves a multi-line script, or
File::Find.

...and there's none yet, here is the perl one-liner:

find . -name "*offender1*" | perl -lne '$total += -s $_; END { print $total }'
空宴 2024-08-05 01:18:56

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

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

I have tried all this commands but no luck.
So I have found this one that gives me an answer:

find . -type f -mtime -30 -exec ls -l {} \; | awk '{ s+=$5 } END { print s }'
骄傲 2024-08-05 01:18:56

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

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

You could also use ls -l to find their size, then awk to extract the size:

find /rapidly_shrinking_drive/ -name "offender1" -mtime -1 | ls -l | awk '{print $5}' | sum
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文