sort — sort lines
The sort command orders lines of a file or command output: as text, as numbers (-n), by field (-k), reversed (-r).
Cheat sheet
8 commands Printable version-
sort file.txtalphabetical order -
sort -n nums.txtas numbers: 2 9 10 100 -
sort -rn nums.txtnumbers, largest first -
sort -k2,2 table.txtby the second field -
sort -t: -k3,3n /etc/passwd":" separator, field 3 -
sort -u names.txtsort and drop duplicates -
du -sh /var/log/* | sort -hsizes 4K 512M 1G -
sort | uniq -c | sort -rnline frequency, most common first
Overview
sort reads every line, puts them in order and prints them. The input file stays
as it was. With no options, lines are compared as text, character by character, so
numbers without -n line up "by the first digit":
A field is a piece of the line between separators. By default fields are split by
spaces and tabs; -t sets another separator. -k2,2 means "compare from field 2
through field 2". Plain -k2 compares from the second field to the end of the line.
In a pipeline, sort usually comes right before uniq: uniq only removes
adjacent repeats, and sorting is what puts equal lines next to each other.
Options
11Order letters can be attached to a key: -k3,3n is field 3 as a number, -k2,2r
is field 2 in reverse. The full list is in man sort.
| Option | What it does | Example |
|---|---|---|
-n --numeric-sort |
Compare as numbers, not as text. | sort -n nums.txt |
-r --reverse |
Reverse the order — largest first. | sort -rn nums.txt |
-k --key N[,M] |
Compare by fields N through M; without M, from N to the end of the line. | sort -k2,2 table.txt |
-t --field-separator C |
Field separator instead of blanks, such as ":" or ",". | sort -t: -k3,3n /etc/passwd |
-u --unique |
Keep one line out of each group of equal lines. | sort -u names.txt |
-h --human-numeric-sort |
Compare sizes with K, M, G suffixes. | du -sh * | sort -h |
-o --output FILE |
Write the result to a file, which may be the input file itself. | sort -o names.txt names.txt |
-f --ignore-case |
Fold lowercase to uppercase — ignore case. | sort -f names.txt |
-b --ignore-leading-blanks |
Ignore leading blanks in a field. | sort -b -k2,2 table.txt |
-V --version-sort |
Sort version numbers: 1.2 < 1.9 < 1.10. | sort -V versions.txt |
-c --check |
Check whether input is sorted; prints the first out-of-order line. | sort -c list.txt |
Nothing found. Try a shorter query.
Recipes
7A table by a numeric field
Sizes with K, M, G
-n reads 512M as 512 and 1G as 1, which gives the wrong order. -h knows the
suffixes.
Several keys
Versions and numbered file names
Write the result back to the same file
sort names.txt > names.txt leaves an empty file; -o writes only after reading.
Check whether a file is sorted
Troubleshooting
710 comes before 9
The comparison is textual. Use -n for numbers and -h for sizes with suffixes.
sort -k2 does not sort by the second field only
-k2 means "from field 2 to the end of the line", so ties are decided by the
following fields. Limit the key: -k2,2.
Upper and lower case are not in the order you expected
The locale decides the text order. In the C locale all capitals come before
lowercase letters; in en_US.UTF-8 case barely matters. For the same result on every
machine use LC_ALL=C sort. To ignore case, use -f.
sort: cannot read: x: No such file or directory
There is no file at that path. Check the name with ls and the current
directory with pwd.
sort: invalid number at field start
-k needs a field number: -k2, -k2,2n. Letters without a number (-k n) are
rejected. Fields are counted from one: -k0 fails with field number is zero.
Numbers with leading spaces sort oddly
Without -n or -b, leading blanks take part in the comparison. Add -b or
compare numerically with -n.
After sort file > file the file is empty
The shell truncated the file for writing before sort read it. Use
sort -o file file — see redirection.
See also
- uniq — count and remove repeats after sorting.
- cut — cut out a field before sorting.
- wc — count the lines of the result.
- head — keep the top of a sorted list.
- redirection — the
|pipeline and writing results to a file. - text-tools — tr, paste, column, comm and other pipeline links.
Found an inaccuracy? Report an error in the article