Th Most Handy du (Disk Usage) Commands in Linux

Hand Holding Variation Of Object

du is a command line tool shipped with Linux that reports the amount of disk space used by directories and files. Short for “disk usage,” du is the primary tool for analyzing disk space in the command line.

Basic Usage

Run the du command with a directory to see a complete analysis of the disk space taken up by each directory. Each directory will be listed in turn in standard output, so large directories or full system scans may produce tens of thousands of lines and take considerable time. As a result, the basic du command is generally supplemented with the flags indicated below. Any of these flags can be combined with any other, though some combinations preclude each other because they are contradictory.

Controlling Output

Best Du Commands Linux Terminal Summary

Shows a line at the bottom of the du output to indicate the total amount of disk space used by the directories that are scanned.

Shows only the summary of the total disk space used by the specified directory. No output will be shown until the summary is calculated.

Command line pros will recognize this command, but not everyone is a pro. This will output the results of the command to a text file in the present working directory named “du-report.txt.”

Pipes the display to the less text editor, creates a more easily readable result that can be skimmed with the less navigation shortcuts.

Changing Size Display

Best Du Commands Linux Terminal Human Readable

Note that file sizes will generally be rounded when specific units are declared. For example, a 4KB file will be shown as taking up zero megabytes if the -m flag is called, while a 750KB file will be shown as 1MB.

Display in “human readable” format with appropriate sizes listed, such as kilobytes, megabytes, and gigabytes, rather than the standard block size.

Display block counts in 1024-byte (1 kilobyte) blocks.

Display block counts in 1,048,576-byte (1 megabyte) blocks.

Display block counts in 1,073,741,824-byte (1 gigabyte) blocks.

Including Files and Links

Best Du Commands Linux Terminal All Files

Follow (or “dereference”) symbolic links in the command line and file hierarchies. Otherwise, the space taken up by the symbolic link itself will be reported (typically the file system minimum) rather than the directory tree that the symbolic link points to.

Show disk usage for all files, not just directories.

Display the disk usage of one specific file, as named in the command.

Excluding Files, Directories, and Links

Best Du Commands Linux Terminal Depth

Exclude files that match any pattern in the specified string.

Exclude files and subdirectories as specified by the pattern. In this example all directories including the string “*.o” would be skipped. Note that these are shell patterns, not regular expressions. As such, control characters are limited to *, which matches any string of zero or more characters, and ?, which matches any one character. This will exclude these files from calculations of directory size. If the -a flag is used, excluded files will be skipped in the resulting output.

Exclude entries smaller than SIZE if positive, or entries greater than SIZE if negative. SIZE is an integer and takes an (optional) unit. For example, --threshold=1MB would skip all files smaller than 1 megabyte (1000^2 bytes). Units include K, M, G, T, P, E, Z, Y for powers of 1024 (kibibyte, mebibyte, gibibyte, and so on) or KB, MB, GB, TB, … for the more standardized powers of 1000 (kilobyte, megabyte, gigabyte, and so on). This can be a useful tool for finding the largest files on your system with a command like du --threshold=1GB.

Set the maximum depth to N folders. This flag can take any positive integer. With this setting, du will scan for up to two subdirectories in the specified directory. If further subdirectories exist, they will not be scanned individually. Rather, their value will be included in the reported folders. Note that -d 0 will report the same results as the -s flag.

For example, consider the directory path “dir1/dir2/dir3/dir4,” which contains one parent directory and three subdirectories. With a du setting of du -d 2 dir1 would scan down to dir3. dir3’s size will include the files in dir4, even though dir4 is not listed separately.

Conclusion: Piping du

The du command is most useful when paired with other utilities, like the command  du -a / | sort -n -r | head -n 10. This will search your entire filesystem (du -a /), sort the results by size (sort -n -r), and then show only the top ten results (head -n 10). It’s essentially a shortcut for the top ten largest files on your machine. Combine du with other commands through pipes to produce even more useful results.

One comment

  1. This is better du | sort | head

    if [ -z $1 ]; then
    du -s .??* * | sort -n |\
    gawk ‘{hum[1000**3]=”TB”;hum[1000**2]=”GB”;hum[1000]=”MB”;hum[1]=”KB”;
    for (x=1000**3; x>=1; x/=1000){
    if ($1>=x) { printf “%6.2f %s \t%s \n”,$1/x,hum[x],$2; break }
    }
    }’

    else
    du -s $@ | sort -n |\
    gawk ‘{hum[1000**3]=”TB”;hum[1000**2]=”GB”;hum[1000]=”MB”;hum[1]=”KB”;
    for (x=1000**3; x>=1; x/=1000){
    if ($1>=x) { printf “%6.2f %s \t%s \n”,$1/x,hum[x],$2; break }
    }
    }’
    fi

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.