Using ls to list directories and their total sizes

Learn using ls to list directories and their total sizes with practical examples, diagrams, and best practices. Covers linux, unix development techniques with visual explanations.

Mastering 'ls': Listing Directories and Their Total Sizes in Linux

Hero image for Using ls to list directories and their total sizes

Learn how to effectively use the 'ls' command in Linux to not only list directory contents but also calculate and display their cumulative sizes. This guide covers various options and practical examples.

The ls command is one of the most fundamental utilities in Linux and Unix-like operating systems, primarily used for listing the contents of directories. While its basic usage is straightforward, many users seek to extend its functionality to include the total size of directories, not just individual files. This article delves into how to achieve this, combining ls with other powerful command-line tools to get a comprehensive view of your file system's disk usage.

Understanding Directory Sizes in Linux

Unlike files, directories themselves don't have a 'size' in the same way. A directory's size is typically understood as the sum of the sizes of all files and subdirectories contained within it. The ls command, by default, shows the size of the directory entry itself (which is usually a small, fixed number like 4KB), not the cumulative size of its contents. To get the true disk usage of a directory, we need to recursively sum up the sizes of its children.

flowchart TD
    A[User wants directory size] --> B{Use 'ls -l'?}
    B -->|No, shows entry size| C[Need recursive sum]
    C --> D[Combine 'du' with 'ls']
    D --> E[Get total directory size]

Flowchart illustrating the challenge of getting directory sizes with ls alone.

Using 'du' for Directory Disk Usage

The du (disk usage) command is specifically designed to estimate file space usage. It can recursively traverse directories and report the total size of their contents. When combined with ls, it provides the information we need. The most common options for du are -h (human-readable format) and -s (summarize, showing only a total for each argument).

du -sh /path/to/directory

Get the human-readable total size of a specific directory.

Listing Directories with Their Total Sizes

To list all subdirectories within the current directory along with their total sizes, you can combine du with find or use du directly with wildcards. Here are a few common approaches:

# Option 1: List all subdirectories in the current path with their sizes
du -h --max-depth=1 .

# Option 2: List specific subdirectories with their sizes
du -sh dir1 dir2 dir3

# Option 3: Using find to get sizes of all subdirectories (more flexible)
find . -maxdepth 1 -type d -print0 | xargs -0 du -sh

Various methods to list directories and their total sizes.

Sorting and Filtering Results

Once you have the list of directories and their sizes, you might want to sort them by size or filter them. The sort command is excellent for this, especially with the -h (human-numeric sort) option.

# List all subdirectories and sort them by size (largest first)
du -h --max-depth=1 . | sort -rh

# List all subdirectories, sort by size, and show only the top 5 largest
du -h --max-depth=1 . | sort -rh | head -n 5

Sorting and filtering directory size output.

1. Identify Target Directory

Navigate to the directory where you want to list subdirectories and their sizes, or specify the full path to the target directory.

2. Use du with --max-depth

Execute du -h --max-depth=1 . to get human-readable sizes for all immediate subdirectories. Replace . with your target path if not in the current directory.

3. Sort Results (Optional)

Pipe the output to sort -rh to sort the directories by size in reverse human-readable order (largest first). Example: du -h --max-depth=1 . | sort -rh.

4. Filter Results (Optional)

Further pipe to head -n X or tail -n X to view the top or bottom X directories by size. Example: du -h --max-depth=1 . | sort -rh | head -n 10.