linux - 如何定位最大的目录

标签 linux bash shell directory

我想创建一个 bash 脚本来定位目录 foo 中超过 500mb 的目录。

我有这个命令,它按大小升序查找带有 foo 的目录。

du /foo/* | sort -n

问题在于它包括父目录的大小。因此,例如输出将是:

...
442790 /foo/bar/baz/qux
442800 /foo/bar/baz
442880 /foo/bar

我希望输出只显示 /foo/bar/baz/qux。由于父目录包含 /foo/bar/baz/qux 并有自己的文件大小,但实际上当排除 /foo/bar/baz/qux 时它们是很小的文件夹>.


一些伪代码:

if the current directory is greater than 500mb then 
    check next directory is parent to current directory (i.e `/foo/bar/baz` is parent of `/foo/bar/baz/qux`) then 
        takeaway size of parent from current.
        if resulting size is greater than 500mb then
            return row
        else
            go to next row
    else
        go to next row
else
    go to next row

最佳答案

使用 GNU find 和 GNU sort:

find /some/dir -type d -exec du -hS {} + | sort -rh

du -S 打印不包括子目录的目录大小。

关于linux - 如何定位最大的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31232491/

相关文章:

django - 无需控制键即可停止 Django 服务器

bash - 将文件从一个目录移动到另一个目录的脚本

java - 如何从 Java 程序中调用 shell 脚本?

arrays - 如何在循环的 shell 脚本中使用两个数组?

c - 如何检查没有root权限安装的C库?

c - 通过 getauxval 检测 Power8 核心加密?

linux - AWS CloudFormation bash 部署脚本与模板命令

linux - 别名在 Linux 机器上不起作用

regex - 查找/sed : How can I recursively search/replace a string in files but only for lines that match a particular regexp

linux - xwininfo 将选定的窗口置于最前面