计算外壳中文件的大小 [英] Calculate size of files in shell
问题描述
我试图仅使用Shell来计算与文件名模式匹配的所有文件(在目录树中)的总大小(以字节为单位).这是我到目前为止的内容:
I'm trying to calculate the total size in bytes of all files (in a directory tree) matching a filename pattern just using the shell. This is what I have so far:
查找-name * .undo -exec stat -c%s {} \; | awk'{total + = $ 1} END {print total}'
find -name *.undo -exec stat -c%s {} \; | awk '{total += $1} END {print total}'
有没有更简单的方法可以做到这一点?我觉得应该有一个简单的du或find开关可以为我完成此操作,但是我找不到.
Is there an easier way to do this? I feel like there should be a simple du or find switch that does this for me but I can't find one.
为了清楚起见,我想在目录树下的任何位置汇总与某个模式匹配的文件,这意味着
To be clear I want to total files matching a pattern anywhere under a directory tree which means
du -bs * .undo
du -bs *.undo
将不起作用,因为它仅与当前目录中的文件匹配.
won't work because it only matches the files in the current directory.
推荐答案
尝试:
find . -name "*.undo" -ls | awk '{total += $7} END {print total}'
在我的系统上,文件大小是find -ls
输出中的第七个字段.如果您的find … -ls
输出不同,请调整.
On my system the size of the file is the seventh field in the find -ls
output. If your find … -ls
output is different, adjust.
在此版本中,使用现有的目录信息(文件大小)和find的内置ls功能应该有效,避免创建进程或文件I/O.
In this version, using the existing directory information (file size) and the built-in ls feature of find should be efficient, avoiding process creations or file i/o.
这篇关于计算外壳中文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!