Sort lines of text — alphabetically, numerically, or by a specific field.
low risk
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.
| Flag | Meaning |
|---|---|
-n --numeric-sort | Sort by numeric value, not lexicographic order |
-r --reverse | Reverse the sort order (descending) |
-k --key | Sort by a specific field/column (e.g. -k2 for 2nd field) |
-t --field-separator | Set the field delimiter (default: whitespace) |
-u --unique | Output only unique lines, removing duplicates |
-h --human-numeric-sort | Sort human-readable numbers like 1K, 2M, 3G |
-o --output | Write result to a file (safe even if same as input) |
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 →Use `sort -u` instead of `sort | uniq` — it deduplicates during the sort pass itself, which is faster on large inputs.
Reference text is hand-written and may be incomplete — check man sort for your version. Test destructive commands on a non-production system first.