du

file managementLinux/Unix
The du command is one of the most frequently used commands in Linux/Unix-like operating systems. du Estimate file space usage for files and directories

Quick Reference

Command Name:

du

Category:

file management

Platform:

Linux/Unix

Basic Usage:

du [options] [arguments]

Common Use Cases

    Syntax

    du [OPTION]... [FILE]...

    Options

    Option Description
    -0, --null End each output line with a null character
    -a, --all Write counts for all files, not just directories
    -B, --block-size=SIZE Scale sizes by SIZE before printing
    -c, --total Produce a grand total
    -d, --max-depth=N Print total for directories only N or fewer levels deep
    -h, --human-readable Print sizes in human readable format (e.g., 1K, 234M, 2G)
    -k Like --block-size=1K
    -m Like --block-size=1M
    -s, --summarize Display only a total for each argument
    --time Show time of last modification of any file in the directory
    --time=WORD Show time as WORD: atime, access, use, ctime, or status
    --exclude=PATTERN Exclude files that match PATTERN
    -X, --exclude-from=FILE Exclude files that match any pattern in FILE

    Examples

    How to Use These Examples

    The examples below show common ways to use the du command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    du file.txt
    Show disk usage for a specific file.
    du -h directory
    Display disk usage for a directory in human-readable format.
    du -sh *
    Show total size of each item in current directory. # Advanced Examples Advanced du -sh /var/log Show total size of a directory in human-readable format. du -cksh /var/log/* | sort -h List size of each item in /var/log, sort by size, and show grand total. du --time -h directory Show disk usage along with last modification time. du --exclude="*.log" -h directory Calculate usage excluding files that match a pattern. du -h --max-depth=2 /var Show usage with directory depth limited to 2 levels.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Du Command Overview: The du (disk usage) command estimates and displays the file space usage for files and directories. It's one of the most commonly used commands for analyzing disk usage on Linux and Unix systems. Key Features: - Shows disk space used by files and directories - Can display sizes in human-readable formats (KB, MB, GB) - Allows for recursive directory analysis - Supports exclusion patterns - Can limit directory traversal depth - Provides summarization options Understanding the Output: By default, du shows the size of each subdirectory and then the size of the specified directory itself. The size is reported in disk blocks (typically 1KB or 512-byte blocks, depending on the system). With the -h option, sizes are displayed in a human-readable format using unit suffixes. Common Use Cases: - Finding which directories are consuming the most space - Troubleshooting disk space issues - Monitoring space usage growth over time - Preparing backups or archives - Identifying large files or directories for cleanup - Checking quota usage Differences from 'df': While 'du' reports the usage of specific files and directories, the 'df' command reports the disk space usage at the filesystem level. 'df' shows available and used space on mounted filesystems, while 'du' shows the space used by specific files and directories. Performance Considerations: Running 'du' on very large directories or filesystems can be I/O-intensive and time-consuming. Use options like --max-depth to limit traversal when working with large directory structures.

    Related Commands

    These commands are frequently used alongside du or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the du command works in different scenarios.

    $ du
    View All Commands