Linux 一个目录中所有文件内容的总大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1241801/
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
Total size of the contents of all the files in a directory
提问by Arthur Ulfeldt
When I use ls
or du
, I get the amount of disk space each file is occupying.
当我使用ls
or 时du
,我会得到每个文件占用的磁盘空间量。
I need the sum total of all the data in files and subdirectories I would get if I opened each file and counted the bytes. Bonus points if I can get this without opening each file and counting.
如果我打开每个文件并计算字节数,我需要文件和子目录中所有数据的总和。如果我可以在不打开每个文件并计算的情况下获得此奖励积分。
回答by rob
Use du -sb
:
使用du -sb
:
du -sb DIR
Optionally, add the h
option for more user-friendly output:
或者,添加h
选项以获得更用户友好的输出:
du -sbh DIR
回答by Nelson
stat's "%s" format gives you the actual number of bytes in a file.
stat 的“%s”格式为您提供文件中的实际字节数。
find . -type f |
xargs stat --format=%s |
awk '{s+=} END {print s}'
Feel free to substitute your favourite method for summing numbers.
回答by Arkady
If you want the 'apparent size' (that is the number of bytes in each file), not size taken up by files on the disk, use the -b
or --bytes
option (if you got a Linux system with GNU coreutils):
如果您想要“表观大小”(即每个文件中的字节数),而不是磁盘上文件占用的大小,请使用-b
or--bytes
选项(如果您有一个带有 GNU coreutils的 Linux 系统):
% du -sbh <directory>
回答by Sam Liao
If you use busybox's "du" in emebedded system, you can not get a exact bytes with du, only Kbytes you can get.
如果您在嵌入式系统中使用busybox 的“du”,则无法通过du 获得准确的字节数,只能获得千字节数。
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)
回答by Rob Jones
Use:
用:
$ du -ckx <DIR> | grep total | awk '{print }'
Where <DIR> is the directory you want to inspect.
其中 <DIR> 是您要检查的目录。
The '-c' gives you grand total data which is extracted using the 'grep total' portion of the command, and the count in Kbytes is extracted with the awk command.
“-c”为您提供使用命令的“grep total”部分提取的总计数据,并使用 awk 命令提取以千字节为单位的计数。
The only caveat here is if you have a subdirectory containing the text "total" it will get spit out as well.
这里唯一的警告是,如果您有一个包含文本“total”的子目录,它也会被吐出。
回答by Barun
Just an alternative:
只是一个替代方案:
ls -lAR | grep -v '^d' | awk '{total += } END {print "Total:", total}'
grep -v '^d'
will exclude the directories.
grep -v '^d'
将排除目录。
回答by Sun
For Win32 DOS, you can:
对于 Win32 DOS,您可以:
c:> dir /s c:\directory\you\want
c:> dir /sc:\directory\you\wan
and the penultimate line will tell you how many bytes the files take up.
倒数第二行会告诉你文件占用了多少字节。
I know this reads all files and directories, but works faster in some situations.
我知道这会读取所有文件和目录,但在某些情况下运行速度更快。
回答by AO_
cd to directory, then:
cd 到目录,然后:
du -sh
ftw!
哇!
Originally wrote about it here: https://ao.gl/get-the-total-size-of-all-the-files-in-a-directory/
最初在这里写到它:https: //ao.gl/get-the-total-size-of-all-the-files-in-a-directory/
回答by Ataul Haque
This may help:
这可能有帮助:
ls -l| grep -v '^d'| awk '{total = total + } END {print "Total" , total}'
The above command will sum total all the files leaving the directories size.
上面的命令将对离开目录大小的所有文件求和。
回答by ruvim
du
is handy, but find
is useful in case if you want to calculate the size of some files only (for example, using filter by extension). Also note that find
themselves can print the size of each file in bytes. To calculate a total size we can connect dc
command in the following manner:
du
很方便,但find
如果您只想计算某些文件的大小(例如,使用按扩展名过滤),则很有用。还要注意,find
自己可以以字节为单位打印每个文件的大小。要计算总大小,我们可以通过dc
以下方式连接命令:
find . -type f -printf "%s + " | dc -e0 -f- -ep
Here find
generates sequence of commands for dc
like 123 + 456 + 11 +
.
Although, the completed program should be like 0 123 + 456 + 11 + p
(remember postfix notation).
这里find
为dc
like生成命令序列123 + 456 + 11 +
。虽然,完成的程序应该像0 123 + 456 + 11 + p
(记住后缀符号)。
So, to get the completed program we need to put 0
on the stack before executing the sequence from stdin, and print the top number after executing (the p
command at the end).
We achieve it via dc
options:
所以,为了得到完整的程序,我们需要0
在从 stdin 执行序列之前放入堆栈,并在执行后打印顶部编号(最后的p
命令)。我们通过dc
选项实现它:
-e0
is just shortcut for-e '0'
that puts0
on the stack,-f-
is for read and execute commands from stdin (that generated byfind
here),-ep
is for print the result (-e 'p'
).
-e0
只是-e '0'
放入0
堆栈的快捷方式,-f-
用于从 stdin 读取和执行命令(由find
此处生成),-ep
用于打印结果 (-e 'p'
)。
To print the size in MiB like 284.06 MiB
we can use -e '2 k 1024 / 1024 / n [ MiB] p'
in point 3 instead (most spaces are optional).
以 MiB 为单位打印大小,就像284.06 MiB
我们可以-e '2 k 1024 / 1024 / n [ MiB] p'
在第 3 点中使用一样(大多数空格是可选的)。