sort Command Guide for Ordering Text
This guide helps you understand and use the sort command, the standard way to arrange lines of text alphabetically, numerically, or by any column.
What is sort, and Why Use It?
The sort command reads lines of text and writes them back in order — alphabetically by default, but you can order by number, by a specific column, or in reverse. It’s the backbone of nearly every “top N” pipeline on Linux.
Why Learn sort?
- Everything counts better sorted: The classic “10 biggest files” or “top 10 errors” pipelines are
sortplusuniqplushead. - Dedupe and group: Sorting is the required first step for
uniqto find duplicates. - Data cleanup: Log files, CSV exports, and config lists become readable once ordered.
- It’s everywhere: Every system has
sort, and scripts depend on it for stable output.
If
uniqis the accountant of Linux,sortis the bookkeeper — it has to do its job first.
1. Sort a File
sort names.txt
That prints the file’s lines in alphabetical order, without touching the original. To sort in place, use redirection or a temp file:
sort names.txt > sorted.txt
Apt-style lists sorted for readability:
sort /etc/passwd
2. Reverse and Numeric Sorting
sort -r names.txt
-rreverses the order — Z to A, 9 to 1.
Text sorts 10 before 9 because it compares character by character. For real numeric order:
sort -n numbers.txt
-ncompares values as numbers, so10comes after9.
Combine both for a “biggest first” list:
sort -rn numbers.txt
3. Sort by a Specific Column
Say a file has names and sizes separated by whitespace:
sort -k2 /etc/passwd
-k2sorts by the second field (column) instead of the whole line.-k2,2sorts by field 2 only, not extending to the end of the line.
The classic pipeline — biggest files first:
du -sh * | sort -rh
-hunderstands human-readable sizes (1K,234M,1.2G), so this sorts them by actual size.
4. The Uniq-and-Sort Combo
sort is almost never used alone. The three-command pipeline that answers “what are the most common things?” is:
sort data.txt | uniq -c | sort -rn | head
Read right to left: take the top 100 lines of output, count occurrences, sort by count descending. This counts repeated log lines, visited domains, or error messages.
Sorting also makes uniq’s duplicate detection possible at all:
sort names.txt | uniq
That prints each name once.
5. Unique, Check, and Random
sort -u names.txt
-uoutputs only the first of each run of equal lines — the same result assort | uniq, in one command.
Check whether a file is already sorted (scripts use this):
sort -c names.txt && echo "sorted!" || echo "not sorted"
-cverifies order without printing anything; it exits with success only if the file is sorted.
Random order:
sort -R playlist.txt
-Rshuffles lines — handy for randomizing lists in scripts.
6. Custom Sorting Rules
Sort with locale-specific rules (like ls does for filenames):
LC_ALL=C sort file.txt
- Plain
sortuses your locale, which can order accented characters surprisingly.LC_ALL=Cforces byte order — faster and predictable in scripts.
Sort with a custom separator (for CSV files):
sort -t ',' -k3 -n data.csv
-t ','says fields are separated by a comma instead of whitespace.
7. Combine with Pipes
cat access.log | sort | uniq -c | sort -rn | head -n 10
That finds the ten most-repeated lines in a log.
ps aux | sort -k4 -rn | head
That lists processes sorted by memory usage (column 4). See ps in the processes section for what those columns mean.
Common Pitfalls
- Numeric columns sort wrong without
-n.sorton1 2 10gives1 10 2. Add-nwhen the values are numbers. -k2vs-k2,2:-k2sorts from field 2 to the end of the line; ties extend the comparison. Use-k2,2when only that column should decide.sortchanges your file’s order permanently if you redirect over it. Always sort to a new file or usesort -o file file— and beware-owith a single input overwriting while reading.- Human sizes need
-h.sort -ntreats1.2Gas1.2. Use-hfor sizes fromduorls -h. - Blank lines and trailing whitespace affect ordering. Use
sort -bto ignore leading blanks when it matters.
Related Pages
| Page | Relationship |
|---|---|
uniq | Finds and counts duplicates — needs sort first. |
wc | Counts lines; often appears in the same pipelines. |
head.md | Shows the top N of a sorted list. |
pipes.md | How sort chains with uniq and head. |
sort Cheatsheet
| Command | What It Does |
|---|---|
sort file | Sort lines alphabetically. |
sort -r file | Sort in reverse. |
sort -n file | Sort numerically. |
sort -rn file | Sort numerically, biggest first. |
sort -k2 file | Sort by the second column. |
sort -k2,2 file | Sort by column 2 only. |
sort -t ',' file | Sort CSV using commas as separators. |
sort -u file | Sort and remove duplicates. |
sort -c file | Check if the file is sorted. |
sort -R file | Shuffle lines randomly. |
sort -h file | Sort human-readable sizes (K, M, G). |
sort file > out | Write sorted output to a new file. |