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.

LinuxtextsortnumbersLow risk
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 -20

02What 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

RiskLow risk
!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 manual
    Arch Linux · technical · 2026-09-17
    Evidence: -h, --human-numeric-sort compares human-readable numbers (e.g. 2K 1G); -n numeric; -r reverse; -t/-k field options
  • sort(1P) — POSIX manual page
    man7.org · technical · 2026-09-17
    Evidence: POSIX documents -n numeric comparison and key selection via -k

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions