加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何找到最大的目录

发布时间:2020-12-13 23:26:51 所属栏目:Linux 来源:网络整理
导读:我想创建一个bash脚本,在目录foo中定位超过500mb的目录. 我有这个命令,以升序大小顺序查找foo目录. du /foo/* | sort -n 这个问题是它包括父目录的大小.例如,输出将是: ...442790 /foo/bar/baz/qux442800 /foo/bar/baz442880 /foo/bar 我希望输出只显示/ fo
我想创建一个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排序:

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

du -S打印除子目录之外的目录大小.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读