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?
3 Answers
`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.
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.
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.
Thanks! I’m using Debian, where `du --sort=size` doesn’t seem to be available, but the `sort -h` pipeline works perfectly.

That explanation helps—using a pipe to connect small tools makes sense. The simpler command is exactly what I needed.