Find Out How Much Disk Space a Directory Consumed from the Command Line

Find Out How Much Disk Space a Directory Consumed from the Command Line
On a graphical desktop environment, you can find out how much disk space a directory consumed by just right clicking on a directory and going to the Properties. It’s really simple.

From the command line, you can’t see how much disk space the directory is using that easily. But there are programs such as du and ncdu that you can use to find out the disk space a directory is consuming.

In this article, I will show you how to use du and ncdu to find out the disk space a directory or directories are consuming. So, let’s get started.

Using du to Find Directory Disk Usage:

The du command is available in all modern Linux distribution by default. You don’t have to installing anything extra.

The du command with the options -s (–summarize) and -h (–human-readable) can be used to find out how much disk space a directory is consuming.

For example, let’s say, you want to check how much disk space the ~/Downloads directory is consuming. To do that, run the du command as follows:

$ du -sh ~/Downloads

As you can see, the ~/Downloads directory consumed about 813 MB of disk space.

At times, you may need super user (root) privileges in order to check how much disk space certain directory is consuming. This is because, if you don’t have permission to read a file in a directory, then the du command can’t analyze it to find the disk space that file is using as well.

For example, let’s say, you want to calculate how much disk space the /etc directory is consuming. To do that, run the du command as follows:

$ sudo du -sh /etc

As you can see, the /etc directory consumed only 13 MB of disk space.

With the du command, you can find out how much disk space each sub directories inside a directory consumed as well.

For example, if you want to know how much disk space each sub directories inside your login users HOME directory (~) consumed, then you can run the du command as follows:

$ du -h -d 1 ~

Or,

$ du -h -d 1 $HOME

As you can see, the size of each directory inside my login users HOME directory is listed. The last line also shows the total disk space consumed by the login users HOME directory.

Using ncdu to Find Directory Disk Usage:

ncdu is a ncurse based terminal disk usage analyzer. You can also call it a terminal version of graphical GNOME Disk Usage Analyzer (Baobab) app. It is a great tool for monitoring disk usage per directory basis on a headless server.

The ncdu software package is not installed by default on Ubuntu/Debian. But, it is available in the official package repository of Ubuntu/Debian. So, it is really easy to install.

First, update the APT package repository cache with the following command:

$ sudo apt update

Then, install ncdu on your Ubuntu/Debian machine with the following command:

$ sudo apt install ncdu

ncdu should be installed.

Now, you can start ncdu with the following command:

$ ncdu

Or run as super user with the following command:

$ sudo ncdu

ncdu should start. It should look as shown in the screenshot below.

By default, ncdu will show you the sub-directories of your current working directory. As you can see, it’s showing sub-directories of my login user’s HOME directory.

You can see how much disk space each directory consumed. There’s a nice looking graph there as well. It shows comparatively how much disk space is used by a particular directory. You can press the g key on your keyboard to toggle between graph, percentage, graph + percentage view if you want.

e symbol in a directory means the directory is empty.

You can see how much disk space the parent directory (in my case /home/shovon) is using in total and the number of files and directories (items) inside the parent directory here in the ncdu interface.

You can use the <Up> and <Down> arrow keys to navigate in the ncdu interface. You can also select a directory and press <Enter> to see the contents of the directory as well.

If you want to exit out of the ncdu interface, press q.

You can manually tell ncdu to summarize a specific directory when you start ncdu.

For example, let’s say, you want to take a quick look into the /usr directory with ncdu. To do that, start ncdu as follows:

$ ncdu /usr

Or

$ sudo ncdu /usr

As you can see, the contents of the /usr directory is shown in the ncdu interface.

So, that’s how you find out how much disk space a directory consumed from the command line in Linux. Thanks for reading this article.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *