How to find the largest catalogs

I want to create a bash script that defines directories over 500 MB in a directory foo.

I have this command that finds directories with foo in ascending order of size.

du /foo/* | sort -n

The problem is that it includes the size of the parent directory. So, for example, the output will be:

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

I want the output to display only /foo/bar/baz/qux. Since the parent directories are included /foo/bar/baz/quxin the native file size, but in reality these are tiny folders when excluded /foo/bar/baz/qux.


Some pseudo codes:

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
+4
source share
1 answer

With GNU findand GNU sort:

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

du -S prints the size of the directory excluding subdirectories.

+1
source

All Articles