Viewing and processing text

Command sort

Sort rows

Linux · Bash examples · commands run on the selected server

When to use sort

Use sort to order lines before comparison, deduplication or reporting. Set numeric comparison and locale deliberately: lexical ordering, numeric ordering and language-sensitive collation can produce different results.

Syntax

sort [OPTIONS] [FILE...]

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-n
Compare values as numbers.
-r
Reverse order.
-u
Remove identical lines after comparison.

Usage examples

Sort names

sort ./names.txt

Names are ordered using the active locale's collation rules.

Sort numbers

sort -n ./numbers.txt

Numeric values are compared numerically rather than as strings.

Get unique strings

LC_ALL=C sort -u ./names.txt

A fixed locale gives predictable byte-oriented ordering and removes duplicate lines.

Notes and limitations

  • The order depends on the locale; LC_ALL=C provides a byte comparison.
  • For columns, explicitly specify the delimiter and key via -t and -k.

Common questions and troubleshooting

Why does 10 appear before 2?

Default ordering compares text. Select numeric ordering for plain numbers, or the appropriate mode for versions or human-readable sizes.

Will sorting change my input file?

Not without an output option or redirection. Write results to a separate path and check them before replacing important input.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man sort.