Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sort Command Guide for Ordering Text

This guide helps you understand and use the sort command, the standard way to arrange lines of text alphabetically, numerically, or by any column.


What is sort, and Why Use It?

The sort command reads lines of text and writes them back in order — alphabetically by default, but you can order by number, by a specific column, or in reverse. It’s the backbone of nearly every “top N” pipeline on Linux.

Why Learn sort?

  • Everything counts better sorted: The classic “10 biggest files” or “top 10 errors” pipelines are sort plus uniq plus head.
  • Dedupe and group: Sorting is the required first step for uniq to find duplicates.
  • Data cleanup: Log files, CSV exports, and config lists become readable once ordered.
  • It’s everywhere: Every system has sort, and scripts depend on it for stable output.

If uniq is the accountant of Linux, sort is the bookkeeper — it has to do its job first.


1. Sort a File

sort names.txt

That prints the file’s lines in alphabetical order, without touching the original. To sort in place, use redirection or a temp file:

sort names.txt > sorted.txt

Apt-style lists sorted for readability:

sort /etc/passwd

2. Reverse and Numeric Sorting

sort -r names.txt
  • -r reverses the order — Z to A, 9 to 1.

Text sorts 10 before 9 because it compares character by character. For real numeric order:

sort -n numbers.txt
  • -n compares values as numbers, so 10 comes after 9.

Combine both for a “biggest first” list:

sort -rn numbers.txt

3. Sort by a Specific Column

Say a file has names and sizes separated by whitespace:

sort -k2 /etc/passwd
  • -k2 sorts by the second field (column) instead of the whole line.
  • -k2,2 sorts by field 2 only, not extending to the end of the line.

The classic pipeline — biggest files first:

du -sh * | sort -rh
  • -h understands human-readable sizes (1K, 234M, 1.2G), so this sorts them by actual size.

4. The Uniq-and-Sort Combo

sort is almost never used alone. The three-command pipeline that answers “what are the most common things?” is:

sort data.txt | uniq -c | sort -rn | head

Read right to left: take the top 100 lines of output, count occurrences, sort by count descending. This counts repeated log lines, visited domains, or error messages.

Sorting also makes uniq’s duplicate detection possible at all:

sort names.txt | uniq

That prints each name once.


5. Unique, Check, and Random

sort -u names.txt
  • -u outputs only the first of each run of equal lines — the same result as sort | uniq, in one command.

Check whether a file is already sorted (scripts use this):

sort -c names.txt && echo "sorted!" || echo "not sorted"
  • -c verifies order without printing anything; it exits with success only if the file is sorted.

Random order:

sort -R playlist.txt
  • -R shuffles lines — handy for randomizing lists in scripts.

6. Custom Sorting Rules

Sort with locale-specific rules (like ls does for filenames):

LC_ALL=C sort file.txt
  • Plain sort uses your locale, which can order accented characters surprisingly. LC_ALL=C forces byte order — faster and predictable in scripts.

Sort with a custom separator (for CSV files):

sort -t ',' -k3 -n data.csv
  • -t ',' says fields are separated by a comma instead of whitespace.

7. Combine with Pipes

cat access.log | sort | uniq -c | sort -rn | head -n 10

That finds the ten most-repeated lines in a log.

ps aux | sort -k4 -rn | head

That lists processes sorted by memory usage (column 4). See ps in the processes section for what those columns mean.


Common Pitfalls

  • Numeric columns sort wrong without -n. sort on 1 2 10 gives 1 10 2. Add -n when the values are numbers.
  • -k2 vs -k2,2: -k2 sorts from field 2 to the end of the line; ties extend the comparison. Use -k2,2 when only that column should decide.
  • sort changes your file’s order permanently if you redirect over it. Always sort to a new file or use sort -o file file — and beware -o with a single input overwriting while reading.
  • Human sizes need -h. sort -n treats 1.2G as 1.2. Use -h for sizes from du or ls -h.
  • Blank lines and trailing whitespace affect ordering. Use sort -b to ignore leading blanks when it matters.

PageRelationship
uniqFinds and counts duplicates — needs sort first.
wcCounts lines; often appears in the same pipelines.
head.mdShows the top N of a sorted list.
pipes.mdHow sort chains with uniq and head.

sort Cheatsheet

CommandWhat It Does
sort fileSort lines alphabetically.
sort -r fileSort in reverse.
sort -n fileSort numerically.
sort -rn fileSort numerically, biggest first.
sort -k2 fileSort by the second column.
sort -k2,2 fileSort by column 2 only.
sort -t ',' fileSort CSV using commas as separators.
sort -u fileSort and remove duplicates.
sort -c fileCheck if the file is sorted.
sort -R fileShuffle lines randomly.
sort -h fileSort human-readable sizes (K, M, G).
sort file > outWrite sorted output to a new file.