Text processing

Linux sort command

Sort lines of text. Add -n for numeric order or -r for reverse order.

Availability: Common Linux commands; shell built-ins and external implementations may differ by distribution.中文

Syntax

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

Examples

Sort lines by numeric value.

sort -n scores.txt

Sort colon-separated records by their third field numerically.

sort -t: -k3,3n /etc/passwd

Parameters, options and clauses

Option / clauseDescription
-n, --numeric-sortCompare according to string numerical value.
-r, --reverseReverse the result of comparisons.
-u, --uniqueOutput only the first of equal lines.
-k, --key=KEYDEFSort by a selected field or character range.
-t, --field-separator=SEPUse SEP instead of whitespace between fields.
-h, --human-numeric-sortCompare human-readable numbers such as 2K and 1G.
-f, --ignore-caseFold lowercase letters to uppercase for comparison.
Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory