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

How to install the Elegant Simple Weather Indicator 0.9 for Linux

Simple Weather Indicator 0.9 recently released, is an elegant and very simple weather indicator for Ubuntu unity and Gnome...
28/12/2020

How to Create a Custom Application Launcher with Quicklist in Linux

Many popular applications come with quick shortcuts that can be used to execute a specific action. If you right click on...
29/12/2020

Upstart – How is it better or worse than the others?

When Upstart was first conceived by Canonical, the prevailing system was still sysvinit, which started everything in sequence...
29/12/2020
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024