sort -h: sort human-readable sizes like 1.5K and 10M correctly
Sort du/df/ls -lh output numerically on Linux using GNU sort's -h flag.
Pipe size output into sort -h to compare K/M/G suffixes in true magnitude order (add -r for largest first).
01Command
Command
$ du -h | sort -h -r | head -2002What it does
Default sort is lexicographic, so 9M sorts above 10G — the classic broken pipeline. GNU sort's -h understands the same human-readable suffixes that du -h, df -h and ls -lh print (K, M, G, T with decimal points), and -n handles plain numbers with -k to select which field to sort on. Without -h you must first convert sizes to bytes, which is exactly the step this flag removes.
03Risk
!Warning: -h is a GNU extension, absent from BSD sort; also LC_ALL=C is needed for byte-exact sorting of mixed text.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- sort(1) — Arch Linux manualEvidence: -h, --human-numeric-sort compares human-readable numbers (e.g. 2K 1G); -n numeric; -r reverse; -t/-k field options
- sort(1P) — POSIX manual pageEvidence: POSIX documents -n numeric comparison and key selection via -k
Last verified: 2026-09-17 · Category: Files & Folders