How to Get a Directory Size in Linux: Practical Examples

Getting a directory size in Linux is common for administrators and users who want to manage their disk space effectively. This article will explore 10 examples of how to get the size of a directory in Linux using various commands and scripting techniques.

Why Determine Directory Size?

Determining the size of a directory can help you manage your disk space, find large files or directories that are taking up too much space, and optimize your system’s performance. Knowing the directory size is essential for system maintenance and can help you identify potential issues before they escalate.

Example 1: Using the du Command

Basic Usage

The du (Disk Usage) command is a built-in Linux tool for estimating file space usage. To use du, navigate to the directory you want to analyze and type the following command:

du -sh

This command will display the total size of the current directory.

Display Human-Readable Format

To display the size in a human-readable format, use the -h flag:

du -sh /path/to/directory

This will show the directory size in a more readable format, such as in kilobytes (K), megabytes (M), or gigabytes (G).

Example 2: Using du with –max-depth Option

Analyzing Directory Depth

The du command can also be used to analyze directory sizes at different depths. To do this, use the --max-depth option followed by the depth level:

du -h --max-depth=1 /path/to/directory

This command will display the sizes of all subdirectories within the specified directory up to the specified depth level.

Example 3: Using find Command with du

Basic Usage

The find command can be used to locate directories and then pipe the output to du for analyzing their sizes. Here’s a simple example:

find /path/to/directory -type d -exec du -sh {} \;

This command will find all directories within the specified path and display their sizes.

Example 4: Using du with awk

Filtering Output

You can use the awk command to filter the output of du and display only specific information. For example, to display only the total size of a directory, use the following command:

du -s /path/to/directory | awk '{print $1}'

This command will show the total size of the specified directory in kilobytes.

Example 5: Using du with sort

Sorting by Size

To sort the output of du by size, you can use the sort command. Here’s an example:

du -s /path/to/directory/* | sort -n

This command will display the sizes of all items within the specified directory, sorted by size in ascending order.

Example 6: Using a Bash Function

Creating a Function

To make it easier to get the directory size, you can create a Bash function. Add the following code to your .bashrc or .bash_profile file:

function dirsize() {
  du -sh "$1"

Using the Function

After adding the function and reloading your shell, you can use the dirsize command like this:

dirsize /path/to/directory


In summary, this article has discussed several practical examples of determining the size of a directory in Linux using various built-in commands and techniques. By understanding these methods, you can effectively manage your disk space and optimize your system’s performance. Remember to choose the most suitable approach for your needs and requirements to ensure efficient directory size analysis.

Share to...