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.
- 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 [option] [directory-or-file]
du command works without any options. However, using the options listed in the table below allows you to customize the command's output:
|Shows file/directory size in a human-friendly format.
|Shows only the total size of the specified directories rather than breaking down the sizes for each subdirectory and file within them.
|Skips directories on different file systems.
|Shows sizes for files as well as directories.
|Displays the total disk usage in the last line.
|Excludes files/directories that match the specified [pattern].
|Limits the depth of the directory tree
du travels to [N] levels.
|Shows the last modification time for any file or directory.
|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:
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.
Print the Current Directory Size in a Human-Friendly Format
-h option to the
du command to make the output easily readable:
Each entry starts with a number and a letter. The number is the space used, and the letter
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
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
sudo du -h /var
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
du -c ./example1
To repeat the same command but print the output in human-readable format, enter the following:
du -hc ./example1
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
du -h --max-depth=0 ./example1
To list the top directory (example1) and the first layer of subdirectories (example2 and example3), use
du -h --max-depth=1 ./example1
Print the Disk Usage Summary for a Directory
-s option prints the disk usage summary for a directory. For instance, print the example1 usage summary with:
du -s ./example1
Print all File and Directory Sizes
-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
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.
tree command syntax is:
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
tree command, when run without any arguments, doesn't show directory size. However, using
-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:
Print a Specific Directory Size
To get the size of a specific directory, run
-d arguments. For instance, get the example1 size with:
tree -hd ./example1
The output shows all example1 subdirectories and their sizes in a human-readable format.
Combine tree with du
tree command also works in combination with
du. For instance, find the size of example1 with:
tree -h --du ./example1
The output shows the sizes of example1 and its subdirectories (in blue) and all the files they contain (in red).
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]
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
ncdu without any arguments to see the current directory size. Execute the following:
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
ncdu command also provides the information of a specific directory. For instance, check Example1 size with:
For help, press the
? key inside the ncdu interface. To quit, press the letter
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.
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.
3. Navigate to the directory for which you want to find the size. In this case, it's example1.
4. Right-click the directory.
5. Choose Properties or a similar option to view the information about the directory size.
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.
3. Launch the Disk Usage Analyzer application.
4. Select the directory to analyze. In this case, it's example1.
The tool shows a graphical representation of disk space usage.
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.