Sort (Unix)

In computing, sort is a standard command line program of Unix and Unix-like operating systems, that prints the lines of its input or concatenation of all files listed in its argument list in sorted order.

Uses your system voice; voice and rate in settings.

≈ 6 min read

Cite, export and learn8 citation formats · 8 exports · lesson and quiz

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In computing, sort is a standard command line program of Unix and Unix-like operating systems, that prints the lines of its input or concatenation of all files listed in its argument list in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as sort key.

Blank space is the default field separator. The command supports a number of command-line options that can vary by implementation. For instance the " -r " flag will reverse the sort order.

Sort ordering is affected by the environment's locale settings.

History

A command that invokes a general sort facility was first implemented within Multics. Later, it appeared in Version 1 Unix. This version was originally written by Ken Thompson at AT&T Bell Laboratories.

By Version 4 Thompson had modified it to use pipes, but sort retained an option to name the output file because it was used to sort a file in place. In Version 5, Thompson invented "-" to represent standard input. is part of X/Open Portability Guide Issue 2 (1987). From there it was inherited into POSIX.

The version of bundled in GNU coreutils was written by Mike Haertel and Paul Eggert. This implementation employs the mergesort algorithm. It offers an option to sort in parallel, though performance gain diminishes after 8 threads.

GNU parallel also provides a wrapper to perform parallel invocations of with similar performance-gain characteristics: on a 48-core system, the speedup is about 3×. The command has also been ported to the IBM i operating system, being accessible from the POSIX-compatible Qshell.

Syntax

sort [OPTION]... [FILE]... With no FILE , or when FILE is - , the command reads from standard input.

Parameters

In the table below, "Short" indicates only support for the one-letter (short) form of the option. Long options are originally a GNU extension and is not part of any version of SUS or POSIX. It has since also been adopted by FreeBSD.

Sort a file in alphabetical order

$ cat phonebook Smith, Brett 555-4321 Doe, John 555-1234 Doe, Jane 555-3214 Avery, Cory 555-4132 Fogarty, Suzie 555-2314 $ sort phonebook Avery, Cory 555-4132 Doe, Jane 555-3214 Doe, John 555-1234 Fogarty, Suzie 555-2314 Smith, Brett 555-4321

Sort by number

The -n option makes the program sort according to numerical value. The command produces output that starts with a number, the file size, so its output can be piped to to produce a list of files sorted by (ascending) file size: $ du /bin/* | sort -n 4 /bin/domainname 24 /bin/ls 102 /bin/sh 304 /bin/csh The command with the option prints file sizes in the 7th field, so a list of the files sorted by file size is produced by: $ find . -name "*.tex" -ls | sort -k 7n

Columns or fields

Use the -k option to sort on a certain column. For example, use " -k 2 " to sort on the second column. In old versions of sort, the +1 option made the program sort on the second column of data ( +2 for the third, etc.). This usage is deprecated. $ cat zipcode Adam 12345 Bob 34567 Joe 56789 Sam 45678 Wendy 23456 $ sort -k 2n zipcode Adam 12345 Wendy 23456 Bob 34567 Sam 45678 Joe 56789

Sort on multiple fields

The -k m,n option lets you sort on a key that is potentially composed of multiple fields (start at column m , end at column n ): $ cat quota fred 2000 bob 1000 an 1000 chad 1000 don 1500 eric 500 $ sort -k2,2n -k1,1 quota eric 500 an 1000 bob 1000 chad 1000 don 1500 fred 2000 Here the first sort is done using column 2. -k2,2n specifies sorting on the key starting and ending with column 2, and sorting numerically. If -k2 is used instead, the sort key would begin at column 2 and extend to the end of the line, spanning all the fields in between. -k1,1 dictates breaking ties using the value in column 1, sorting alphabetically by default.

Note that bob, and chad have the same quota and are sorted alphabetically in the final output.

Sorting a pipe delimited file

$ sort -k2,2,-k1,1 -t'|' zipcode Adam|12345 Wendy|23456 Sam|45678 Joe|56789 Bob|34567

Sorting a tab delimited file

Sorting a file with tab separated values requires a tab character to be specified as the column delimiter. This illustration uses the shell's dollar-quote notation to specify the tab as a C escape sequence. $ sort -k2,2 -t $'\t' phonebook Doe, John 555-1234 Fogarty, Suzie 555-2314 Doe, Jane 555-3214 Avery, Cory 555-4132 Smith, Brett 555-4321

Sort in reverse

The -r option just reverses the order of the sort: $ sort -rk 2n zipcode Joe 56789 Sam 45678 Bob 34567 Wendy 23456 Adam 12345

Sort in random

The GNU implementation has a -R --random-sort option based on hashing; this is not a full random shuffle because it will sort identical lines together. A true random sort is provided by the Unix utility shuf.

Sort by version

The GNU implementation has a -V --version-sort option which is a natural sort of (version) numbers within text. Two text strings that are to be compared are split into blocks of letters and blocks of digits.

Blocks of letters are compared alpha-numerically, and blocks of digits are compared numerically (i.e., skipping leading zeros, more digits means larger, otherwise the leftmost digits that differ determine the result). Blocks are compared left-to-right and the first non-equal block in that loop decides which text is larger.

This happens to work for IP addresses, Debian package version strings and similar tasks where numbers of variable length are embedded in strings.

See also

Collation List of Unix commands uniq shuf join (Unix)

Original Sort manpage The original BSD Unix program's manpage Further details about sort at Softpanorama

Text derived from https://en.wikipedia.org/w/index.php?oldid=1371442580 under CC BY-SA 4.0; modifications have been made.

Words in each sectionHistoryHistoryHistory: 171 words171Sort on multiple fieldsSort on multiple fieldsSort on multiple fields: 142 words142Sort by versionSort by versionSort by version: 107 words107Sort by numberSort by numberSort by number: 91 words91Columns or fieldsColumns or fieldsColumns or fields: 76 words76Sorting a tab delimited…Sorting a tab delimited fileSorting a tab delimited file: 59 words59ParametersParametersParameters: 44 words44Sort in randomSort in randomSort in random: 36 words36Sort a file in alphabet…Sort a file in alphabetical orderSort a file in alphabetical order: 34 words34Sort in reverseSort in reverseSort in reverse: 24 words24Sorting a pipe delimite…Sorting a pipe delimited fileSorting a pipe delimited file: 19 words19SyntaxSyntaxSyntax: 17 words17See alsoSee alsoSee also: 9 words9
Words in each section. Measured from the stored article body; sections below the minimum are not plotted.

Provenance

snapshot enwiki-001371442580 · captured 2026-08-26 · ledger · version history

Cite this page as: encyclopegia.org, “Sort (Unix)”, snapshot enwiki-001371442580. https://encyclopegia.org/a/sort-unix

Sources cited in this revision

9 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.