Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 使用';选择的文件组的总大小;查找';_Shell_Scripting_Find_Grep_Filesize - Fatal编程技术网

Shell 使用';选择的文件组的总大小;查找';

Shell 使用';选择的文件组的总大小;查找';,shell,scripting,find,grep,filesize,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 /rap

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

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 one liner,我对此持开放态度,但我不会使用任何涉及多行脚本或File::Find的解决方案。

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

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

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

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

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

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

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

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

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

我试过所有这些命令,但没有成功。 所以我找到了这个答案:

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

我想把上面jason的评论提升到answer的状态,因为我相信它是最有助于记忆的(虽然不是最通用的,如果你真的需要有
find
指定的文件列表的话):

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

find $path -type f -printf '%s '
它将以字节为单位显示文件大小,从
manfind

$ 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
-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

很不错的。虽然,记住“查找”这个残酷的关键字(“--files0 from=”)实际上可能并不比记住“awk”序列更容易。使用
du
version 8.13,下面给出了相同的结果:
du-ch/ravely\u-resking\u-drive/*offer1*;tail-n1
我的机器不喜欢
--files0 from=
选项-/一个似乎有效的替代方法是:
find rapid\u resking\u drive/-name“afcler1”-mtime-1-print0 | xargs-0 du-hc | tail-n1
注意:上面的标志
--files0 from
参数不是拼写错误。在find中使用-exec的替代方法是:
find rapid\u resking\u drive/-name“afcler1”-mtime-1-exec du{} \; | awk'{total+=$1}END{print total}'
是否能够将最终输出的数字转换为更易于阅读的格式,如103M?@zen您可以使用
numfmt
(coreutils的部分)并执行类似于
查找快速缩小的驱动器/-name“offer1”-mtime-1 | du awk'{total+=$1}END{print total}“| numfmt--to=si
+1,用于明确提及GNU find。(糟糕的是,这种方式的可移植性较差)。如果要这样做,您需要使用xargsInteresting,并且只使用
find
功能!美好的你能解释一下echo的语法吗?@cipriantomoagă确定它是bash数学语法
echo$[1+1]
将打印
2
genius!我会再次投票