Skip to main content

Command referencesort

sort command (Linux / bash / sh / zsh)

Linuxbashshzshtextbeginner

What it does

Order text lines using lexical, numeric, or keyed comparisons. When people search for the sort command linux text, they usually want sort's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.

Order text lines using lexical, numeric, or keyed comparisons.

Syntax

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

Common options

Flag / optionMeaning
-nCompare values numerically.
-rReverse the result.
-k KEYSort using selected fields.
-uOutput one line per equal key.
-hCompare human-readable numbers.

Practical examples

Alphabetical order

sort names.txt

Sorts lines using the current locale.

Numeric reverse

sort -nr scores.txt

Places the largest number first.

Readable sizes

du -h --max-depth=1 | sort -h

Orders human-readable disk usage.

Common mistakes

  • Using lexical sort for numbers, placing 10 before 2.
  • Forgetting locale settings can change collation order.

FAQ

What does sort do in Linux?

Order text lines using lexical, numeric, or keyed comparisons.

How do I use sort from the terminal?

Start with `sort [OPTION]... [FILE]...`, then choose only the options needed for the task.

What is a practical sort example?

Try `sort names.txt` — Sorts lines using the current locale.

Can I use sort in a shell script?

Yes. sort works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.

Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.