Frequently Asked Question
How do I sort numerically, by column, or stably?
Plain sort orders lines lexicographically, so the numbers 1, 2, 10, 20 come
out as 1, 10, 2, 20, which is rarely what anyone wants. sort -n sorts
numerically and gets that right. sort -h understands human-readable suffixes
like 4K, 2M, 1G, which is handy with du. sort -r reverses the order.
To sort by a particular column, use -k together with -t for the delimiter.
sort -t: -k3 -n /etc/passwd sorts the password file numerically by UID (the
third colon-separated field). Awkwardly, sort -k3 by default treats the rest
of the line from field 3 onward as the sort key; use -k3,3 to mean only
field 3. Finally, sort -s is a stable sort, equal keys preserve their
original order, which lets you sort by one key, then re-sort by another to get
a deterministic two-level ordering.