How to list directory size of all child directories?

Viewed 5051

From the shell, I need to list the size of all child directories. I am currently using:

du -hs * | sort -hr 

However, this only gets one level down and does not traverse the directory tree.

5 Answers

The simplest is:

du -h --max-depth=1 parent

This will show all sizes of the children of parent If you also want the grandchildren, you can do

du -h --max-depth=2 parent

If you want the whole family

du -h parent

All these will just summarize the total directory size of each subdirectory up to a given level (except the last, it will give for all)

If you don't want the content of the subdirectories, add the -S flag.

You can use **/*

shopt -s globstar
du -hs **/* | sort -hr
shopt -u globstar

If sorting you will likely want a consistent output instead of having some in GB, some in MB, some in KB.... And if I read the OP correctly, it isn't getting the directory tree, and that's a problem, right?

It isn't listing subdirs because of the -s. Take that out (again, sorry if I'm misreading.)

du -b | sort -n

This lists all sizes in bytes, so you only need -n for sort.

Wildcards are nice, but they'll also list FILES and not just directories. The below will return the sizes of all directories and only the directories:

find . -type d -exec du -sh {} \; | sort -hr

The maxdepth feature should solve your issue, but for some strange reason, it seems not to work:

find ./ -maxdepth 1 -type d -exec du -k {} \;

Maybe somebody can explain why the maxdepth is not taken into account?

Related