← Full reference

sort

Sorting lines — alphabetically, by numbers, sizes, versions and by individual columns

sort reads lines from files or standard input and prints them in order. By default it compares whole lines as text; options enable numeric comparison, reverse order and sorting by a single column. Several files are merged into one sorted stream.

Syntax

sort [options] [file…]
sort -k START[,END][type] [-t separator] [file…]

Options

How to compare

OptionWhat it does
-nas numbers: 9 before 10; understands a minus sign and a decimal point
-gas floating-point numbers, understands 1e3, inf (slower than -n)
-has sizes with suffixes: 512 < 1K < 2M < 1G (output of du -h, ls -lh)
-Vas versions: 1.2 < 1.9 < 1.10, file2 < file10
-Mas month names: JAN < FEB < … < DEC
-fcase-insensitive
-dconsider only letters, digits and blanks
-iignore non-printing characters
-bignore leading blanks in a field
-rreverse order
-Rshuffle (identical lines stay together)

Keys and separator

OptionWhat it does
-k 2the key is from the 2nd field to the end of the line
-k 2,2the key is only the 2nd field
-k 2,2nthe 2nd field as a number; the letters bdfgiMhnRrV can be appended to a key
-b -k 2.3,2.5characters 3 through 5 of the second field (without -b or -t counting starts from the blanks before the field)
-k1,1 -k3,3nrseveral keys: the next one decides only when the previous ones are equal
-t ','field separator. By default a field starts where a blank turns into a non-blank, and includes the leading blanks

Other

OptionWhat it does
-uof lines that are identical (by key!) keep the first
-sstable sort: lines equal by key keep their original order
-o filewrite the result to a file; it can be the same as the input
-conly check whether the input is sorted: prints the first out-of-place line, code 1; -C — the same, silently
-mmerge already sorted files without re-sorting
-zlines are separated by a NUL byte (for find -print0)
--debugunderline which part of the line sort treats as the key and warn about suspicious options
-S 50%, -T dir, --parallel=4buffer memory, directory for temporary files, number of threads — for very large files

Examples

# numbers: without -n lines are compared character by character
printf '10\n9\n100\n' | sort
printf '10\n9\n100\n' | sort -n

# by the second column as a number, descending
printf 'b 2\na 10\nc 1\n' | sort -k2,2nr

# CSV: by the second field
printf 'x,3\ny,1\nz,2\n' | sort -t, -k2,2n

# two keys: by the first field, on a tie — by the second as a number
printf 'a 2 x\na 1 y\nb 0 z\n' | sort -k1,1 -k2,2n

# /etc/passwd by UID
sort -t: -k3,3n /etc/passwd | head -n 3

# human-readable sizes — the largest on top
du -sh /var/* 2>/dev/null | sort -rh | head -n 5

# versions and names with numbers
printf '1.10\n1.9\n1.2\n' | sort -V
printf 'file10\nfile2\nfile1\n' | sort -V

# unique lines
printf 'c\na\nb\na\n' | sort -u

# case-insensitive
printf 'b\nB\na\nA\n' | sort -f

# sort a file "in place"
printf 'b\na\n' > list.txt
sort -o list.txt list.txt

# check whether a file is sorted
sort -c list.txt && echo 'sorted'

# see what sort treats as the key
printf 'x 10\ny 9\n' | sort --debug -k2,2n

Pitfalls

  • sort file > file wipes the file. The shell truncates it before sort starts. Use sort -o file file.
  • -k2 means "from the second field to the end of the line", not "the second field". Limit the key: -k2,2.
  • Letters on a key cancel the global options for it. In sort -n -k2,2r the second key is compared as text: it has its own letter r, and the global -n doesn't apply to it. Put everything in the key: -k2,2nr.
  • Without -n numbers sort as text: 10 < 100 < 9.
  • The order depends on the locale. The sandbox locale is POSIX (C): bytes are compared, so all upper-case letters come before lower-case ones (A B a b), and spaces and digits come before letters. On a system with en_US.UTF-8 the same input sorts differently (a A b B, punctuation is mostly ignored). For predictable results in scripts set LC_ALL=C sort.
  • The decimal separator also depends on the locale. In C it's a dot: 1,5 for sort -n is just 1.
  • Leading blanks are part of the field. Without -t and -b, in space-aligned output the line x b (two spaces) comes before y a (one space) with -k2,2: a space is less than any letter. Add -b or set a separator with -t.
  • sort -u compares only the key. With -k, lines whose keys match collapse even if they differ in other columns.
  • -h compares suffixes first, then numbers: 2000 ends up less than 1K. Sort output where all sizes are written the same way (du -h, ls -lh).

See also

uniq · cut · awk · head and tail · df and du