目录中所有文件内容的总大小
当我使用ls或du ,我得到每个文件占用的磁盘空间量。 
如果我打开每个文件并计算字节数,我需要文件和子目录中所有数据的总和。 奖金点,如果我可以得到这个没有打开每个文件和计数。
 如果你需要'外观大小'(即每个文件中的字节数),而不是磁盘上文件占用的大小,可以使用-b或--bytes选项(如果你有一个带有GNU coreutils的Linux系统) : 
 % du -sbh <directory> 
 使用du -sb : 
 du -sb DIR 
 或者,添加h选项以获得更多用户友好的输出: 
 du -sbh DIR 
cd到目录,然后:
 du -sh 
FTW!
最初写在这里: https : //andrewodendaal.com/get-the-total-size-of-all-the-files-in-a-directory/
只是一个select:
 $ ls -lR | grep -v '^d' | awk '{total += $5} END {print "Total:", total}' 
grep -v'^ d'将排除目录。
stat的“%s”格式为您提供文件中的实际字节数。
  find . -type f | xargs stat --format=%s | awk '{s+=$1} END {print s}' 
随意用你最喜欢的方法来replace数字 。
如果你在emebedded系统中使用busybox的“du”,你不能得到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 (eg, 1K 243M 2G ) -m Print sizes in megabytes -k Print sizes in kilobytes(default) 
  du是方便的,但是如果只想计算某些文件的大小(例如,使用扩展名筛选), find会很有用。 另外请注意, find自己可以按字节打印每个文件的大小。 要计算总大小,我们可以按以下方式连接dc命令: 
 find . -type f -printf "%s + " | dc -e0 -f- -ep 
 这里find生成dc的命令序列,如123 + 456 + 11 + 。 虽然,完成的程序应该像0 123 + 456 + 11 + p (记住后缀表示法)。 
 所以,为了获得完整的程序,我们需要在执行stdin的序列之前将0放在堆栈上,并在执行后打印最上面的数字(最后的p命令)。 我们通过dc选项实现它: 
-   -e0只是-e0-e '0'快捷方式,将0放在堆栈上,
-   -f-用于从标准input(在此find生成)读取和执行命令,
-   -ep用于打印结果(-e 'p')。
 要以284.06 MiB尺寸打印,我们可以在第3点使用284.06 MiB -e '2 k 1024 / 1024 / n [ MiB] p' 284.06 MiB -e '2 k 1024 / 1024 / n [ MiB] p' (大多数空格是可选的)。 
使用:
 $ du -ckx <DIR> | grep total | awk '{print $1}' 
其中<DIR>是您要检查的目录。
'-c'给出了使用命令的'grep total'部分提取的总数据,awk命令提取了千字节数。
唯一需要注意的是,如果你有一个包含文本“total”的子目录,它也会被吐出来。
这可能有助于:
 ls -l| grep -v '^d'| awk '{total = total + $5} END {print "Total" , total}' 
上面的命令将总和所有文件离开目录的大小。
 至less有三种方法可以在Linux / Unix和Windows下的Git Bash中按字节获得“文件和子目录中所有数据的总和”,按照从最快到最慢的顺序排列。 为了供您参考,它们是在相当深的文件系统的根目录下执行的(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 。 
请注意,一些build议的解决scheme不会返回准确的结果,所以我会坚持使用上面的解决scheme。
 $ 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 
对于Win32 DOS,您可以:
c:> dir / sc:\ directory \你需要
倒数第二行会告诉你文件占用了多less字节。
我知道这读取所有文件和目录,但在某些情况下工作更快。
当创build文件夹时,许多Linux文件系统分配4096个字节来存储一些关于目录本身的元数据。 随着目录的增长,这个空间增加了4096个字节的倍数。
du命令(有或没有-b选项) 请记下这个空间 ,你可以看到input:
 mkdir test && du -b test 
你将有一个空的目录4096字节的结果。 所以,如果你把2个10000字节的文件放在目录里面,那么du -sb给出的总量就是24096字节。
如果你仔细阅读这个问题,这不是什么问题。 提问者问:
如果打开每个文件并计算字节数,我将得到的文件和子目录中所有数据的总和
在上面的例子中应该是20000字节,而不是24096。
所以,正确的答案恕我直言可能是纳尔逊答案和hlovdalbuild议处理文件名包含空格的混合:
 find . -type f -print0 | xargs -0 stat --format=%s | awk '{s+=$1} END {print s}'