目录中所有文件内容的总大小

时间:2009-08-06 22:08:14

标签: linux embedded shell

当我使用lsdu时,我会获得每个文件占用的磁盘空间量。

如果我打开每个文件并计算字节数,我需要文件和子目录中所有数据的总和。如果我可以在不打开每个文件并计数的情况下获得此奖励积分。

12 个答案:

答案 0 :(得分:102)

如果您想要“表观大小”(即每个文件中的字节数),而不是磁盘上文件占用的大小,请使用-b--bytes选项(如果您得到了一个GNU coreutils)的Linux系统:

% du -sbh <directory>

答案 1 :(得分:44)

使用du -sb

du -sb DIR

(可选)添加h选项以获得更加用户友好的输出:

du -sbh DIR

答案 2 :(得分:23)

cd到目录,然后:

du -sh

FTW!

最初在这里写到: https://ao.gl/get-the-total-size-of-all-the-files-in-a-directory/

答案 3 :(得分:15)

只是另一种选择:

ls -lAR | grep -v '^d' | awk '{total += $5} END {print "Total:", total}'

grep -v '^d'将排除目录。

答案 4 :(得分:12)

stat的“%s”格式为您提供文件中的实际字节数。

 find . -type f |
 xargs stat --format=%s |
 awk '{s+=$1} END {print s}'

随意替换favourite method for summing numbers

答案 5 :(得分:3)

如果在嵌入式系统中使用“busybox su”,则无法获得du的精确字节,只能获得Kbytes。

BusyBox v1.4.1 (2007-11-30 20:37:49 EST) multi-call binary

Usage: du [-aHLdclsxhmk] [FILE]...

Summarize disk space used for each FILE and/or directory.
Disk space is printed in units of 1024 bytes.

Options:
        -a      Show sizes of files in addition to directories
        -H      Follow symbolic links that are FILE command line args
        -L      Follow all symbolic links encountered
        -d N    Limit output to directories (and files with -a) of depth < N
        -c      Output a grand total
        -l      Count sizes many times if hard linked
        -s      Display only a total for each argument
        -x      Skip directories on different filesystems
        -h      Print sizes in human readable format (e.g., 1K 243M 2G )
        -m      Print sizes in megabytes
        -k      Print sizes in kilobytes(default)

答案 6 :(得分:3)

创建文件夹时,许多Linux文件系统分配4096个字节来存储有关目录本身的一些元数据。 随着目录的增长,这个空间增加了4096个字节的倍数。

du 命令(带或不带-b选项)计算此空间,如您所见:

mkdir test && du -b test

对于空目录,您将得到4096字节的结果。 因此,如果在目录中放置2个10000字节的文件,则 du -sb 给出的总量将为24096字节。

如果你仔细阅读了这个问题,那就不是问了什么。提问者问:

  

如果打开每个文件并计算字节数,我将获得的文件和子目录中所有数据的总和

在上面的例子中应该是20000字节,而不是24096。

所以,正确的答案恕我直言可以混合使用 Nelson 答案和 hlovdal 建议来处理包含空格的文件名:

find . -type f -print0 | xargs -0 stat --format=%s | awk '{s+=$1} END {print s}'

答案 7 :(得分:2)

至少有三种方法可以获得“Linux / Unix和Git Bash for Windows”中的“文件和子目录中所有数据的总和”,以下列出的顺序从最快到最慢。供您参考,它们是在一个相当深的文件系统的根目录下执行的(Magento 2 Enterprise安装中的docroot,包含30,027个目录中的71,158个文件)。

<强> 1

$ time find -type f -printf '%s\n' | awk '{ total += $1 }; END { print total" bytes" }'
748660546 bytes

real    0m0.221s
user    0m0.068s
sys     0m0.160s

<强> 2

$ time echo `find -type f -print0 | xargs -0 stat --format=%s | awk '{total+=$1} END {print total}'` bytes
748660546 bytes

real    0m0.256s
user    0m0.164s
sys     0m0.196s

第3

$ time echo `find -type f -exec du -bc {} + | grep -P "\ttotal$" | cut -f1 | awk '{ total += $1 }; END { print total }'` bytes
748660546 bytes

real    0m0.553s
user    0m0.308s
sys     0m0.416s


这两个也有效,但它们依赖于Git Bash for Windows上不存在的命令:

<强> 1

$ time echo `find -type f -printf "%s + " | dc -e0 -f- -ep` bytes
748660546 bytes

real    0m0.233s
user    0m0.116s
sys     0m0.176s

<强> 2

$ time echo `find -type f -printf '%s\n' | paste -sd+ | bc` bytes
748660546 bytes

real    0m0.242s
user    0m0.104s
sys     0m0.152s


如果您只想要当前目录的总数,请将-maxdepth 1添加到find

请注意,某些建议的解决方案无法返回准确的结果,因此我会坚持使用上述解决方案。

$ du -sbh
832M    .

$ ls -lR | grep -v '^d' | awk '{total += $5} END {print "Total:", total}'
Total: 583772525

$ find . -type f | xargs stat --format=%s | awk '{s+=$1} END {print s}'
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
4390471

$ ls -l| grep -v '^d'| awk '{total = total + $5} END {print "Total" , total}'
Total 968133

答案 8 :(得分:1)

对于Win32 DOS,您可以:

C:GT; dir / s c:\ directory \ you \ want

并且倒数第二行将告诉您文件占用的字节数。

我知道这会读取所有文件和目录,但在某些情况下效果会更快。

答案 9 :(得分:1)

这可能有所帮助:

ls -l| grep -v '^d'| awk '{total = total + $5} END {print "Total" , total}'

上面的命令将总计保留目录大小的所有文件的总和。

答案 10 :(得分:1)

du非常方便,但如果您只想计算某些文件的大小(例如,使用按扩展名过滤),则find非常有用。另请注意,find本身可以以字节为单位打印每个文件的大小。要计算总大小,我们可以通过以下方式连接dc命令:

find . -type f -printf "%s + " | dc -e0 -f- -ep

此处find生成dc的{​​{1}}命令序列。 虽然,完成的程序应该像123 + 456 + 11 +(记住后缀表示法)。

因此,为了获得完成的程序,我们需要在执行stdin序列之前将0 123 + 456 + 11 + p放在堆栈上,并在执行后打印最顶层的数字(最后是0命令)。 我们通过p选项实现它:

  1. dc只是将-e0放入堆栈的-e '0'的快捷方式,
  2. 0用于从stdin读取和执行命令(由-f-生成),
  3. find用于打印结果(-ep)。
  4. 要在MiB中打印-e 'p'的尺寸,我们可以在第3点使用284.06 MiB(大多数空格都是可选的)。

答案 11 :(得分:0)

使用:

$ du -ckx <DIR> | grep total | awk '{print $1}'

其中&lt; DIR&gt;是您要检查的目录。

'-c'为您提供使用命令的'grep total'部分提取的总数据,并使用awk命令提取以KB为单位的计数。

这里唯一需要注意的是,如果你有一个包含文本“total”的子目录,它也会被吐出来。