Feature image for the article about how to get the directory size using the Linux command line

How to get the directory size using the Linux command line

With the help of your desktop environment’s file manager, you can easily obtain the size of a directory’s contents. Right-click the directory and select Properties from the pop-up menu. When working on the command line in the terminal, it’s a different story. For example, when interacting with a server over SSH. This article explains who to get the size of a directory using the Linux command line, while working in the terminal.

Background

When you run Linux on your desktop PC, you can easily obtain the size of everything inside a directory. Just open up your desktop environment’s file manager: Nautilus on GNOME, Dolphin on KDE, Thunar on XFCE, Nemo on Cinnamon and Caja on Mate. Next, right-click the directory of interest and select something along the lines of Properties from the pop-up menu. It presents you with a dialog, listing the number of files in the directory and the total size of the directory:

Nautilus file manager screenshot that show the properties dialog of a directory, including its size and the total number of files it contains.

So why would you want to know how to get the directory size using the Linux command line? Why use the terminal if you can use a graphical user interface application? Some 2021 Linux statistics to consider:

  • Only 2% of all desktop operating systems worldwide run Linux.
  • A whopping 90% of all cloud server infrastructure runs on Linux.

These statistics shows you the huge adoption Linux has in the server world. When performing server administration tasks, you typically open up your terminal program, SSH into the server and you work remotely on the command line. Sooner or later you’ll run into a situation where you want to obtain the size of a directory on the server and you’ll wonder: How do I use the Linux command line to get the size of this particular directory? This article explains exactly that: How to get the size of a directory using the Linux command line, while working in the terminal.

What do you need

To complete the steps in this article, you just need a Linux system. It can be any Linux distribution and you won’t even need a desktop environment. Your daily driver PC works fine, as does a Raspberry PI, server or a virtual machine. For this article, I decided on using my daily driver PC, which runs openSUSE Tumbleweed.

If you’re interested in trying out openSUSE Tumbleweed yourself, the article about five things to do after installing openSUSE Tumbleweed gets you started. It’s a rolling release distribution. You upgrade frequently to obtain the latest and greatest versions of all your favorite software applications. Due to its rolling nature, there is a slightly higher chance of something breaking during an update. Nothing to worry about on openSUSE Tumbleweed, because you can easily rollback to a previous snapshot to restore your system to the previously working state.

List the directory size on the Linux command line with “du”

The du program is perfect for obtaining the size of a directory. All popular Linux distributions install the du command line program by default. The man-page of du describes it as:

  • Summarizes the device usage of the set of files, recursively for directories.
Screenshot of the "du" program's man page.

Great, so we found the program to use. Now how do we actually use it? Read on.

List the directory size of the current directory

To list the directory size of your current directory, open up your terminal and cd to the directory of interest. Then run the command:

  • du -sh

Here is what it looks like when I run this command on my home directory:

Screenshot of running "du -sh" on the terminal command line for listing the directory size of the current directory.

It shows that the contents of my home directory occupy 79 GB of space.

List the directory size of a specific directory

Instead of obtaining the size of the current directory, you can also use the du program to list the size of a specific directory on the Linux command line. The command syntax for this is:

  • du -sh [DIRECTORY]

Let’s say that I want to find out about the size of my Downloads directory. I can then run this command:

  • du -sh ~/Downloads
Screenshot of running "du -sh ~/Downloads" on the terminal command line for listing the directory size of the Downloads directory in your home directory.

Apparently, my Downloads directory contains 482 MB of files.

List the directory size of a multiple directories

You can use the same du program to list the size of multiple directories. The command syntax for this:

  • du -shc [DIRECTORY 1] [DIRECTORY 2] [DIRECTORY n]

If I want to know the size of the Downloads and Music directories inside my home directory, the command to run is:

  • du -shc ~/Downloads ~/Music
Screenshot of running "du -sh ~/Downloads ~/Music" on the terminal command line for listing the directory size of both the Downloads and Music subdirectories inside your home directory.

Note that extra c parameter was added to obtain the grand total, as shown on the last line of the command output. For an explanation of these, and other du program parameters, you can always refer to its man-page:

  • man du

Alternatively, you can visit a website such as explainshell.com, to enter the entire command and view its meaning:

Web browser screenshot from the explainshell.com website, the illustrates how this site can be used to explain commonly used command line programs with specific parameters.

A note about permissions

For the du program to properly list the directory size on the Linux command line, it needs read permissions to all the files and directories it holds. Otherwise, it will output a message about Permission denied and the actual directory size will not be correct. Example:

Terminal output showing "permission denied" errors, when trying to list the directory size on a directory where your user does not have full read permission.

You bypass this problem, by running the command with sudo to obtain super user privileges:

Output of running the "du" command line program as sudo to obtain the directory size, for bypassing the permission denied error.

Wrap up

This article explained how you can obtain the size of a directory, using the command line in the Linux terminal. Perfect for those who prefer working in the terminal over graphical user interface application, and for those remotely administering servers via SSH.

We used the du command line program in the terminal, to list the directory size. The following summarizes its syntax for this particular purpose:

  • For listing the directory size of the current directory:
    • du -sh
  • Listing the directory size of a specific directory:
    • du -sh [DIRECTORY]
  • Multiple directories can also be specified:
    • du -shc [DIRECTORY 1] [DIRECTORY 2] [DIRECTORY n]

PragmaticLinux

Long term Linux enthusiast, open source software developer and technical writer.

View all posts by PragmaticLinux →