Keywords: Linux command line | du command | directory size统计
Abstract: This article provides a comprehensive exploration of advanced usage of the du command in Linux systems, focusing on concise and efficient methods to display the total size of each subdirectory. By comparing implementations across different coreutils versions, it details the workings and advantages of the `du -cksh *` command, supplemented by alternatives like `du -h -d 1`. Key technical aspects such as parameter combinations, wildcard processing, and human-readable output are systematically explained. Through code examples and performance comparisons, the paper offers practical optimization strategies for system administrators and developers within a rigorous analytical framework.
Introduction and Problem Context
In Linux system administration and daily development, accurately assessing disk usage of directories and their subdirectories is a fundamental yet critical task. The du (disk usage) command, as a standard tool for disk space statistics, offers flexible parameter combinations to meet various needs. However, users often encounter a specific challenge: how to display only the total size of each immediate subdirectory in the most concise command form, without recursively listing all detailed contents? This requirement is particularly useful for quick directory structure evaluation, disk space cleanup, or storage reporting.
Core Solution: Deep Dive into the du -cksh * Command
Based on practical verification in coreutils 5.97 and later versions, the du -cksh * command provides an elegant and efficient solution. We will analyze this from three dimensions: parameter breakdown, execution mechanism, and applicable scenarios.
First, the command consists of four key parameters: -c, -k, -s, and -h. The -c (--total) parameter adds a total line at the end of the output, which is especially useful for scenarios requiring aggregation of multiple directory sizes. The -k parameter specifies display in KB (kilobytes); although when used with -h, the output is converted to human-readable format, retaining -k ensures consistent calculation baselines. The -s (--summarize) parameter is the core, instructing du to display only the total size for each specified directory without recursively listing internal items—this is key to achieving the goal of "showing only total per directory." The -h (--human-readable) parameter converts numeric sizes to more readable units (e.g., K, M, G), enhancing output clarity.
The wildcard * in the command plays a crucial role. In shell environments, * expands to a list of all non-hidden files and directories in the current directory. When combined with -s, du calculates the total size for each expanded directory separately. For example, if the current directory contains dir1, dir2, and file.txt, * expands to dir1 dir2 file.txt, and du outputs summarized sizes for dir1 and dir2 (as directories), while for file.txt (a file), the -s parameter has no additional effect, directly showing the file size. This design allows automatic filtering and processing of all subdirectories without manual enumeration.
To illustrate more intuitively, consider the following example code and output. Assume the current directory structure is:
$ ls -la
total 16
drwxr-xr-x 4 user group 4096 Jan 10 10:00 .
drwxr-xr-x 5 user group 4096 Jan 10 09:00 ..
drwxr-xr-x 2 user group 4096 Jan 10 10:00 documents
drwxr-xr-x 3 user group 4096 Jan 10 10:00 projects
-rw-r--r-- 1 user group 1024 Jan 10 10:00 README.md
Executing the du -cksh * command:
$ du -cksh *
2.5M documents
4.0M projects
1.0K README.md
6.5M total
The output shows the documents directory totals 2.5MB, projects totals 4.0MB, the file README.md is 1KB, and a total of 6.5MB is given in the last line. This result clearly displays the summarized size of each subdirectory, meeting user needs. From a performance perspective, since the -s parameter avoids deep recursive traversal, this command is more efficient than full recursion when handling large directory trees, reducing I/O overhead and computation time.
Supplementary Methods and Version Compatibility Analysis
While du -cksh * performs excellently in most scenarios, different coreutils versions may offer alternatives. For instance, in coreutils 8.14, the du -h -d 1 command can achieve similar effects. Here, the -d 1 parameter (or equivalent --max-depth=1) limits recursion depth to 1, displaying sizes only for immediate subdirectories and files of the current directory. Compared to -s, -d 1 lists each subitem rather than summarizing directories only, so the output may be more detailed but still serves the purpose of "showing subdirectory sizes." For example:
$ du -h -d 1
2.5M ./documents
4.0M ./projects
1.0K ./README.md
6.5M .
This output includes sizes for each subdirectory and file, plus the total for the current directory. The choice between methods depends on specific needs: if only directory summaries are required, -s is more concise; if file details are needed, -d 1 is more comprehensive. In practice, selection should be based on system version and output format preferences. Note that some older versions may not support the -d parameter, making du -cksh * more compatible.
Technical Summary and Best Practices Recommendations
Based on the above analysis, several key technical points can be distilled. First, understanding parameter combination logic is essential: -s enables summarization, -h enhances readability, -c adds totals, and the wildcard * automates subdirectory listing. Second, note the command's limitations: * does not include hidden files or directories (starting with a dot); if these need to be counted, use expansions like du -cksh .[!.]* *, though this may add complexity. Additionally, when dealing with directory names containing spaces or special characters, ensure proper shell quoting or use find with xargs to avoid parsing errors.
From a practical standpoint, we recommend the following best practices: in most modern Linux distributions, prioritize du -cksh * as the default command due to its simplicity and broad compatibility; for scenarios requiring depth control, try du -h -d N (where N is the desired depth); in scripts, consider adding error handling, such as checking directory existence or using set -e to ensure exit on failure. Performance-wise, if directories contain many small files, the -s parameter can significantly reduce统计 time but may sacrifice detail.
In conclusion, by deeply mastering the parameter semantics of the du command and shell expansion mechanisms, users can efficiently solve directory size统计 problems, enhancing command-line productivity. The methods discussed in this article are applicable not only for personal use but also for integration into automated monitoring or cleanup scripts, providing reliable support for system management.