Mykhaylo Adamovych
Mykhaylo Adamovych

Reputation: 20966

Find most fat directories by it's own size

I would like to list most fat directories by it's own size sorted by size descending. 'Directory own size' means size of the directory excluding size of all it's subdirectories.

For example, we have directory structure:

    /tmp/D1
            |-- 5m.file
            |-- D2
            |   |-- 2m.file
            |   `-- D4
            |       `-- 4m.file
            `-- D3
                `-- 3m.file

By for executing command and passing /tmp/D1 as argument I'd like to get result like

    5m      /tmp/D1
    4m      /tmp/D1/D2/D4
    3m      /tmp/D1/D3
    2m      /tmp/D1/D2

du -Sh . | sort -rh | head -n 10

+x to limit to current filesystem only

du -Shx . | sort -rh | head -n 10

Upvotes: 1

Views: 395

Answers (1)

Inian
Inian

Reputation: 85683

You can use du with an -S option for this

From the man page

-S, --separate-dirs do not include size of subdirectories

$ du -Sh /foo/bar/temp2/ | sort -rh

84K     /foo/bar/temp2/
40K     /foo/bar/temp2/tempo
4.0K    /foo/bar/temp2/opt/logs/merchantportal
4.0K    /foo/bar/temp2/opt/logs
4.0K    /foo/bar/temp2/opt
4.0K    /foo/bar/temp2/folder
4.0K    /foo/bar/temp2/bang

Now checking in the normal way with the -s option, which is inclusive of all sub-directories.

$ du -sh /foo/bar/temp2/opt
12K     /foo/bar/temp2/opt

which is the summation of sizes of the sub-folders /foo/bar/temp2/opt/logs/merchantportal, /foo/bar/temp2/opt/logs and the base folder itself

The -h formats the size in the human readable format according to the man page. If you forcefully want to format the output in 1-Mbyte blocks you can use the option du -Sm

Upvotes: 3

Related Questions