Skip to Content

Linux du command

  1. Basic Usage:
    • Syntax: du [options] [directory/file]
    • Basic Examples:
      • du /path/to/file: Displays disk usage of the specified file.
      • du /path/to/directory: Shows disk usage of all the files and directories under the specified directory and its subdirectories
  2. Common Options:
    • h or -human-readable: Prints sizes in human-readable format (e.g., 1K, 234M, 2G).
    • s or -summarize: Displays only the total size of each specified directory.
    • c or -total: Provides a grand total of the disk usage at the end of the output.
    • a or -all: Shows the disk usage of all files and directories, including hidden ones.
    • x or -one-file-system: Limits the command to a single file system and does not cross mount points.
    • -max-depth=N: Limits the depth of directory traversal to the specified level (e.g., -max-depth=1).
  3. Show Human-Readable Sizes and Sort by Size:
    • Command: du -h | sort -h
    • Description: Shows disk usage in human-readable format and sorts the output by size, with the smallest sizes listed first.
  4. Display Total Disk Usage of a Directory:
    • Command: du -sh /path/to/directory
    • Description: Provides a summary of the total disk usage of the specified directory, showing the result in a human-readable format.
  5. Find the Largest Files in a Directory:
    • Command: du -ah /path/to/directory | sort -rh | head -n 5
    • Description: Lists the five largest files in the specified directory and subdirectory, sorted in descending order by size, and displayed in a human-readable format.
  6. Exclude Specific Directories from Disk Usage Calculation:
    • Command: du -sh –exclude=/path/to/exclude/dir /path/to/directory
    • Description: Calculates the disk usage of the specified directory, excluding the specified directory from the calculation. The output is displayed in a human-readable format.