How to Check Directory Size in Ubuntu Command Line

Not getting the directory size with the ls command? Here's how to check the folder size in the Ubuntu command line.

The ls command shows the file size in human-readable format with ls -lh. However, the directory size is always displayed as 4k.

I'll explain the logic behind 4k directory size later. For now, the bigger question is how do you get the directory size in Ubuntu terminal?

Like most things in Linux, there are several ways to do it. Let's start with the 'official' command for checking the size of a directory.

Use the du command to check directory size in Ubuntu

The du (disk usage) command is a popular solution for checking directory sizes in Linux.

du [option] path_to_file_or_directory

For example, I will be getting details of Directory and use -h option to get output in human-readable form:

du -h Directory
use du command to check size of directory in ubuntu

But how could a directory containing one sub-directory worth 1.6 Gb can sum around 16 gigs? Because by default, it only shows details of sub-directories.

And by using -a option, you can include files, making it a more sensible output:

du -ah Directory
use du command to get size of every file present in specified directory

Now, you can have a better idea of how it was sized around 16 gigs!

But the du command has a lot more to offer and if you want to utilize it to its max potential, I'd recommend checking out our detailed guide:

How to Check Directory Size in Linux Command Line
The du command in Linux is used for checking the size of directory. Here are various ways you can find the size of directory in Linux with the du command.

Use the tree command to check directory size in Ubuntu

You will find me using the tree command in various guides to show the file system in a hierarchy but it can also be used to get directory size.

By using --du with the tree command, it will get you the total size of a directory including the size of every file and sub-directory present inside.

tree --du -h Directory/
use tree command to check size of directory in ubuntu

And if you're curious why I used the -h option here, it is for getting output in the human-readable form!

Use the ncdu utility to check directory size in Ubuntu

Built on top of du (which I discussed earlier), the ncdu is an interactive and efficient way of checking disk space.

But it requires manual installation and can be done through the given command:

sudo apt install ncdu

Now, you just have to append the directory with the ncdu command:

ncdu Directory/
check directory size using ncdu utility in ubuntu

Use the dust utility to check directory size in Ubuntu

You can think of dust as an enhanced version of tree utility that is specifically made for checking disk space.

It has a fascinating way of presenting data with graph and shows how many percent of the total size is occupied by a single file!

But you'd need to use the cargo package manager for installation and to set up cargo in Ubuntu, you can check out our detailed guide:

How to Install Rust and Cargo on Ubuntu & Other Linux
Learn various ways of installing Rust programming language on Linux along with its package manager Cargo.

And here are quick 3 commands to install and setup cargo:

sudo apt install curl
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env

Once you are done with the setup, you can install dust by the given command:

cargo install du-dust

Now pair the name of the directory with the dust command:

dust Directory/
use dust utility to check directory size in ubuntu

Looks cool. Isn't it?

Final Words

Through this guide, I explained multiple ways of checking directory size and the last two were my favorite ones as they went with a different approach.

Now coming back to the mystery of the 4K directory size the ls command shows. You know, a directory is simply a special file that has the details of where the files (inside the directory) are stored in the memory. The 4K is the block size of the system, and every file has this minimum block size.

And if you have any queries, make sure to leave a comment and I'll be there with answers.

Linux Foundation Training