我想创建一个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打印除子目录之外的目录大小.

标签: shell, bash, linux, directory

相关文章推荐

添加新评论,含*的栏目为必填