sort

Sort lines of text — alphabetically, numerically, or by a specific field.

low risk

interactive breakdown — edit the command and press Explain
$ 
What it does

sort reads lines from files or stdin and outputs them sorted. -n sorts numerically, -r reverses, -k selects a key column, -u deduplicates. Combine with uniq and awk for powerful log analysis and data processing pipelines.

Common flags
FlagMeaning
-n --numeric-sortSort by numeric value, not lexicographic order
-r --reverseReverse the sort order (descending)
-k --keySort by a specific field/column (e.g. -k2 for 2nd field)
-t --field-separatorSet the field delimiter (default: whitespace)
-u --uniqueOutput only unique lines, removing duplicates
-h --human-numeric-sortSort human-readable numbers like 1K, 2M, 3G
-o --outputWrite result to a file (safe even if same as input)
Examples
sort -rn -k5 access.log | head -20Top 20 lines by 5th column descending — e.g. largest request sizes · explain & risk-check →
du -sh /var/log/* | sort -rhList log directories sorted largest-first (human-readable sizes) · explain & risk-check →
sort -t: -k3 -n /etc/passwdSort /etc/passwd by UID (3rd colon-delimited field) numerically · explain & risk-check →
Pro tip

Use `sort -u` instead of `sort | uniq` — it deduplicates during the sort pass itself, which is faster on large inputs.

Related
uniq awk grep cut head
Open the full explainer →More in Text Processing

Reference text is hand-written and may be incomplete — check man sort for your version. Test destructive commands on a non-production system first.