Where to start First steps track · 15 stages
Linux Labs

sort — sort lines

6 min Ubuntu 24.04 · GNU coreutils 9.4

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.txt alphabetical order
  • sort -n nums.txt as numbers: 2 9 10 100
  • sort -rn nums.txt numbers, largest first
  • sort -k2,2 table.txt by the second field
  • sort -t: -k3,3n /etc/passwd ":" separator, field 3
  • sort -u names.txt sort and drop duplicates
  • du -sh /var/log/* | sort -h sizes 4K 512M 1G
  • sort | uniq -c | sort -rn line 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":

text
student@ubuntu:~$ printf '10\n9\n100\n2\n' | sort
10
100
2
9
student@ubuntu:~$ printf '10\n9\n100\n2\n' | sort -n
2
9
10
100

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

11

Order 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

7

Which lines occur most often

bash
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head

The first sort groups equal addresses together, uniq -c counts them, the second sort -rn puts the largest counts on top, and head keeps ten.

A table by a numeric field

bash
sort -t: -k3,3n /etc/passwd | cut -d: -f1,3   # users by UID
ps -eo pid,rss,comm | sort -k2,2nr | head -5  # top 5 by memory

Sizes with K, M, G

bash
du -sh /var/log/* 2>/dev/null | sort -h       # smallest to largest
du -sh /var/log/* 2>/dev/null | sort -rh | head -3

-n reads 512M as 512 and 1G as 1, which gives the wrong order. -h knows the suffixes.

Several keys

bash
sort -k2,2 -k1,1n table.txt   # by field 2, ties by field 1 as a number

Versions and numbered file names

bash
printf 'app-1.10\napp-1.9\napp-1.2\n' | sort -V
# app-1.2  app-1.9  app-1.10 — without -V, 1.10 would precede 1.2

Write the result back to the same file

bash
sort -o names.txt names.txt

sort names.txt > names.txt leaves an empty file; -o writes only after reading.

Check whether a file is sorted

bash
sort -c list.txt        # sort: list.txt:2: disorder: a
sort -C list.txt; echo $?   # silent: 0 — sorted, 1 — not

Troubleshooting

7

10 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.
Loading…