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
| Option | What it does |
|---|---|
-n | as numbers: 9 before 10; understands a minus sign and a decimal point |
-g | as floating-point numbers, understands 1e3, inf (slower than -n) |
-h | as sizes with suffixes: 512 < 1K < 2M < 1G (output of du -h, ls -lh) |
-V | as versions: 1.2 < 1.9 < 1.10, file2 < file10 |
-M | as month names: JAN < FEB < … < DEC |
-f | case-insensitive |
-d | consider only letters, digits and blanks |
-i | ignore non-printing characters |
-b | ignore leading blanks in a field |
-r | reverse order |
-R | shuffle (identical lines stay together) |
Keys and separator
| Option | What it does |
|---|---|
-k 2 | the key is from the 2nd field to the end of the line |
-k 2,2 | the key is only the 2nd field |
-k 2,2n | the 2nd field as a number; the letters bdfgiMhnRrV can be appended to a key |
-b -k 2.3,2.5 | characters 3 through 5 of the second field (without -b or -t counting starts from the blanks before the field) |
-k1,1 -k3,3nr | several 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
| Option | What it does |
|---|---|
-u | of lines that are identical (by key!) keep the first |
-s | stable sort: lines equal by key keep their original order |
-o file | write the result to a file; it can be the same as the input |
-c | only check whether the input is sorted: prints the first out-of-place line, code 1; -C — the same, silently |
-m | merge already sorted files without re-sorting |
-z | lines are separated by a NUL byte (for find -print0) |
--debug | underline which part of the line sort treats as the key and warn about suspicious options |
-S 50%, -T dir, --parallel=4 | buffer 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 > filewipes the file. The shell truncates it beforesortstarts. Usesort -o file file.-k2means "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,2rthe second key is compared as text: it has its own letterr, and the global-ndoesn't apply to it. Put everything in the key:-k2,2nr. - Without
-nnumbers 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 withen_US.UTF-8the same input sorts differently (a A b B, punctuation is mostly ignored). For predictable results in scripts setLC_ALL=C sort. - The decimal separator also depends on the locale. In
Cit's a dot:1,5forsort -nis just1. - Leading blanks are part of the field. Without
-tand-b, in space-aligned output the linex b(two spaces) comes beforey a(one space) with-k2,2: a space is less than any letter. Add-bor set a separator with-t. sort -ucompares only the key. With-k, lines whose keys match collapse even if they differ in other columns.-hcompares suffixes first, then numbers:2000ends up less than1K. 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