sort
text processingLinux/Unix
The sort command is one of the most frequently used commands in Linux/Unix-like operating systems. sort Sort lines of text files
Quick Reference
Command Name:
sort
Category:
text processing
Platform:
Linux/Unix
Basic Usage:
sort [options] [arguments]
Common Use Cases
Syntax
sort [options] [file...]
Options
Option | Description |
---|---|
-b, --ignore-leading-blanks |
Ignore leading blanks |
-c, --check |
Check if input is already sorted; don't sort |
-d, --dictionary-order |
Consider only blanks and alphanumeric characters |
-f, --ignore-case |
Fold lowercase to uppercase characters |
-g, --general-numeric-sort |
Compare according to general numerical value |
-h, --human-numeric-sort |
Compare human readable numbers (e.g., 2K, 1G) |
-i, --ignore-nonprinting |
Consider only printable characters |
-k, --key=POS1[,POS2] |
Sort via a key; start at POS1 and end at POS2 |
-m, --merge |
Merge already sorted files; don't sort |
-n, --numeric-sort |
Compare according to string numerical value |
-o, --output=FILE |
Write result to FILE instead of standard output |
-r, --reverse |
Reverse the result of comparisons |
-R, --random-sort |
Shuffle, but group identical keys (same as --random-source=FILE) |
-s, --stable |
Stabilize sort by disabling last-resort comparison |
-t, --field-separator=SEP |
Use SEP instead of non-blank to blank transition |
-u, --unique |
With -c, check for strict ordering; without -c, output only the first of equal lines |
-V, --version-sort |
Natural sort of (version) numbers within text |
--help |
Display help and exit |
--version |
Output version information and exit |
Examples
How to Use These Examples
The examples below show common ways to use the sort
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
# Basic Examples Basic
sort file.txt