The du (disk usage) command helps you find out exactly how much space files and directories consume on your Linux system. Whether you need to track down what’s filling up your disk, measure directory sizes before archiving, or identify which subdirectories are using the most storage, du provides the answers with flexible output options. By the end of this guide, you will be able to display disk usage in human-readable formats, exclude specific file types from calculations, control traversal depth, and view last-modified timestamps alongside size information.
Displaying Disk Usage Summary with the du Command
Using the -s Option for Summary
The -s option with the du command provides a concise summary of disk usage. Specifically, it aggregates the total size of a specified directory, excluding the individual sizes of files and subdirectories within it.
Example: Displaying the Current Directory Size
To view the total size of the current directory in kilobytes, execute:
du -s
This command returns the total size of the current directory, measured in kilobytes.
Implementing the -h Option for Readability
The -h option enhances the du command’s output by presenting sizes in a human-readable format. Instead of raw kilobyte counts, it uses K, M, or G suffixes based on the size.
Example: Human-Readable Format for Current Directory
To display the size of the current directory in a user-friendly format, use:
du -sh
This command shows the total size of the current directory in an easily interpretable format, for example:
628K .
Specifying a Different Directory Path
Additionally, the du command allows users to specify a different directory path to check its size.
Example: Checking Size of a Specific Directory
To find the size of a directory located at /path/to/directory in a human-readable format, use:
du -sh /path/to/directory
This command displays the total size of the directory at the specified path, formatted for readability.
Displaying Total Disk Usage with the du Command
Utilizing the -c and -h Flags for Comprehensive Output
To obtain a comprehensive view of disk usage, the -c and -h flags are used together. Specifically, the -c flag adds a grand total line at the end, while -h formats sizes with K, M, or G suffixes.
Example: Total Size of Current Directory
To check the total size of the current directory, including all its files and subdirectories, execute:
du -ch
The output lists each subdirectory size followed by a grand total at the bottom:
16K ./subdir2 504K ./subdir1 628K . 628K total
Checking Disk Usage of a Specific Directory
Furthermore, the du command’s versatility allows for specifying different directory paths to assess their sizes.
Example: Total Size of a Specified Directory
To determine the total size of a directory located at /path/to/directory, use:
du -ch /path/to/directory
This command provides the total size of the specified directory and its contents in a user-friendly format.
Displaying Disk Information in Kilobytes and Megabytes Using the du Command
Using the -k Option for Kilobyte Display
To view the sizes of files and directories in kilobytes, the -k option is utilized. In particular, this setting is helpful for detailed analysis of disk space usage.
Example: Display Sizes in Kilobytes
To see the sizes of files and directories in the current directory in kilobytes, execute:
du -k
This command lists the sizes of all subdirectories in the current directory, measured in kilobytes.
Applying the -m Option for Megabyte Display
For a broader view, particularly in larger directories, the -m option allows sizes to be displayed in megabytes instead.
Example: Display Sizes in Megabytes
To view the sizes in megabytes, use:
du -m
This command outputs the sizes of subdirectories in the current directory in megabytes.
Simplifying Output with the -s Option
Alternatively, the -s option simplifies the du command’s output by showing only the total size of a directory, rather than the sizes of each individual file and subdirectory.
Example: Total Directory Size in Kilobytes
To display the total size of the current directory in kilobytes, run:
du -sk
This command provides the aggregate size of the current directory in kilobytes.
Example: Total Size of a Specific Directory in Megabytes
To check the total size of a different directory in megabytes, execute:
du -sm /path/to/directory
This command returns the total size of the specified directory in megabytes.
Human-Readable Output and Depth Control
Using the -h Option for Auto-Scaled Units
The -h option with du automatically scales the output to the most appropriate unit (K, M, G, or T) based on size. As a result, this simplifies disk usage analysis, especially for directories with varying file sizes.
Example: Human-Readable Sizes for Current Directory
To view file and directory sizes in the current directory with auto-scaled units, execute:
du -h
This command displays sizes with automatic unit scaling based on the actual values.
Specifying a Different Directory Path
Similarly, the du command allows specifying a path to a different directory for size analysis.
Example: Human-Readable Sizes for Specified Directory
To check sizes for a directory at /path/to/directory with human-readable units, use:
du -h /path/to/directory
This command lists the sizes of files and directories in the specified path with appropriate unit suffixes.
Using the -d Option for Depth Control
Moreover, the -d option in du provides control over the depth of the directory tree displayed, offering a focused view of disk usage.
Example: Displaying Top-Level Directory Sizes
To list sizes of only the top-level directories within a file system, run:
du -hd1 /path/to/directory
The output shows only immediate subdirectories without recursing into nested folders:
16K /path/to/directory/subdir2 504K /path/to/directory/subdir1 628K /path/to/directory
Displaying Last Modified Date with the du Command
In addition to size information, the du command can display the last modified date of files, making it a valuable tool for file management and system monitoring.
Using the –time Flag
The --time flag with du reveals the last modified time for each file, alongside its size. Consequently, this feature is particularly useful for tracking recent changes in files and directories.
Example: Displaying Last Modified Time for All Files
To show the last modified time for each file, use:
du --time *
This command lists the size and last modified date for every file in the current directory.
Combining with the -h Flag for Readability
Furthermore, integrating the -h (human-readable) flag enhances the output, making it more user-friendly by using size abbreviations like “M” for megabytes.
Example: Human-Readable File Sizes and Dates
For a more readable format, execute:
du -h --time *
This command displays each file’s size and last modified time in the current directory in an easily understandable format.
Sample Output
4.0K 2022-01-07 13:14 file1.txt 8.0K 2022-01-07 13:14 file2.txt 16M 2022-01-07 13:14 file3.txt
Counting Hard Link Sizes with the -l Flag
By default, du counts hard-linked files only once, even when multiple hard links point to the same data. However, the -l (lowercase L) flag changes this behavior by counting the size for each hard link separately. This is particularly useful when analyzing backup directories or deduplication scenarios where hard links are common.
Example: Measuring Hard Link Impact
When your directory contains hard links, the following command shows sizes with each link counted:
du -lh
Without -l, a 100K file with two hard links appears as 100K total. With -l, that same file appears as 200K because each link is counted separately.
Excluding Specific File Formats with the du Command
Using the –exclude Flag
The --exclude flag allows users to specify a pattern that matches the file formats to be omitted from the du command’s output. As a result, this approach is highly effective for customizing disk usage reports.
Example: Excluding .tmp Files
To exclude files with the .tmp extension, execute:
du --exclude=*.tmp /path/to/directory
This command assesses the sizes of files and directories at /path/to/directory, excluding any files ending in .tmp.
Using Wildcard Patterns for Multiple Exclusions
Additionally, the --exclude pattern supports shell glob syntax, allowing wildcards like * for any characters and ? for a single character.
Example: Excluding Multiple File Formats
To exclude files with both .tmp and .log extensions, use:
du --exclude=*.tmp --exclude=*.log /path/to/directory
This command provides the disk usage of /path/to/directory, omitting files ending in .tmp and .log.
Sorting Files by Disk Usage
Combining du with sort for Ranked Output
Piping du output to sort arranges files and directories by size, making it easier to identify what consumes the most space. Meanwhile, the -a flag includes individual files in the output, while sort -n performs a numeric sort.
Example: Sort Files by Size (Smallest to Largest)
To list all files and directories in the current directory sorted by disk usage from smallest to largest:
du -ah | sort -h
The -h flag on both commands ensures human-readable sizes (K, M, G) sort correctly. Otherwise, numeric sorting would place “9K” after “10M” because it only compares the leading digit.
Example: Find the Largest Files and Directories
To quickly identify the top 10 largest items, reverse the sort order and pipe to head (the opposite of the tail command):
du -ah /path/to/directory | sort -rh | head -10
628K /path/to/directory 504K /path/to/directory/subdir1 100K /path/to/directory/file2.bin 16K /path/to/directory/subdir2 12K /path/to/directory/subdir2/small.txt 4.0K /path/to/directory/file1.txt 0 /path/to/directory/random.tmp 0 /path/to/directory/test.log
As a result, this approach reveals exactly which files or subdirectories are consuming the most space, helping you prioritize cleanup efforts.
Staying on a Single Filesystem with -x
When scanning directories that contain mount points, the -x flag prevents du from crossing into other filesystems. In particular, this is useful when measuring disk usage on the root partition without including mounted drives, network shares, or virtual filesystems.
Example: Measure Root Partition Only
To measure disk usage starting from root while staying on the root filesystem:
sudo du -shx /
Without -x, this command would include /home, /boot, or any other mounted partitions. However, with -x, only the root filesystem is measured.
Conclusion
In summary, you now have the core techniques for analyzing disk usage with du: summarizing directories, displaying sizes in various units, excluding file patterns, showing timestamps, and sorting output to find space hogs. Together, these commands form the foundation for monitoring file system growth and identifying cleanup targets on any Linux system.