聚合测量某些文件类型的磁盘空间
我的一些文件分布在多个文件夹中:
/home/d/folder1/a.txt
/home/d/folder1/b.txt
/home/d/folder1/c.mov
/home/d/folder2/a.txt
/home/d/folder2/d.mov
/home/d/folder2/folder3/f.txt
如何测量 /home/d/ 中所有 .txt 文件占用的磁盘空间总量?
我知道 du 会给我给定文件夹的总空间,ls -l 会给我单个文件的总空间,但是如果我想加起来怎么办所有 txt 文件,然后查看 /home/d/ 中所有 .txt 的所有 .txt 文件占用的空间,包括folder1和folder2及其子文件夹(例如folder3)?
I have some files across several folders:
/home/d/folder1/a.txt
/home/d/folder1/b.txt
/home/d/folder1/c.mov
/home/d/folder2/a.txt
/home/d/folder2/d.mov
/home/d/folder2/folder3/f.txt
How can I measure the grand total amount of disk space taken up by all the .txt files in /home/d/?
I know du will give me the total space of a given folder, and ls -l will give me the total space of individual files, but what if I want to add up all the txt files and just look at the space taken by all .txt files in one giant total for all .txt in /home/d/ including both folder1 and folder2 and their subfolders like folder3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
查找文件夹 1 文件夹 2 -iname '*.txt' -print0 | du --files0-from - -c -s | 杜--files0-from--c -s |尾部-1
find folder1 folder2 -iname '*.txt' -print0 | du --files0-from - -c -s | tail -1
这将按扩展名报告磁盘空间使用情况(以字节为单位):
输出:
This will report disk space usage in bytes by extension:
Output:
简单:
如果您只想显示所占用的总空间,那么:
Simple:
If you just want the total space taken to show up, then:
这是一种方法(在 Linux 中,使用 GNU coreutils
du
和 Bash 语法),避免不良做法:如果要排除当前目录,请将
-mindepth 2
与find
结合使用。另一个不需要 Bash 语法的版本:
请注意,这些版本对于包含换行符的文件名将无法正常工作(但带有空格的文件名可以正常工作)。
Here's a way to do it (in Linux, using GNU coreutils
du
and Bash syntax), avoiding bad practice:If you want to exclude the current directory, use
-mindepth 2
withfind
.Another version that doesn't require Bash syntax:
Note that these won't work properly with file names which include newlines (but those with spaces will work).
macOS
du
和参数-I
排除所有其他文件Linux
macOS
du
and the parameter-I
to exclude all other filesLinux
这将做到这一点:
This will do it:
GNU 发现,
GNU find,
在 ennukiller 的基础上,这将处理名称中的空格。我需要这样做并获得一些报告:
find -type f -name "*.wav" | grep 导出 | ./calc_space
Building on ennuikiller's, this will handle spaces in names. I needed to do this and get a little report:
find -type f -name "*.wav" | grep export | ./calc_space
对于那些在 bash 上使用 GNU 工具的人来说,一则说明:
您必须启用 extglob:
如果您希望点文件工作,则必须运行
示例输出:
等
A one liner for those with GNU tools on bash:
You must have extglob enabled:
If you want dot files to work, you must run
Sample output:
etc
我的解决方案是获取给定路径和子目录中所有文本文件的总大小(使用 perl oneliner)
my solution to get a total size of all text files in a given path and subdirectories (using perl oneliner)
我喜欢将 find 与 xargs 结合使用:
如果您只想查看总计,请添加 tail
I like to use find in combination with xargs:
Add tail if you only want to see the grand total
对于任何想要在命令行上使用 macOS 执行此操作的人,您需要基于 -print0 参数而不是 printf 的变体。上面的一些答案解决了这个问题,但这将通过扩展来全面地做到这一点:
For anyone wanting to do this with macOS at the command line, you need a variation based on the -print0 argument instead of printf. Some of the above answers address that but this will do it comprehensively by extension:
接受的答案有几个潜在的问题:
globstar
)globstar
) wledge.org/ParsingLs" rel="nofollow noreferrer">解析ls
的输出正如由ghostdog74提议的,您可以使用GNU特定的
-printf< /code> 选项来
find
实现更强大的解决方案,避免所有过多的管道、子 shell、Perl 和奇怪的du
选项:是的,是的,解决方案使用
paste
或bc
也是可以的,但不是更简单。在 macOS 上,您需要使用 Homebrew 或 MacPorts 来安装
findutils< /code>,然后调用
gfind
。 (我看到这个问题上的“linux”标签,但它也被标记为“unix”。)如果没有GNU
find
,您仍然可以回退到使用du
:……但您必须注意,由于历史原因,
du
的默认输出位于 512 字节块中(请参阅手册页的“RATIONALE”部分),以及某些版本du 的(尤其是 macOS 的)甚至没有以字节为单位打印大小的选项。这里有许多其他好的解决方案(特别是参见 Barn 的答案),但大多数都存在不必要的复杂或过于依赖的缺点仅适用于 GNU 的功能 — 也许在您的环境中,那没关系!
There are several potential problems with the accepted answer:
globstar
)ls
As proposed by ghostdog74, you can use the GNU-specific
-printf
option tofind
to achieve a more robust solution, avoiding all the excessive pipes, subshells, Perl, and weirddu
options:Yes, yes, solutions using
paste
orbc
are also possible, but not any more straightforward.On macOS, you would need to use Homebrew or MacPorts to install
findutils
, and callgfind
instead. (I see the "linux" tag on this question, but it's also tagged "unix".)Without GNU
find
, you can still fall back to usingdu
:…but you have to be mindful of the fact that
du
's default output is in 512-byte blocks for historical reasons (see the "RATIONALE" section of the man page), and some versions ofdu
(notably, macOS's) will not even have an option to print sizes in bytes.Many other fine solutions here (see Barn's answer in particular), but most suffer the drawback of being unnecessarily complex or depending too heavily on GNU-only features—and maybe in your environment, that's OK!