du

du

The du command in Unix-like operating systems is used to estimate file and directory space usage. It stands for "disk usage". Here’s an overview of du and its common usage:

Overview of du

Purpose: du is used to determine the disk usage of files and directories within a filesystem. It recursively traverses directories and reports back the total disk space used by each file and directory.

Availability: du is a standard command-line utility available on Unix-like systems, including Linux distributions, macOS, and BSD variants.

Common du Commands and Usage

  1. Display Disk Usage of Current Directory:

    • To display the disk usage of files and directories in the current directory:

      du

      By default, du recursively lists the disk usage of all files and directories starting from the current directory.

  2. Display Disk Usage of a Specific Directory:

    • To display the disk usage of a specific directory (e.g., /home/user/docs):

      du /home/user/docs

      Replace /home/user/docs with the path to the directory you want to analyze.

  3. Display Human-Readable Format:

    • To display disk usage in a more human-readable format (e.g., in kilobytes, megabytes, gigabytes):

      du -h

      The -h option (or --human-readable) converts sizes into a human-readable format.

  4. Display Total Disk Usage:

    • To display the total disk usage of a directory, including all its subdirectories:

      du -h --summarize /path/to/directory

      The --summarize option provides a summary at the end of the output, showing the total disk usage.

  5. Sort Output by Size:

    • To sort the output of du by size, showing the largest items first:

      du -h | sort -rh

      The -r option sorts in reverse order (largest to smallest), and -h provides human-readable sizes.

  6. Limit Depth of Recursive Search:

    • To limit the depth of recursive directory search (e.g., up to 2 levels):

      du -h --max-depth=2 /path/to/directory

      Adjust 2 to the desired depth level you want to analyze.

Considerations

  • Symbolic Links: By default, du does not follow symbolic links unless specified with the -L option.

  • Permissions: Ensure the user running du has appropriate permissions to access the directories and files being analyzed.

  • Performance Impact: Analyzing large filesystems with du can be resource-intensive, especially when used with options like -r for recursive scanning.

Alternatives

  • ncdu: A text-based disk usage analyzer that provides an interactive interface for exploring disk usage and navigating directories.

  • Disk Usage Analyzers: Graphical tools like Baobab (for GNOME) and Filelight (for KDE) offer visual representations of disk usage.

Conclusion

du is a versatile command-line tool for analyzing disk usage on Unix-like systems, providing insights into file and directory sizes within filesystems. It’s useful for managing disk space, identifying large files, and optimizing storage resources.

help

Last updated