Learn how to know the occupied space in the disk by directories and files.

How to get and display the size of directories in Linux

If you are willing to know how much spaces occupies some directory on your linux based server (CentOS, Ubuntu) with the CLI, you are looking for the disk usage utility (du), that displays the amount of disk space used by the specified files and for each subdirectory.

When using this command is useful to use any of the following arguments during its initialization:

  • -h: Display size in K (kilobytes), M (megabytes), G (gigabytes) instead of the default kilobytes.
  • -s: Summarize result and display only a total for each argument.
  • -c: Display a total sum of all the space occupied by directories and files in the execution path.

How to use

The usage of the command is pretty straightforward, for example if you want to know the space occupied by files and directories in the current directory, you could use:

du -sh *

If you want as well a total (sum) of the files and directories, you can add the c argument:

du -shc *

If you want to know directly the total size of a directory, provide the path as argument:

du -sh /var/www/mydirectory

Happy coding !

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.