Skip to main content

sort

The sort command is used to sort the lines of text files. It can sort alphabetically, numerically, in reverse order, and can sort by specific columns or fields.

Basic Syntax

sort [OPTION]... [FILE]...

Common Options

OptionDescription
-r, --reverseSort in reverse (descending) order.
-n, --numeric-sortSort numerically instead of alphabetically.
-k, --key=KEYDEFSort via a key; KEYDEF gives location and type. Sort by a specific column.
-t, --field-separator=SEPUse SEP instead of non-blank to blank transition as field separator.
-u, --uniqueOutput only the first of an equal run (remove duplicates).
-f, --ignore-caseFold lower case to upper case characters (case-insensitive sort).

Real-world Examples

  1. Sort a file alphabetically:

    sort names.txt
  2. Sort numerically: Useful when sorting a list of numbers or file sizes.

    sort -n numbers.txt
  3. Sort by the second column (comma-separated):

    sort -t ',' -k 2 data.csv
  4. Sort and remove duplicates:

    sort -u list.txt