The sort command implements a full sort and merge facility. sort operates on input files containing records that are separated by the newline character.
If you do not specify either the
allows files containing null characters to be treated as ASCII files rather than binary files.
skips, for comparison purposes, any leading white space (blanks or tabs) in any field (or key specification).
checks input files to ensure that they are correctly ordered according to the key position and sort ordering options specified, but does not modify or output the files. This option only affects the exit code.
uses dictionary ordering. sort examines only blanks, upper and lowercase letters, and numbers when making comparisons.
converts lowercase letters to uppercase for comparison purposes.
ignores, for comparison purposes, non-printable characters.
specifies a sorting key. See the Sorting Keys section of this reference page for more information.
assumes that the field contains a month name for comparison purposes. Any leading white space is ignored. If the field starts with the first three letters of a month name in uppercase or lowercase, the comparisons are in month-in-year order. Anything that is not a recognizable month name compares less than JAN.
merges files into one sorted output stream. This option assumes that each input file is correctly ordered according to the other options specified on the command line; you can check this with the
specifies that the file being sorted does not have field delimiters.
assumes that the field contains an initial numeric value. sort sorts first by numeric value, then by the remaining text in the field, according to options. This option treats a field that contains no digits as if it had a value of zero. If more than one line contains no digits, the lines are sorted alphanumerically.
writes output to the file outfile. By default, sort writes output onto the standard output. The output file can be one of the input files. In this case, sort makes a copy of the data to allow the (potential) overwriting of the input file.
reverses the order of all comparisons so that sort writes output from largest to smallest rather than smallest to largest.
specifies tempdir as the directory to use for sort's temporary files. When this option is not specified, sort stores its temporary file in the directory specified by the TMPDIR environment variable.
indicates that the character char separates input fields. When you do not specify the
-toption, sort assumes that any number of white space characters (blank or tab) separate fields. -u
ensures that output records are unique. If two or more input records have equal sort keys, sort writes only the first record to the output. When you use
-uwith -c, sort prints a diagnostic message if the input records have any duplicates. -y[n]
restricts the amount of memory available for sorting to n K of memory (where a K of memory is 1024 bytes). If n is missing, sort chooses a reasonable maximum amount of memory for sorting, dependent upon system configuration. sort needs at least enough memory to hold five records simultaneously. If you try to request less, sort automatically takes enough. When the input files overflow available memory, sort automatically does a polyphase merge (external sorting) algorithm which is, of necessity, much slower than internal sorting. n must be at least 2. n has a maximum value of 1024 and a default value of 250.
indicates that the longest input record (including the newline character) is n characters in length. By default, record length is limited to 400 characters.
is an obsolete method of specifying a sorting key. See the Sorting Keys section of this reference page for more information.
By default, sort examines entire input records to determine ordering. By specifying sorting keys on the command line, you can tell sort to restrict its attention to one or more parts of each record.
You can indicate the start of a sorting key with
where m and the optional n are positive integers.
You can choose options from the set
The number m specifies which field in the input record
contains the start of the sorting key.
The character given with the
When you do not specify the
-toption, a field is considered to begin with the white space that separates it from the preceding field. When -tis specified, a field begins with the character following the separator.
You can also specify an ending position for a key, with
where p and q are positive integers, indicating that the sort key ends with the qth character of the pth field. If you do not specify q or you specify a value of 0 for q, the sorting key ends at the last character of the pth field. For example,
defines a sorting key that extends from the third character of the second
field to the sixth character of the fourth field.
sort also supports a historical method of defining the sorting key. Using this method, you indicate the start of the sorting key with
which is equivalent to
You can also indicate the end of a sorting key with
which when preceded with +m[.n] is equivalent to
if q is specified and is zero. Otherwise
defines a sorting key with a starting position that sort
finds by skipping the first field and the first 2 characters of the next
field, its end position is found by skipping the first three fields and then
the first five characters of the next field.
In other words the sorting key extends from the third character of the
second field to the sixth character of the fourth field.
This is the same key as defined under the
With either syntax, if the end of a sorting key is not a valid position or no end was specified, the sorting key extends to the end of the input record.
You can specify multiple sort key positions by using several
When you specify the
To sort an input file having lines consisting of the day of the month, white space and the month, as in:
30 December 23 MAY 25 June 10 June
use the command:
sort -k 2M -k 1n
To merge two dictionaries, with one word per line:
sort -m -dfi dict1 dict2 >newdict
Often it is useful to explicitly specify both the starting and ending field
sort -k 1,1 -k 2,2r file1
temporary files used for merging and
-ooption. You can specify a different directory for temporary files using the TMPDIR environment variable. For further information, see envvar.
Possible exit status values are:
Successful completion. Also returned if
-cis specified and the file is already in correctly sorted order.
Returned if you specified
-cand the file is not correctly sorted. Also returned to indicate a non-unique record if you specified -cu.
Failure due to any of the following:
- — missing key description after
- — more than one
- — missing file name after
- — missing character after
- — more than one character after
- — missing number with
- — endposition given before a startposition
- — badly formed sort key
- — invalid command line option
- — too many key field positions specified
- — insufficient memory
- — inability to open the output file
- — inability to open the input file
- — error writing to the output file
- — inability to create a temporary file or temporary file name
- — missing key description after
- Badly formed sort key position x
The key position was not specified correctly. Check the format and try again.
- file filename is binary
sort has determined that filename is binary because it found a NULL ('\0') character in a line.
- Insufficient memory for ...
This error normally occurs when you specify very large numbers for
-yor -zand there is not enough memory available for sort to satisfy the request.
- Line too long: limit nn - truncated
Any input lines that are longer than nn. which is the default number of characters (400) or the number specified with the
-zoption, are truncated.
- Missing key definition after -k
-k, but did not specify a key definition after the -k.
- Non-unique key in record: ...
-cand -uoptions, a non-unique record was found.
- Not ordered properly at: ...
-coption, an incorrect ordering was discovered.
- No newline at end of file
Any file not ending in a newline character has one added.
- Tempfile error on ...
The named temporary (intermediate) file could not be created. Make sure that you have a directory named /tmp and that this directory has space to create files. The directory for temporary files can be changed using the TMPDIR environment variable; see envvar.
- Tempnam() error
sort could not generate a name for a temporary working file. This should almost never happen.
- Temporary file error (no space) for ...
Insufficient space was available for a temporary file. Make sure that you have a directory named /tmp and that this directory has space to create files. The directory for temporary files can be changed using the ROOTDIR and TMPDIR environment variables; see envvar.
- Too many key field positions specified
This implementation of sort has a limit of 64 key field positions.
- Write error (no space) on output
Some error occurred in writing the standard output. This normally occurs when there is insufficient disk space to hold all of the intermediate data, or a diskette is write protected.
POSIX.2. x/OPEN Portability Guide 4.0. Windows Server 2012. Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019.
Available on all UNIX systems, with only UNIX System V.2 or later having the full functionality described here.
The maximum number of key field positions is 64.
The sortgen AWK script is a useful way to handle complex sorting tasks. It is described in the AWK Tutorial in the User's Guide. It originally appeared in The AWK Programming Language, by Aho, Weinberger, and Kernighan.
The sort command provided with PTC MKS Toolkit should not be confused with the Windows sort command.
PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
PTC MKS Toolkit 10.3 Documentation Build 39.