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 [options]... [file]

-b Ignores spaces at beginning of the line.
-c Check whether input is sorted; do not sort
-d Uses dictionary sort order and ignores the punctuation.
-f Ignores caps
-g Ccompare according to general numerical value
-i Ignores nonprinting control characters.
-k Start a key at POS1, end it at POS2 (origin 1)
-m Merges two or more input files into one sorted output.
-M Treats the first three letters in the line as a month (such as may.)
-n Sorts by the beginning of the number at the beginning of the line.
-o Write result to FILE instead of standard output
-r Sorts in reverse order
-s Stabilize sort by disabling last-resort comparison
-t Use SEP instead of non-blank to blank transition
-T Uuse DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories
-u If line is duplicated only display once
-z End lines with 0 byte, not newline
+fields Sorts by fields , usually by tabs
filename The name of the file that needs to be sorted.
-o outputfile Sends the sorted output to a file.

Examples

sort -r file.txt

Sort the file, file.txt in reverse order.

Related commands

comm
join
uniq