sort Sort lines of text
Reads lines and writes them back in order. By default it sorts as text, like a dictionary. Use it to tidy a list, to order a table by one column, or to prepare input for uniq.
Synopsis
sort [OPTION]... [FILE]...
sort -k START[,END] [-t SEP] [FILE]...
Common options
| Option or form | What it does |
|---|---|
-n | Compare as numbers, so 9 comes before 10. The default text order puts 10 before 9 |
-r | Reverse the order |
-u | Output only the first of each run of equal lines, removing duplicates |
-k START[,END] | Sort on a field range instead of the whole line. Fields count from 1. -k2,2 means field 2 only |
-t SEP | Use SEP as the field separator for -k, for example -t, for CSV. The default separator is blank space |
-f | Ignore case when comparing |
-h | Compare human-readable sizes such as 2K, 1M and 3G, as printed by du -h |
-o FILE | Write the result to FILE. Safe even when FILE is also the input |
Examples
sort words.txt
apple
apple
apple
fig
pear
pear
sort -u words.txt
apple
fig
pear
printf '10\n9\n100\n' | sort -nr
100
10
9
sort -t, -k3,3nr -k1,1 data.csv
sort -o words.txt words.txt
du -sh * | sort -rh | head
Notes
- Default order is text, not numeric. Without -n, 100 sorts before 9. This is the most common mistake.
- In the CSV example the header row is sorted along with the data. Its score value is not a number, so it counts as zero and lands last. Strip the header first with tail -n +2 if that matters.
- Do not write sort file > file. The shell empties the file before sort reads it. Use sort -o file file instead.
- Sort order depends on your locale. In some locales upper and lower case are mixed together. Set LC_ALL=C for plain byte order when you need predictable results in scripts.
- -k2 sorts from field 2 to the end of the line. Write -k2,2 to sort on field 2 alone.
See also
Last reviewed 2026-10-07.