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

Các tiện ích và công cụ để quản lý proxy.

Trong thế giới của công nghệ thông tin hiện đại, Proxy đã trở thành một công cụ không thể thiếu...
27/02/2023

Hướng dẫn hack băng thông 4G Viettell bằng Cách tạo Server V2ray Dùng Data 4G miễn phí

V2ray là gì? V2ray là một phần mềm giúp bạn tạo ra khả năng kết nối mạng riêng tư dựa vào cách...
27/05/2021

SQLite Tutorial

Ever since man started creating systems, there have always been databases corresponding to them. Fast forwarding to our...
28/12/2020
Bài Viết

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

Cách gắn set proxy cho điện thoại android, oppo, giả lập android, Ldplayer Bằng Proxydroid
20/09/2023

Mua Proxy Socks5 VN Chơi Game Gia Lập Tăng Cường Trải Nghiệm Chơi Game
22/06/2023

Mua Proxy Mỹ, Us Nuôi Tài Khoản Etsy, eBay Tìm Hiểu Về Mua Proxy Mỹ tại Onet.com.vn
22/06/2023

Mua Proxy Game – Giải pháp tuyệt vời cho việc chơi game trên mạng mà không bị giới hạn về vị trí địa lý
03/06/2023

Sử dụng Proxy để Quản Lý Tài Khoản Quảng Cáo Ads Một Cách An Toàn
27/05/2023