Skip to content

sort

14 commands

Sorts lines of text alphabetically, numerically, by field or by human-readable size.

  • text processing

Install sort

sudo apt install coreutils

Basics6

sort <file>

Sorts lines alphabetically.

Link
sort -r <file>

Reverse order.

Link
sort -n <file>

Numeric sort, so 10 comes after 9.

Link
sort -h <file>

Sorts human-readable sizes like 4K, 2M and 1G correctly.

Link
sort -u <file>

Sorts and removes duplicates in one step.

Link
sort -f <file>

Ignores case.

Link

Keys and fields4

sort -t<sep> -k<n> <file>

Sorts by field n using a delimiter — here, by UID.

Link
sort -k<n>,<n>nr <file>

Sorts by one field numerically in reverse; limiting the key to n,n stops it spilling into later fields.

Link
sort -V <file>

Version sort: 1.2 < 1.10.

Link
sort -t. -k1,1n -k2,2n -k3,3n -k4,4n <file>

Sorts IPv4 addresses numerically, octet by octet.

Link

Large data4

sort -o <file> <file>

Writes the result back to the same file safely (unlike > list.txt).

Link
sort -R <file>

Shuffles lines randomly.

Link
sort --parallel=<n> -S <size> <file>

Uses more threads and memory for very large files.

Link
sort -c <file>

Checks whether a file is already sorted.

Link