sort command (Linux / bash / sh / zsh)
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 / option | Meaning |
|---|---|
| -n | Compare values numerically. |
| -r | Reverse the result. |
| -k KEY | Sort using selected fields. |
| -u | Output one line per equal key. |
| -h | Compare 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.
Related commands
- uniqReport or remove adjacent duplicate lines.
- cutExtract selected fields, characters, or bytes from each input line.
- duEstimate disk usage for files and directory trees.
- grepSearch text for lines matching a pattern.
- Where-ObjectFilter pipeline objects by property values or a script block predicate.
- catConcatenate files and write their contents to standard output.
- Get-ContentRead file content or provider data as a stream of objects.
- echoWrite arguments and shell-expanded values to standard output.
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.