Using “du” to determine directory size

The *nix command ls returns lots of useful information, particularly with the -alh flags, to display permissions, owner, group, modification date, file name, and human-readable size for all files and directories within the current directory.

Well, sort of.

It falls short on directory size, treating the directory like a file and returning only the size of the directory itself, helpfully ignoring its contents.

To return size information for files and directories, we can use du, which stands for disk usage. Some useful flags include:

  • -s summarizes directory information into a single line (leave off to see file sizes for the directory contents as well)
  • -h converts size information into human-readable form (e.g. 24280 becomes 24K)
  • -c appends the list with the total disk space used

You can either name the directory or use the * wildcard to return sizes for everything:

:: Return the size of a specific directory ("tmp" in this example)
du -sh tmp

:: Or return the size of all files and directories within the current directory
du -shc *