Linux 计算shell中文件的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/599027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:03:48  来源:igfitidea点击:

Calculate size of files in shell

linuxshell

提问by yothenberg

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:

我正在尝试仅使用 shell 计算与文件名模式匹配的所有文件(在目录树中)的总大小(以字节为单位)。这是我到目前为止:

find -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}'

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.

有没有更简单的方法来做到这一点?我觉得应该有一个简单的 du 或 find 开关可以为我执行此操作,但我找不到。

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.

不会工作,因为它只匹配当前目录中的文件。

采纳答案by tpgould

Try:

尝试:

find . -name "*.undo" -ls | awk '{total += } END {print total}'

On my system the size of the file is the seventh field in the find -lsoutput. If your find … -lsoutput is different, adjust.

在我的系统上,文件的大小是find -ls输出中的第七个字段。如果您的find … -ls输出不同,请进行调整。

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.

在这个版本中,使用现有的目录信息(文件大小)和 find 的内置 ls 功能应该是有效的,避免进程创建或文件 i/o。

回答by CMS

Check the du(disk usage) command.

检查du(磁盘使用情况)命令。

回答by Cantillon

du -c *pattern*

This will print the total on the last line of output.

这将在输出的最后一行打印总数。

回答by David Z

find -name '*.undo' -exec wc -c {} + | tail -n 1

should give the actual total number of bytes in the files, if you don't have too many files (where "too many" is going to be a really large number, probably in the thousands). Or if you just want to get the number alone,

应该给出文件中的实际总字节数,如果您没有太多文件(其中“太多”将是一个非常大的数字,可能是数千个)。或者,如果您只想单独获取号码,

find -name '*.undo' -exec wc -c {} + | tail -n 1 | cut -d' ' -f 1

回答by S.Lott

Python is part of most linux distributions.

Python 是大多数 linux 发行版的一部分。

import os
import fnmatch
size= 0
for path, dirs, files in os.walk( '.' ):
    for f in files:
        if fnmatch.fnmatch(f,'*.py'):
            fileSize= os.path.getsize( os.path.join(path,f) ) 
            print f, fileSize
            size += fileSize
print size

Longish, but perfectly clear and highly extensible.

较长,但非常清晰且高度可扩展。

回答by Chris AtLee

With zsh, you can use extended globbing to do:

使用 zsh,您可以使用扩展的通配符来执行以下操作:

du -c **/*.undo

du -c **/*.undo

回答by gerdemb

find -name *.undo -print0 | du -hc --files0-from=-

回答by xilef

find -name '*.undo' -print0 | du -hc --files0-from=- | tail -n 1

Put together from gerdemb's and strager's contributions. Using du -cbshould display bytes.

结合gerdemb和 strager 的贡献。使用du -cb应该显示字节。

回答by T-Monster

I have been looking at this problem too (only a year later...) - only just found this page.

我也一直在研究这个问题(仅一年后......) - 才刚刚找到这个页面。

Something that I found works (for me) is the following:

我发现有效的东西(对我来说)如下:

find /mnt/iso -name *.avi -printf "%s\n" | paste -sd+ - | bc

This will return the total size of all the .avi files in all the sub-folders below /mnt/iso

这将返回 /mnt/iso 下所有子文件夹中所有 .avi 文件的总大小

I have to give credit to radoulov for the paste command - see this page: Shell command to sum integers, one per line?

我必须将粘贴命令归功于 radoulov - 请参阅此页面: Shell 命令对整数求和,每行一个?

Just to add - just in case a folder matches the search term - it's a good idea to use -type fin the find command too.

只是添加 - 以防文件夹与搜索词匹配 --type f在 find 命令中使用也是一个好主意。

回答by Dennis Mathews

How about this simple one.

这个简单的怎么样。

find ./ -name *.undo | xargs wc