text

sort

Sorts lines of text. Supports numeric, human-numeric, reverse, unique, and keyed sorting.

sort [OPTION]... [FILE]...

Common flags

Flag Purpose
-n Compare according to numeric value
-h Compare human-readable sizes (2K, 1G)
-r Reverse the sort order
-u Output only unique lines
-k Sort by the specified key/column
-t Use CHAR as the field separator
-o Write output to FILE (safe with the same input file)

Examples

sort names.txt

Standard lexicographic sort

sort -nr scores.txt

Sort numerically, highest first

sort -t: -k3n /etc/passwd

Sort passwd by UID column

du -h * | sort -h

Sort disk usage by human-readable size

Gotcha

Default sort is locale-aware -- set LC_ALL=C for byte-wise sorting when you need deterministic results across systems.

Related commands

← All Linux commands · Linux cheat sheet