sort

sort reads lines and writes them out in order.

sort [options] [file...]
$ sort names.txt

With no file, sort reads standard input. Given several files, it sorts all of their lines together as one stream. By default it compares lines as plain text, character by character.

How lines are compared #

The default text comparison is often not the order you want — 10 sorts before 9, and case matters. These options change the comparison:

OptionCompares lines as…
-n, --numeric-sortnumbers. 9 sorts before 10.
-g, --general-numeric-sortnumbers, including scientific notation. Slower than -n; use it only when -n is not enough.
-h, --human-numeric-sorthuman-readable sizes — 2K before 1M before 3G.
-M, --month-sortmonth names — JAN before FEB.
-V, --version-sortversion numbers — 1.2.10 after 1.2.9.
-R, --random-sorta random order (a shuffle that groups equal lines together).

And these adjust what is compared:

OptionEffect
-f, --ignore-caseTreat lower and upper case as the same.
-d, --dictionary-orderConsider only letters, digits, and blanks.
-i, --ignore-nonprintingIgnore non-printing characters.
-b, --ignore-leading-blanksIgnore blanks at the start of a line (or field).

Sort order options #

OptionEffect
-r, --reverseReverse the result.
-u, --uniqueOutput only the first line of each run of equal lines — sort and de-duplicate in one step.
-s, --stableKeep equal lines in their original relative order.

Sorting by a field #

By default sort compares whole lines. -k sorts on a key — a chosen part of each line — which is what you need for tabular data.

A line is split into fields (by default at whitespace). A key is written FIELD[.CHAR][OPTIONS][,FIELD[.CHAR]] — a start field, an optional start character within it, and an optional end. The single-letter comparison options above can be appended to a key to apply only to it.

$ sort -k 2 -n scores.txt        # sort on the 2nd field, numerically
$ sort -t , -k 3 people.csv      # sort a CSV on the 3rd field
OptionEffect
-k, --key=KEYDEFSort on the key KEYDEF. May be given more than once; keys are tried in order.
-t, --field-separator=SEPUse SEP to split fields, instead of the whitespace default.

Checking and merging #

OptionEffect
-c, --checkDo not sort — just check whether the input is already sorted, and report the first line that is out of order.
-C, --check=silentCheck, but report only through the exit status.
-m, --mergeTreat the inputs as already-sorted files and merge them, which is faster than a full sort.

Output and resources #

OptionEffect
-o, --output=FILEWrite to FILE instead of standard output. FILE may be one of the inputs.
-z, --zero-terminatedTreat the NUL character as the line delimiter.
--parallel=NUse N threads.
-S, --buffer-size=SIZESet the size of the in-memory sort buffer.
-T, --temporary-directory=DIRPut temporary files in DIR rather than the default location.
--files0-from=FTake the list of input files from F, NUL-separated.
--debugUnderline the part of each line that was actually used as the sort key — invaluable when a -k key is not behaving.

Exit status #

CodeMeaning
0The lines were sorted — or, with -c/-C, the input was already sorted.
1With -c/-C, the input was not sorted.
2An error — a file could not be read, or an option was invalid.

Edit this page