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:
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:
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:
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:
With the -hc option, you can display the size of the specific directory in human-readable form, as follows:
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:
Similarly, to retrieve the top directory with one layer of subdirectory, then you will set max-depth=1.
If you want to explore more commands related to du, then you can use the following command:
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:
To display the current directory, subdirectories and files use the following command on the terminal:
With the tree command, you may also retrieve the content of a specific directory using the following syntax:
To list the content of /var directory, you will use the following command:
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:
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:
Using ncdu, you can view an interactive display of your system disk usage. Execute the following to try out this command:
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:
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.