How to Get the Size of a Directory in Linux

February 8, 2024

Introduction

Checking a directory's size in Linux is a crucial task in storage management.

Large directories are hard to navigate and take up system resources. Therefore, knowing a directory's size determines whether to take any actions and free up disk space.

This guide explains how to find the size of a directory in Linux.

Tutorial on how to find directory size linux

Prerequisites

  • A system running Linux (this tutorial uses Ubuntu 22.04).
  • Access to the terminal.
  • A user account with sudo or root privileges.

Note: In Linux, the ls -l command shows file and directory information, including names, dates, and sizes (disk usage). However, the displayed size is consistently 4096 bytes irrespective of containing files larger than 4 KB. This discrepancy arises because ls provides metadata for the directory (information about the directory itself, such as its structure, permissions, timestamps, and other attributes), not their actual size.

How to Check the Size of a Directory in Linux

Apart from the ls -l command, there are other, more reliable ways to check the size of a directory in Linux. The following text presents four different methods to find the directory size.

du Command

The du (disk usage) command in Linux operating systems estimates and displays the disk space usage of files and directories. The basic du command syntax is:

du [option] [directory-or-file]

The du command works without any options. However, using the options listed in the table below allows you to customize the command's output:

OptionDescription
-h, --human-readableShows file/directory size in a human-friendly format.
-s, --summarizeShows only the total size of the specified directories rather than breaking down the sizes for each subdirectory and file within them.
-S, --separate-dirsExcludes subdirectories.
-x, --one-file-system
Skips directories on different file systems.
-a, --allShows sizes for files as well as directories.
-c, --totalDisplays the total disk usage in the last line.
-X, --exclude=[pattern]Excludes files/directories that match the specified [pattern].
d, --max-depth=[N]Limits the depth of the directory tree du travels to [N] levels.
--timeShows the last modification time for any file or directory.
–apparent-size
Prints apparent size and disregards the additional space occupied by metadata.

Print the Size of the Current Directory

Display the current directory size by running du without any arguments:

du
du terminal output

The system lists the current directory content and subdirectories with a number representing the object size in kilobytes. The current directory size is the last line.

Size of the current directory determined by du

Print the Current Directory Size in a Human-Friendly Format

Add the -h option to the du command to make the output easily readable:

du -h
du -h terminal output

Each entry starts with a number and a letter. The number is the space used, and the letter K, M, or G indicates Kilobytes, Megabytes, or Gigabytes.

Find the Size of a Specific Directory

Find the size of a directory different from the current working directory with the du command and the directory path. This example shows how to find the size of the example1 directory located in Home:

du -h ./example1
du -h ./example1 terminal output

The output shows the size of different example1 subdirectories (example2, example3, and example4) as well as the size of example1 in the last line.

However, in some cases, the output shows some entries with an error. For example, check the size of the var directory with:

du -h /var
du -h /var terminal output

The error occurs when the user account does not have permission to access a particular directory. Use the sudo or su command to get access privileges:

sudo du -h /var
sudo du -h /var terminal output

The output now shows the size of every var subdirectory.

Show Only the Total Disk Usage of a Directory

To print an extra total line at the end of the output and summarize the disk space usage for all files and directories, use the -c argument:

du -c ./example1
du -c ./example1 terminal output

To repeat the same command but print the output in human-readable format, enter the following:

du -hc ./example1
du -hc ./terminal output

Scan up to a Chosen Subdirectory Level

Limit the scan to a certain subdirectory level with the --max-depth option. For example, to scan only the top-level directory (example1), use --max-depth=0:

du -h --max-depth=0 ./example1
du -h --max-depth=0 ./example1 terminal output

To list the top directory (example1) and the first layer of subdirectories (example2 and example3), use --max-depth=1:

du -h --max-depth=1 ./example1
du -h --max-depth=1 ./example1 terminal output

Print the Disk Usage Summary for a Directory

The -s option prints the disk usage summary for a directory. For instance, print the example1 usage summary with:

du -s ./example1
du -s ./example1 terminal output

Print all File and Directory Sizes

Use the -a argument to print the sizes of all subdirectories and files contained in the directory of choice. For example, show the sizes of every file and subdirectory in example1 in a human-readable format with:

du -ha ./example1
terminal output for du -ha ./example1

tree Command

The tree command displays a visual representation of directories. It uses lines to indicate the location of subdirectories and colors to distinguish directories and files. When used with specific arguments, the command provides directory sizes.

The tree command syntax is:

tree [options]

Note: By default, the tree command is not included in some Linux distributions. To install it on Debian/Ubuntu, run sudo apt install tree. To get tree on CentOS/RedHat, execute sudo yum install tree.

Print Current Directory Size

The tree command, when run without any arguments, doesn't show directory size. However, using tree with -h displays directories and files in a human-readable format and includes their sizes. To print only directory sizes (without file sizes), add the -d argument. Therefore, print the current directory size with:

tree -hd
terminal output for tree -hd

Print a Specific Directory Size

To get the size of a specific directory, run tree with -h and -d arguments. For instance, get the example1 size with:

tree -hd ./example1
terminal output for tree -hd ./example1

The output shows all example1 subdirectories and their sizes in a human-readable format.

Combine tree with du

The tree command also works in combination with du. For instance, find the size of example1 with:

tree -h --du ./example1
tree -h --du ./example1 terminal output

The output shows the sizes of example1 and its subdirectories (in blue) and all the files they contain (in red).

ncdu Command

ncdu is a command-line utility that stands for NCurses Disk Usage. It is a text-based disk usage analyzer that provides an interactive interface to analyze disk space usage on the system. The ncdu syntax is:

ncdu [options] [directory-or-file]

Note: ncdu is not installed by default on some versions of Linux. To install it on Debian/Ubuntu, run sudo apt install ncdu. To get ncdu on CentOS/RedHat, execute sudo yum install ncdu.

Print the Current Directory Size

Run ncdu without any arguments to see the current directory size. Execute the following:

ncdu
ncdu terminal output

In the upper left corner, the output displays the current directory being scanned. The output also contains:

  • A column that displays the size of directories.
  • The visual representation of space usage in square brackets [##########].
  • The file or directory path.

The up and down arrows allow users to select different lines. The right arrow browses into a directory, and the left arrow takes users back.

Find Specific Directory Size

The ncdu command also provides the information of a specific directory. For instance, check Example1 size with:

ncdu ./example1
ncdu e./xample1 terminal output

For help, press the ? key inside the ncdu interface. To quit, press the letter q.

GUI Tools

To find the size of a directory in Linux via a graphical user interface (GUI), use the file manager or a disk usage analyzer tool. Both methods are easy to use and are explained in the sections below.

File Manager

To check the directory size via a file manager, take the following steps:

1. Access the Applications menu and search for file.

2. Open the Files tool. The tool name varies depending on the file manager you're using.

Open the file manager

3. Navigate to the directory for which you want to find the size. In this case, it's example1.

4. Right-click the directory.

Choose properties in the file manager

5. Choose Properties or a similar option to view the information about the directory size.

Example1 properties

Disk Usage Analyzer

Some Linux distributions come with a disk usage analyzer tool that visually represents disk space usage.

1. Open the Applications menu.

2. Search for a disk usage analyzer.

Find the disk usage analyzer

3. Launch the Disk Usage Analyzer application.

4. Select the directory to analyze. In this case, it's example1.

example1 in Disk Usage Analizer

The tool shows a graphical representation of disk space usage.

Conclusion

After reading this article, you know how to use five different options to find the size of a directory in Linux operating systems.

To learn more about directories in Linux, read our article on how to rename directories in Linux.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
How to Check Disk Space in Linux
April 13, 2020

This tutorial shows how to display disk usage from a command line in Linux. It is important to know how much...
Read more
How to Use fsck Command to Check and Repair Filesystem
May 14, 2020

If you want to keep your filesystems clean and without errors, you need to scan it on regular basis. In this...
Read more
How To Customize Bash Prompt in Linux
May 12, 2020

Follow this article to learn how to make changes to your BASH prompt. The guide shows how to edit the bashrc...
Read more