sort
Sort lines of text files
Overview
Sorts the contents of a text file, line by line. Sorting is based on one or more sort keys extracted from each line.
Syntax
sort [OPTION]... [FILE]...Common Options
-r, --reverseReverse the result of comparisons.
-n, --numeric-sortCompare according to string numerical value.
-f, --ignore-caseFold lower case to upper case for comparisons.
-u, --uniqueWith -c, check for strict ordering. Without -c, output only the first of an equal run.
-k POS1[,POS2], --key=POS1[,POS2]Sort via a key; POS1 gives the start position and POS2 the end position (optional).
-t CHAR, --field-separator=CHARUse CHAR as field separator.
-o FILE, --output=FILEWrite result to FILE instead of standard output.
-C, --check=quiet, --check=silentCheck for sorted input; do not output anything, merely set exit status.
-m, --mergeMerge already sorted files; do not sort.
-h, --human-numeric-sortCompare human readable numbers (e.g., 2K 1G).
Examples
Sorts the lines in names.txt alphabetically.
Sorts data.txt in reverse (descending) order.
Lists files and sorts them by size (5th field, numerically).
Sorts duplicates.txt and removes duplicate lines.
Sorts the /etc/passwd file numerically by the 3rd field (UID), using ':' as a delimiter.
Sorts data.txt and saves the output to sorted_data.txt.
Checks if numbers.txt is already sorted; prints nothing if sorted, an error otherwise.
Sorts lines with human-readable sizes (like 1K, 2M) in sizes.txt.