即使 maxdepth 值为 2,find 命令也会发送子文件夹?

即使 maxdepth 值为 2,find 命令也会发送子文件夹?

我有这个 bash 命令:

find . -type d -mindepth 1 -maxdepth 2 | du -h --threshold=2KB

我得到了这些结果:

r2g: the tarball folders: 8.0K  ./cli
r2g: the tarball folders: 24K   ./dist/commands/ls
r2g: the tarball folders: 48K   ./dist/commands/add
r2g: the tarball folders: 36K   ./dist/commands/run
r2g: the tarball folders: 20K   ./dist/commands/basic
r2g: the tarball folders: 36K   ./dist/commands/config/local
r2g: the tarball folders: 36K   ./dist/commands/config/global
r2g: the tarball folders: 92K   ./dist/commands/config
r2g: the tarball folders: 4.0K  ./dist/commands/find
r2g: the tarball folders: 44K   ./dist/commands/init
r2g: the tarball folders: 272K  ./dist/commands
r2g: the tarball folders: 416K  ./dist
r2g: the tarball folders: 48K   ./assets
r2g: the tarball folders: 504K  .

但是当这个命令单独运行时:

find . -type d -mindepth 1 -maxdepth 2

我只得到这个:

r2g: the tarball folders: ./assets
r2g: the tarball folders: ./cli
r2g: the tarball folders: ./dist
r2g: the tarball folders: ./dist/commands

有人知道发生了什么事吗?我猜 du 命令正在显示深度为 2 的文件夹的子文件夹,即使 find 没有明确传递它们,我想我必须以某种方式将 du 限制在深度?

答案1

当为du实用程序提供目录路径名时,默认情况下它将显示该目录及其所有子目录的大小。

当没有给出目录时(如示例代码中所示),它将显示当前目录及其所有子目录的大小。

您的示例代码不会传递任何内容,du因为该du实用程序不从标准输入读取。

您想计算每个目录的大小,然后使用

find . -mindepth 1 -maxdepth 2 -type d -exec du -h --threshold=2KB -s {} \;

请注意,这仍将在计算中包括每个子目录的大小,因为du它会自行递归,但-s仅显示总大小。

您想计算每个目录的大小吗排除其子目录的大小,那么您必须du显式传递目录中文件的文件名。正确地做到这一点有点困难,所以除非确实需要,否则我不会这样做(我尝试的初稿最终findfind......)

相关内容