Skip to content
Latchkey

sort -h: Sort Human-Readable Sizes

sort -h sorts suffixed sizes such as 4K, 1M, 2G by their real magnitude.

du -h prints sizes like 4.0K and 1.2G. A plain sort gets them wrong; -h understands the SI suffixes.

What it does

sort -h compares values that use SI suffixes (K, M, G, T and so on), ordering 900K before 1M before 2G. It is the right tool for output from du -h, df -h, or ls -lh where numbers carry a unit letter.

Common usage

Terminal
du -sh */ | sort -h          # smallest dir first
du -sh */ | sort -hr | head  # largest dirs
df -h | sort -h -k5,5         # by use%

Options

FlagWhat it does
-hCompare human-readable SI suffixes by magnitude
-rReverse: -hr for largest first
-k <pos>Apply -h to a specific field

In CI

Pair sort -h directly with du -sh */ for a quick disk-usage report on a runner. There is no need to strip units or convert to bytes first.

Common errors in CI

-h is a GNU coreutils extension. macOS BSD sort does not support it, so a script that runs on Linux runners fails on a macOS runner with "illegal option -- h"; install coreutils (gsort) or convert to plain bytes with du -b and sort -n. -h expects K/M/G, not KiB/MiB strings.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →