How to Get the Size of a Directory in Linux

May 15, 2020

Introduction

Many users run Linux from the command line. However, the command line - sometimes known as the terminal - doesn’t have an intuitive interface for checking disk space in Linux.

This guide shows you how to find the size of a specific directory in Linux from the command line.

Tutorial on how to find directory size linux

Prerequisites

  • A system running Linux
  • A command line / terminal window (available by clicking Search, then typing terminal)
  • A user account with sudo or root privileges

Note: In Linux, a directory is the equivalent of a folder in Windows. A directory may have directories inside (called subdirectories), or it may only contain files.

Option 1: Display the Size of a Directory Using the du Command

The ducommand stands for disk usage. This command is included by default in most Linux distributions.

You can display the size of your current directory by typing du in the command line:

du

The system should display a list of the contents of your home directory, with a number to the left. That number is the size of the object in kilobytes.

An example of the du command output.

You can add the -h option to make the output more readable:

du -h
Readable list of contents of your home directory.

Each entry will start with a number and a letter. The number is the amount of space used, and the letter (usually K, M, or G) indicates Kilobytes, Megabytes, or Gigabytes. For example:

400K – 400 kilobytes
7.3M – 7.3 megabytes
2.2G – 2.2 gigabytes

To find the size of a specific directory different from your current working directory. The du command allows you to specify a directory to examine:

du -h /var

This displays the size of the contents of the /var directory. You may see some entries with an error, as in the image below.

Display the size of a specific directory.

This happens when your user account does not have permission to access a particular directory. Use the sudo or su command to get access privileges:

sudo du -h /var

Note: Some versions of Linux don’t enable sudo by default. You can use the su command to switch to the root user account instead.

To display total disk usage of a particular directory, use the -c command:

sudo du -c /var

Options can be combined. If you wanted to repeat the previous command in human-readable format, enter the following:

sudo du -hc /var

You can limit the scan to a certain level of subdirectory by using the max-depth option. For example, to scan only the size of the top directory, use --max-depth=0:

sudo du -hc --max-depth=0 /var

If you wanted to list only the top directory and the first layer of subdirectories, change --max-depth=1:

sudo du -hc --max-depth=1 /var
Find the size of the top directory

If you run into trouble or want to explore more options for the du command, enter the following command to display the help file:

man du

Option 2: Get Size of Directory in Linux Using tree Command

By default, the tree command is not included in some versions of Linux. To install it, enter the following:

  • For Debian / Ubuntu
sudo apt-get install tree
  • For CentOS / RedHat
sudo yum install tree

The tree command displays a visual representation of your directories. It uses lines to indicate which subdirectories belong where, and it uses colors to indicate directories and files.

tree can also be used with options. To display a human-readable size of the current directory’s subdirectories, enter the following:

tree -d -h
Display disk usage using the tree command.

Like the du command, tree can target a specific directory:

tree /var

This command takes a few moments since the /var directory has many entries.

The tree command also has a help file, which you can access by entering:

man tree

Option 3: Find the Size of a Linux Directory Using ncdu Command

The ncdu tool stands for NCurses Disk Usage. Like the tree command, it is not installed by default on some versions of Linux. To install it, enter the following:

  • For Debian / Ubuntu
sudo apt-get install ncdu
  • For CentOS / RedHat
sudo yum install ncdu

The ncdu utility is an interactive display of your disk usage. For example, enter the following:

ncdu
Display Disk Usage Using the ncdu command.

In the upper left corner, it displays the current directory being scanned. A column on the left displays the numerical size, a graph of #- signs to indicate the relative size, and the file or directory.

Use the up and down arrows to select different lines. The right arrow will browse into a directory, and the left arrow will take you back.

ncdu can be used to target a specific directory, for example:

ncdu /var

For help, press the ? key inside the ncdu interface. To quit, press the letter q.

Note: Learn how to move directories in Linux using the GUI or system commands.

Conclusion

You now have three different options to find the size of a directory in Linux operating systems.

If you want to learn more about directories in Linux, read our article how to rename directories in Linux.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
Linux Commands Cheat Sheet: With Examples
February 21, 2020

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
How to Check Disk Space in Linux
April 13, 2020

This tutorial shows how to display disk usage from a command line in Linux. It is important to know how much...
Read more
How to Use fsck Command to Check and Repair Filesystem
May 14, 2020

If you want to keep your filesystems clean and without errors, you need to scan it on regular basis. In this...
Read more
How To Customize Bash Prompt in Linux
May 12, 2020

Follow this article to learn how to make changes to your BASH prompt. The guide shows how to edit the bashrc...
Read more