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
| Option | Description |
|---|---|
-r, --reverse | Sort in reverse (descending) order. |
-n, --numeric-sort | Sort numerically instead of alphabetically. |
-k, --key=KEYDEF | Sort via a key; KEYDEF gives location and type. Sort by a specific column. |
-t, --field-separator=SEP | Use SEP instead of non-blank to blank transition as field separator. |
-u, --unique | Output only the first of an equal run (remove duplicates). |
-f, --ignore-case | Fold lower case to upper case characters (case-insensitive sort). |
Real-world Examples
-
Sort a file alphabetically:
sort names.txt -
Sort numerically: Useful when sorting a list of numbers or file sizes.
sort -n numbers.txt -
Sort by the second column (comma-separated):
sort -t ',' -k 2 data.csv -
Sort and remove duplicates:
sort -u list.txt