Linux and Unix sort command

Quick links

About sort
Syntax
Examples
Related commands
Unix main page

About sort

Sorts the lines in a text file.

Syntax

sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F

Options

-b, --ignore-leading-blanks Ignore leading blanks.
-d, --dictionary-order Consider only blanks and alphanumeric characters.
-f, --ignore-case Fold lower case to upper case characters.
-g, --general-numeric-sort Compare according to general numerical value.
-i, --ignore-nonprinting Consider only printable characters.
-M, --month-sort Compare (unknown) < `JAN' < ... < `DEC'.
-h, --human-numeric-sort Compare human readable numbers (e.g., "2K", "1G").
-n, --numeric-sort Compare according to string numerical value.
-R, --random-sort Sort by random hash of keys.
--random-source=FILE Get random bytes from FILE.
-r, --reverse Reverse the result of comparisons.
--sort=WORD Sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V.
-V, --version-sort Natural sort of (version) numbers within text.

Other Options

--batch-size=NMERGE Merge at most NMERGE inputs at once; for more use temp files.
-c, --check, --check=diagnose-first Check for sorted input; do not sort.
-C, --check=quiet, --check=silent Like -c, but do not report first bad line.
--compress-program=PROG Compress temporaries with PROG; decompress them with PROG -d.
--debug Annotate the part of the line used to sort, and warn about questionable usage to stderr.
--files0-from=F Read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input.
-k, --key=POS1[,POS2] Start a key at POS1 (origin 1), end it at POS2 (default end of line). See POS syntax below.
-m, --merge Merge already sorted files; do not sort.
-o, --output=FILE Write result to FILE instead of standard output.
-s, --stable Stabilize sort by disabling last-resort comparison.
-t, --field-separator=SEP Use SEP instead of non-blank to blank transition.
-T, --temporary-directory=DIR Use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories.
--parallel=N Change the number of sorts run concurrently to N.
-u, --unique With -c, check for strict ordering; without -c, output only the first of an equal run.
-z, --zero-terminated End lines with 0 byte, not newline.
--help Display a help message, and exit.
--version Display version information, and exit.

POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.

SIZE may be followed by the following multiplicative suffixes:

% 1% of memory
b 1
K 1024 (default)

...and so on for M, G, T, P, E, Z, Y.

With no FILE, or when FILE is a dash ("-"), read from standard input.

Also note that the locale specified by the environment affects sort order; set LC_ALL=C to get the traditional sort order that uses native byte values.

Examples

sort -r file.txt

Sort the contents of file.txt in reverse order.

Related commands

comm
join
uniq