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.
- 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
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:
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:
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
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
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
sudo du -hc --max-depth=0 /var
If you wanted to list only the top directory and the first layer of subdirectories, change
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:
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
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
tree can target a specific directory:
This command takes a few moments since the /var directory has many entries.
tree command also has a help file, which you can access by entering:
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:
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:
For help, press the
? key inside the ncdu interface. To quit, press the letter
You now have three different options to find the size of a directory in Linux operating systems.