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
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
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:
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.
--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/
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:
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:
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
Looks cool. Isn't it?
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.