sort flags & options

-n, --numeric-sort

Sort by numeric value instead of alphabetically.

sort -n numbers.txt

-r, --reverse

Reverse the sort order.

sort -r file.txt
sort -rn numbers.txt

-k

Sort by a specific field (column). Fields are 1-indexed.

sort -k 2 file.txt
sort -t: -k3 -n /etc/passwd

-u, --unique

Output only unique lines (removes duplicates after sorting).

sort -u names.txt

-t

Set the field delimiter. Default is whitespace.

sort -t, -k2 -n data.csv

-h, --human-numeric-sort

Sort human-readable sizes (2K, 1G, etc.).

du -sh * | sort -h

-o

Write output to a file. Can safely be the same as the input file.

sort -o sorted.txt unsorted.txt
sort -o file.txt file.txt

-f, --ignore-case

Fold lowercase to uppercase for comparison (case-insensitive sort).

sort -f names.txt

-s, --stable

Stabilize sort by preserving original order of equal elements.

sort -s -k1,1 data.txt

-c, --check

Check if a file is already sorted. Exit with 1 if not.

sort -c file.txt