How can I sort `du -h` output by folder size?

August 4, 2026 3 answers
Asked by MellowCedar42
I use `du -h --max-depth=1` in the command line to check the sizes of directories. Is there a way to sort the results from smallest to largest, or from largest to smallest?

Answers

BrightMango7
Pipe the output into `sort`, using its human-readable size option: `du -h --max-depth=1 | sort -h` sorts from smallest to largest, while `du -h --max-depth=1 | sort -hr` sorts from largest to smallest. The `-h` option makes `sort` understand values such as KiB, MiB, and GiB instead of treating them as ordinary text.

MellowCedar42: Thanks! Iโ€™m using Debian, where `du --sort=size` doesnโ€™t seem to be available, but the `sort -h` pipeline works perfectly.

Reply to this answer
SilverPebble29
On systems whose version of GNU `du` supports it, `du -h --max-depth=1 --sort=size` may sort by size directly. However, that option isnโ€™t available everywhere, including some Debian setups, so `du -h --max-depth=1 | sort -h` is the safer option.
Reply to this answer
QuietFalcon18
`du` itself generally doesnโ€™t sort the output, so combining it with the separate `sort` command is the portable approach. For example: `du --human-readable --max-depth=1 /some/path | sort -h`. Add `-r` to reverse the order.

MellowCedar42: That explanation helpsโ€”using a pipe to connect small tools makes sense. The simpler command is exactly what I needed.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions