How to find the size of a directory in Linux

Contents:

  • Option 1: Display the Size of a Directory Using the du Command
  • Option 2: Get Size of Directory in Linux Using tree Command
  • Option 3: Find the Size of a Linux Directory Using ncdu Command

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.

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

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

The du command 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.

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

du -h

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.

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

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

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

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

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 discover or explore further a folder, simply use the arrow keys to navigate to it, and hit enter. Using the backspace key will return you to the parent folder. To quit, press the letter q.

Conclusion

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

Please enter CoinGecko Free Api Key to get this plugin works.