In this quick guide, we will show you how you can get the size of a folder within Ubuntu using the terminal.
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 “
-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.
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 “
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 “
du -h ~/libdyson-neon/
Below you can see how the disk usage utility listed every directory, and its size.
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 “
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.
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.