Using du Command to Get Directory Total Sizes: Beyond ls Limitations

Oct 26, 2025 · Programming · 15 views · 7.8

Keywords: du command | directory size | disk usage | Unix commands | Linux system administration

Abstract: This article provides an in-depth exploration of accurately obtaining the total size of directories and their contents in Unix/Linux systems. By analyzing the limitations of the ls command, it focuses on the powerful capabilities of the du command, including the usage of -s and -h parameters, and presents various command combinations for practical scenarios. The article also compares different parameter options to help readers deeply understand core concepts of disk space management.

Analysis of ls Command Limitations

In Unix and Linux systems, the ls command is the most commonly used tool for listing files and directories, but it has significant limitations when displaying directory sizes. When using ls -l, the system typically shows directory sizes as 4KB, which only represents the metadata size of the directory file itself, not the actual space occupied by all contents within the directory.

Core Functionality of du Command

The du (Disk Usage) command is specifically designed to calculate disk usage of files and directories. Unlike ls, du can recursively calculate the actual space occupied by all files and subdirectories within a directory, providing accurate disk usage data.

Basic Usage and Parameter Details

The basic command to get the total size of all files and directories in the current directory is:

du -sh *

Where:

Complete Command Format Analysis

The complete form of the above short command is:

du --summarize --human-readable *

Although this full format requires more typing, it offers better readability for script writing and documentation purposes.

Practical Extended Commands

In practical usage, other parameters can be combined based on different requirements:

du -sk * | sort -n

This command uses the -k parameter to display sizes in KiB units and pipes the results to sort -n for numerical sorting, making it easier to identify directories consuming the most space.

du -sh * | sort -h

When using human-readable format, it should be combined with sort -h command, which can correctly recognize and sort size values with unit suffixes.

Application Scenarios and Best Practices

In scenarios such as disk space cleanup, system maintenance, and capacity planning, the du command provides accurate space usage information. It's recommended to regularly use these commands to monitor size changes of critical directories and promptly identify files or directories with abnormal growth.

Conclusion and Recommendations

While the ls command is highly practical for daily file browsing, the du command is indispensable when accurate information about directory space usage is needed. Mastering du -sh * and its variant commands can significantly improve efficiency in system management and problem troubleshooting.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.