Disk Management disk space

Check Directory Size in Linux

It is quite easy to check the size of directories and files using GUI. Getting the size of a directory using the command line can be much more difficult than when using GUI. With the ‘ls’ command, you can list the contents of a directory but you cannot see the exact space or directory size. Instead, you must explore more commands to get the exact size of the directory or file.

In this article, you will learn how to check directory size on Linux using the command line environment. All the commands demonstrated in this article were run on an Ubuntu 20.04 system. All methods and steps are performed in the terminal. You can quickly open the terminal window by typing Ctrl + Alt + t.

Following are some methods you may use to check directory size on Linux systems. We will explore these methods one by one:

Method 1: Check Directory Size Using du Command

The default command used to check the size of the directory is known as the ‘du’ command, which stands for disk usage. The du command is installed on most Linux distributions. Using the du command, you can view the current directory size of your system, as follows:

$ du

The above command displays a list of the home directory contents. The numbers displayed to the left show the sizes, in kilobytes, of each object.

Using the -h option, you can also display the output in more descriptive form, as follows:

$ du –h

The above command displays the space in the kilo, mega, and Gigabytes with numbers.

To find the size of the specific directory, use the following syntax:

$ du –h /directory-path

You will need to run the above command as the sudo user, because some directories require certain permissions access particular directory content.

Use the following command to check the directory size of the /var directory:

$ sudo du –h /var

With the -hc option, you can display the size of the specific directory in human-readable form, as follows:

$ sudo du -hc /var

You can also change the subdirectory path depth using the max-depth option. For example, if you only wanted to display the top directory, then you would need to set the max-depth=0, as follows:

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

Similarly, to retrieve the top directory with one layer of subdirectory, then you will set max-depth=1.

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

If you want to explore more commands related to du, then you can use the following command:

$ man du

Method 2: Check Directory Size Using tree Command

The tree command is used to display directories, subdirectories, and files in the tree format. You can make this command more useful by inputting flags and options for customization. The tree command does not come already installed on most of the Linux systems. You can install this command using the apt package manager, as follows:

$ sudo apt install tree

To display the current directory, subdirectories and files use the following command on the terminal:

$ tree -d -h

With the tree command, you may also retrieve the content of a specific directory using the following syntax:

$ tree /directory-path

To list the content of /var directory, you will use the following command:

$ tree /var

After completing the command, it will display the total number of directories and subdirectories.

To learn more about the tree command, use the following command:

$ man tree

Method 3: Check Directory Size Using ncdu Command

The NCurses Disk Usage, abbreviated ‘ncdu,’ is also used to check the directory size. ncdu is not installed by default on most Linux systems. You will need to install this command using the command line through the apt package manager, as follows:

$ sudo apt install ncdu

Using ncdu, you can view an interactive display of your system disk usage. Execute the following to try out this command:

$ ncdu

The upper top left corner displays the current directory being viewed. The left column displays directory size in the numerical value, where the # signs indicate the size next to each directory. Using the arrow keys, you can navigate between these lines. Here, the purpose of the right arrow is to browse the directory, and the purpose of the left arrow is to move you back.

With the ncdu command, you may also target a particular directory, as follows:

$ ncdu /var

To quit the ncdu interface, press ‘q,’ and for help, press ‘?’.

In this article, you learned how to check directory size of using the terminal command line on Ubuntu 20.04 Linux systems through three different methods. You may explore more commands related to the tree, ncdu, and du commands using the terminal. These commands can be used on all Linux distributions. If you have any questions or suggestions, feel free to comment below.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.