Getting the Size of a Folder on Ubuntu

In this quick guide, we will show you how you can get the size of a folder within Ubuntu using the terminal.

Ubuntu Get Directory Size

While you can easily use the “ls” command on your Ubuntu to list the size of files, you will quickly notice that it doesn’t list the size of the folder’s contents and just the folder itself, meaning the folder size will look tiny.

Instead, you will want to utilize the du command to get the size of a directory/folder on Ubuntu. Instead of listing the folder size, it summarizes the size of each file and directory within the specified destination.

Since we are using are command to get the size of a folder, you will need to start by opening the terminal on your Ubuntu device. One of the quickest ways to do this is to press CTRL + SHIFT + T on your keyboard.

Finding the size of directories can be helpful for quickly working out how much something is consuming on your drives.

Using the du Command to List the Size of a Folder

The tool that we will be using to get the size of a folder on Ubuntu is called “disk usage” and is called by using “du” within the command line.

du works by recursively searching through the directory you specify, getting the size and details of every file. Best of all, you don’t have to install anything to your system for this to work. It’s featured as a core part of the Ubuntu operating system.

Getting the Size of a Directory on Ubuntu

You will want to use the following command to get the du utility to list the size of a directory on Ubuntu.

You need to type “du” followed by the “-hs” options, and finally, the destination of the directory you want to get the size of.

du -hs /PATH/TO/DIRECTORY
  • -h – This first option tells the du command to print a human-readable version of the results. Instead of seeing a number like “40616“, you would see it written as “40M“.
  • -s – The other option we utilize tells the disk usage tool to summarize its result. Without this option, the command will print out the size of each folder and file within the directory you have specified.

Example of Getting the Size of a Folder on Ubuntu

For example, if you wanted to get the size of your home directory, you could use the following command on Ubuntu.

du -hs ~

After running the command, you should end up with a single-line result, as shown below.

With the result, you can see the total size of that folder, followed by its full path on your system.

40M     /home/pi

Getting the Size of a Directory and its Subdirectories on Ubuntu

The du utility also allows you to get your designated directory’s size and sub-directories. The only change needed for the utility to do this is to remove the “-s” option.

By removing the “-s” option, the du utility won’t produce a summary and instead print out the size of every directory it traverses.

du -h /PATH/TO/DIRECTORY

We use the “-h” option again here as it makes the result much more readable and easier to scan through.

Example of Print the Size of Every Folder and its Subfolders

To show how this works and the sort of output you will get, let us use du on our Ubuntu device to get the size of a folder located at “~/libdyson-neon/“.

du -h ~/libdyson-neon/

Below you can see how the disk usage utility listed every directory, and its size.

92K     /home/pi/libdyson-neon/libdyson/__pycache__
36K     /home/pi/libdyson-neon/libdyson/cloud/__pycache__
72K     /home/pi/libdyson-neon/libdyson/cloud
244K    /home/pi/libdyson-neon/libdyson
8.0K    /home/pi/libdyson-neon/.git/refs/remotes/origin
12K     /home/pi/libdyson-neon/.git/refs/remotes
8.0K    /home/pi/libdyson-neon/.git/refs/heads
4.0K    /home/pi/libdyson-neon/.git/refs/tags
28K     /home/pi/libdyson-neon/.git/refs
324K    /home/pi/libdyson-neon/.git/objects/pack
4.0K    /home/pi/libdyson-neon/.git/objects/info
332K    /home/pi/libdyson-neon/.git/objects
64K     /home/pi/libdyson-neon/.git/hooks
8.0K    /home/pi/libdyson-neon/.git/logs/refs/remotes/origin
12K     /home/pi/libdyson-neon/.git/logs/refs/remotes
8.0K    /home/pi/libdyson-neon/.git/logs/refs/heads
24K     /home/pi/libdyson-neon/.git/logs/refs
32K     /home/pi/libdyson-neon/.git/logs
4.0K    /home/pi/libdyson-neon/.git/branches
8.0K    /home/pi/libdyson-neon/.git/info
496K    /home/pi/libdyson-neon/.git
12K     /home/pi/libdyson-neon/.github/workflows
16K     /home/pi/libdyson-neon/.github
40K     /home/pi/libdyson-neon/tests/cloud
132K    /home/pi/libdyson-neon/tests
964K    /home/pi/libdyson-neon/

Using du to get the Size of a Directory and its Files and Subdirectories

If you want to take things one step further, you can use du to list the size of a directory, its files, and its subdirectories.

To achieve this, you must utilize the “-a” option. This option tells the du utility to list the size of every file it traverses, not just a summary.

du -ha /PATH/TO/DIRECTORY.

Example of Listing the Size of all directories, files, and subdirectories on Ubuntu

To give you an example, let us again use the “du” command on the “~/libdyson-neon” directory.

du -ha ~/libdyson-neon

Below you can find a snippet of what this command will provide. It is only beneficial if you want an idea of every directory and the files that are contained within it.

The larger the directory tree is, the clumsier this will get to read.

4.0K    /home/pi/libdyson-neon/tests/cloud/utils.py
4.0K    /home/pi/libdyson-neon/tests/cloud/test_bearer_auth.py
12K     /home/pi/libdyson-neon/tests/cloud/test_dyson_account.py
4.0K    /home/pi/libdyson-neon/tests/cloud/mocked_requests.py
4.0K    /home/pi/libdyson-neon/tests/cloud/__init__.py
4.0K    /home/pi/libdyson-neon/tests/cloud/conftest.py
4.0K    /home/pi/libdyson-neon/tests/cloud/test_cloud_360_eye.py
40K     /home/pi/libdyson-neon/tests/cloud
132K    /home/pi/libdyson-neon/tests
4.0K    /home/pi/libdyson-neon/.yamllint
964K    /home/pi/libdyson-neon

Conclusion

Hopefully at this point you will now know how you can get the size of a directory on Ubuntu by using the terminal.

The du utility is perfect for this as it has built-in functionality to summarize every directory and file it finds.

Please comment below if you have any questions about working out the size of a folder.

If you found this tutorial helpful, be sure to check out our many other Ubuntu guides.

Leave a Reply

Your email address will not be published. Required fields are marked *