How to show the disk usage of each subdirectory in Linux?

2 min read 07-10-2024
How to show the disk usage of each subdirectory in Linux?


Unveiling Your Disk Space Hogs: How to See Subdirectory Usage in Linux

Ever wondered which subdirectory in your Linux system is consuming the most disk space? This is a common question, especially when you're facing storage limitations. While the df command provides overall disk usage, it doesn't delve into the individual directories. Fear not, this article will equip you with the tools to identify and conquer those space-hungry subdirectories.

The Quest for Subdirectory Disk Usage

Imagine you have a large directory, /home/user/documents, and you want to find out which subdirectories are using the most space. This is where the power of the du command comes in. Let's break down how to use it effectively.

Basic Usage:

du -sh /home/user/documents/*

Here's the breakdown:

  • du: The command for disk usage.
  • -s: Provides a summary, displaying the total size of each directory.
  • -h: Human-readable output, displaying sizes in units like KB, MB, GB, etc.
  • /home/user/documents/*: The path to the directory and the asterisk (*) signifies all files and subdirectories within it.

Example Output:

2.5G    /home/user/documents/pictures
1.2G    /home/user/documents/music
500M    /home/user/documents/downloads

This output reveals that the pictures subdirectory is the largest, followed by music and downloads.

Advanced Techniques: Deep Dive into Subdirectories

For a more detailed view, you can use the -d option to specify the desired depth of directory traversal.

Example:

du -shd 1 /home/user/documents/*

The -d 1 option tells du to include the sizes of subdirectories at a depth of 1 level. This means it will show the size of each immediate subdirectory within /home/user/documents.

Sorting for Easy Analysis:

To see the directories sorted by their size, pipe the du output to the sort command.

Example:

du -shd 1 /home/user/documents/* | sort -h

The -h flag with sort ensures human-readable sorting based on the size values.

Beyond Basic Usage:

  • -a: Include file sizes as well, providing a complete breakdown.
  • -x: Restrict traversal to the specified file system.
  • -k: Display sizes in kilobytes.
  • --max-depth: Specifies the maximum depth to explore subdirectories.

Tools for Visual Representation

For a more visual understanding of directory sizes, tools like ncdu or tree can be helpful.

ncdu:

sudo apt-get install ncdu
ncdu /home/user/documents

ncdu provides a graphical, interactive interface that lets you navigate through directories and visualize their sizes.

tree:

sudo apt-get install tree
tree -h /home/user/documents

tree generates a visual representation of the directory structure, showing the size of each subdirectory alongside its name.

Conclusion

Identifying space hogs in your Linux system is crucial for maintaining efficient storage management. The du command, in conjunction with its various options, offers a powerful way to analyze disk usage. Remember, tools like ncdu and tree can provide an even more intuitive understanding of your directory structure and its size distribution. Happy space optimizing!