How to get the summarized sizes of directories and their subdirectories?

Let's say I want to get the size of each directory of a Linux file system. When I use ls -la I don't really get the summarized size of the folders.

If I use df I get the size of each mounted file system but that also doesn't help me. And with du I get the size of each subdirectory and the summary of the whole file system.

But I want to have only the summarized size of each directory within the ROOT folder of the file system. Is there any command to achieve that?


Solution 1:

This does what you're looking for:

du -sh /*

What this means:

  • -s to give only the total for each command line argument.
  • -h for human-readable suffixes like M for megabytes and G for gigabytes (optional).
  • /* simply expands to all directories (and files) in /.

    Note: dotfiles are not included; run shopt -s dotglob to include those too.

Also useful is sorting by size:

du -sh /* | sort -h

Here:

  • -h ensures that sort interprets the human-readable suffixes correctly.

Solution 2:

I often need to find the biggest directories, so to get a sorted list containing the 20 biggest dirs I do this:

du -m /some/path | sort -nr | head -n 20

In this case the sizes will be reported in megabytes.

Solution 3:

I like to use Ncdu for that, you can use the cursor to navigate and drill down through the directory structure it works really well.