The Linux sort Command: Sorting Lines

The Linux sort Command: Sorting Lines

sort orders lines of text. Essential for organizing output.


Basic Usage

sort file.txt

Alphabetical sort.

Numeric sort

sort -n numbers.txt

Numeric values.

Reverse

sort -r file.txt

Descending order.


Options

  • -n — Numeric sort
  • -r — Reverse
  • -u — Unique (dedupe)
  • -k N — Sort by column N
  • -t c — Field separator
  • -h — Human numeric (1K, 1M)

Examples

Sort by column

sort -k2,2 -t',' data.csv

Sort by 2nd field.

Unique sorted

sort -u file.txt

Sort and remove duplicates.


Further Reading