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

29/12/2020
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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Huớng dẫn dùng proxy cho ios, iphone 2023

Cách thay đổi Proxy trên Iphone: Để thay đổi địa chỉ IP bạn cần các bước sau: 1. Truy cập...
23/09/2023

Vim and Ctags

Ctags is a very useful tool to navigate any source code of the programming language. Identifiers, methods, classes, etc....
29/12/2020

5 Best Linux Distributions for Gaming

After writing about Steam on Linux in my last article, I’m here with another gaming article for fellow Linux gamers out...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024