Linux is frequently run using the command line. However, Linux’s command line, also referred to as the terminal, lacks an easy-to-use interface for checking disc space. We’ll look at various approaches to finding a directory’s size in Linux. Understanding how directories use disc space can help you allocate resources more effectively, spot storage bottlenecks, and manage your Linux system more efficiently. To organize and manage files one needs to create directories through the mkdir command.

This article explains how to use the Linux command line to determine the size of a particular directory. For effective storage management on a Linux system, reliable monitoring of disc space use is essential. Keeping track of the sizes of individual files is very simple, but measuring the size of an entire directory can be more difficult. Fortunately, Linux comes with several commands and utilities that let users find out a directory’s size and how much disc space it takes up.

Option 1: Show the Directory’s Size Through the du Command

Ducommand is an acronym for disc use. Most Linux distributions come with this command by default.

By entering du in the command line, you can see the directory’s size:

du

Your home directory’s contents should be listed on the screen along with a number to the left. The amount there represents the object’s kilobyte size.

display size of directory using du command

To make the output easier to read, add the -h option:

du -h

readable list of contents of home directory

Each entry will be preceded by a letter and a number. The letter (often K, M, or G) stands for Kilobytes, Megabytes, or Gigabytes while the number is the amount of space used. For instance:

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

To determine a directory’s size distinct from your current working directory. You can provide a guide to inspect with the du command:

du -h /var

This shows the volume of the /var directory’s contents. You can come across some entries containing mistakes, as in the picture below.

display size of specific directory

This occurs when your user account is denied access to a specific directory. To gain access rights, use the sudo or su command:

sudo du -h /var

Use the -c command to view the total disc use of a certain directory:

sudo du -c /var

You can combine options. Enter the following if you want to repeat the previous command in human-readable form:

sudo du -hc /var

Using the max-depth option, you can restrict the scan to a specific level of subfolder. Use –max-depth=0, for instance, to scan only the top directory’s size.

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

Change –max-depth=1 to only list the root directory and the top-level subdirectories:

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

get list of only top directory using command

Enter the following command to view the help file if you need assistance or want to learn more about the options available with the du command:

man du

Option 2: Some Linux Versions may not have the tree command by default.

Enter the Next to Install it:

The visual representation of your folders is shown by the tree command. It utilizes colors to represent folders and files and lines to show which subdirectories belong where.

For the Intent of Debian / Ubuntu

sudo apt-get install tree

For the Intent of CentOS / RedHat

sudo yum install tree

The visual representation of your folders is shown by the tree command. It utilizes colors to represent folders and files and lines to show which subdirectories belong where. The tree can be combined with choices as well. Enter the following to see the size of the subdirectories in the current directory in human-readable form:

tree -d -h

using the tree command show disk usage

Similar to the du command, the tree can focus on a certain directory:

tree /var

Due to the large number of items in the /var directory, this command takes some time.

You may access the help file for the tree command by typing the following:

man tree

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

NCurses Disc Usage stands for the ncdu utility. It is not always installed by default on some Linux versions, just like the tree command. Enter the next to install it:

Regarding Debian/Ubuntu:

sudo apt-get install ncdu

Regarding CentOS / RedHat:

sudo yum install ncdu

Your disc consumption is displayed interactively by the ncdu tool. Enter the following, for instance:

ncdu

show disk usage by ncdu command

It shows the directory that is now being scanned in the top left corner. The file or directory, the numerical size, and a graph of #- signs to show the relative size are all displayed in a column on the left.

To choose alternative lines, use the up and down arrows. The left arrow will return you, and the right arrow will navigate you to a directory. When using ncdu to target a particular directory, for instance:

It shows the directory that is now being scanned in the top left corner. The file or directory, the numerical size, and a graph of #- signs to show the relative size are all displayed in a column on the left. To choose alternative lines, use the up and down arrows. The left arrow will return you, and the right arrow will navigate you to a directory. When using ncdu to target a particular directory, for instance:

ncdu /var

Final Thought on How to Get the Size of a Directory in Linux

In this tutorial, we looked at using the du command to determine the overall size of a directory in Linux. As you can see, finding the size of a directory or folder in Linux is not difficult. In Linux operating systems, you have three choices for determining a directory’s size.

Users who are aware of the size of Linux folders are better equipped to allocate storage space wisely, spot areas for improvement, and guarantee effective disc space usage. It makes it easier to organize things well, clean up when necessary, and take preventative maintenance measures to avoid running out of space or using up unneeded resources.