Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux sort command

Reference for the Linux sort command: alphabetical and numeric order, sorting by field, reverse, unique, human sizes, stable sorts and the locale trap.

sort reads lines of text, orders them, and writes them back out. By default it compares whole lines as text according to the current locale. With options you can compare numbers, sort by a particular column, ignore case, remove duplicates, or understand version numbers and file sizes.

It is the workhorse behind many pipelines: uniq needs sorted input, comm and join need it too, and "top ten" reports are a sort | head away. For big inputs, sort spills to temporary files and merges, so it can handle files larger than memory.

Terminal
sort [OPTION]... [FILE]...

Try it

Bash Text, numbers and reverse

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Fields, separators and unique

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
sort [-bdfghiMnrRuVz] [-t SEP] [-k KEYDEF]... [-o FILE] [FILE]...
sort -c|-C [OPTION]... [FILE]
sort -m [OPTION]... FILE...
Option Description
-n Compare by leading numeric value (integers and decimals, not exponents).
-g General numeric comparison; understands 1e3, NaN and inf. Slower than -n.
-h Human-readable numbers such as 2K, 1G.
-V Natural version sort (1.2 before 1.10).
-M Month names (JAN before FEB).
-r Reverse the result.
-f Fold lower case to upper case when comparing.
-d Dictionary order: consider only blanks and alphanumerics.
-b Ignore leading blanks in the key.
-u Output only the first of each run of equal lines (equal as defined by the keys).
-s Stable: disable the last-resort whole-line comparison.
-t SEP Use SEP as the field separator instead of the blank-to-nonblank transition.
-k START[,END] Sort by a key from field START to field END; modifier letters may follow each number, as in -k2,2nr.
-o FILE Write to FILE; safe even if FILE is also an input.
-c, -C Only check whether the input is sorted; -c complains, -C stays silent.
-m Merge files that are already sorted.
-R Random order (groups equal keys together).
-z Lines end in NUL instead of newline.
-T DIR, -S SIZE, --parallel=N Temp directory, memory buffer and thread count for big sorts.

Returns: 0 on success. With -c or -C, 1 if the input is not sorted. 2 for other errors.

Usage notes

  • The locale decides text order. In most UTF-8 locales sort ignores case and punctuation at first and the result differs between machines. Use LC_ALL=C sort for byte order that is the same everywhere, and for speed.
  • Numbers sort as text without -n: 10 comes before 9. This is the most common mistake.
  • Keys: -k2 means "from field 2 to the end of the line". You nearly always want -k2,2 to limit the key to one field. Modifiers after a number apply only to that key; placed before any -k they apply globally.
  • Fields are counted from 1. Without -t, fields are separated by runs of blanks and the blanks belong to the next field, so add -b when columns are aligned with spaces.
  • Ties are broken by the whole line, which makes the result deterministic but can surprise you. Add -s to keep input order for equal keys.
  • -u with keys keeps only the first line for each distinct key, so sort -u -k1,1 deduplicates by first column.
  • sort -o file file is the correct way to sort a file in place; sort file > file truncates the file before reading it.
  • Large data: raise -S (for example -S 50%) and point -T at a fast disk with space.
  • sort -R is not a shuffle of equal lines; use shuf for real shuffling.
  • Check before relying on order: sort -c file tells you whether a file is sorted for comm or join.