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
cut
Extracts sections from each input line by byte, character, or delimited field. Handy for pulling columns out of CSV or colon-delimited files.
awk
Pattern-action language for processing columnar text. Excellent for reshaping tabular data, aggregating fields, and quick reports.
wc
Counts lines, words, characters, and bytes in files or stdin. Frequently used at the end of pipelines to size results.
cat
Concatenates files and prints them to standard output. Also useful as a simple viewer or to feed content into pipelines.
grep
Searches text for lines matching a regular expression. Central to log analysis and pipeline filtering.