Sorting Subdirectories by Size

Sorting Subdirectories by Size

Sorting Subdirectories by Size
The flood we just experienced in Chiang Mai Thailand

When managing directories, especially those with many subdirectories, it can be helpful to determine the size of each subdirectory and sort them by size. This way, you can quickly identify which directories are consuming the most space and address potential issues.

The following command will list the sizes of subdirectories and sort them in descending order:

du -h -d 1 2>/dev/null | sort -hr

Command Breakdown

  • du -h -d 1:
    • -h makes the sizes human-readable (KB, MB, GB, etc.).
    • -d 1 limits the depth to 1, so it only lists directories in the current folder.
  • 2>/dev/null: Redirects any error messages (e.g., permission issues) to /dev/null, so they won’t clutter the output.
  • sort -hr:
    • -h ensures sorting is done based on human-readable sizes.
    • -r displays the sizes in reverse order, showing the largest directories first.