sort, one decision per page
Twenty-eight lessons on the sort command, one sorting decision per page: the line sorter that takes the whole line as its key by default; the text trap that puts 10 before 9 and the -n that fixes it; -r to reverse; fields and keys with -k, key ranges that end where they should, -t separators and two-key tie-breaks; -u for one of each; -M for months and -h for human sizes like 2K and 1G; stability and -s; the -c and -C checks before you trust a file; the locale surprise and LC_ALL=C; -b for leading blanks; uniq next door with its adjacency rule, uniq -c counting and the sort, count, sort ladder; -m merge, -o output and the dash for standard input; mergesort under the hood; the kin cut and comm on sorted files; and teaching a friend one honest hour.
One decision, one page, one honest hour. That's the rhythm.
sort, one decision per page
Twenty-eight lessons on the sort command, one sorting decision per page: the line sorter that takes the whole line as its key by default; the text trap that puts 10 before 9 and the -n that fixes it; -r to reverse; fields and keys with -k, key ranges that end where they should, -t separators and two-key tie-breaks; -u for one of each; -M for months and -h for human sizes like 2K and 1G; stability and -s; the -c and -C checks before you trust a file; the locale surprise and LC_ALL=C; -b for leading blanks; uniq next door with its adjacency rule, uniq -c counting and the sort, count, sort ladder; -m merge, -o output and the dash for standard input; mergesort under the hood; the kin cut and comm on sorted files; and teaching a friend one honest hour.
Set in Space Grotesk, Inter and JetBrains Mono (SIL Open Font License).
Every fact in this book is as the official source states it, fetched and read during this build: the local manual pages sort(1), uniq(1), cut(1) and comm(1) (GNU coreutils 9.4), the definition of truth for this book, and the Wikipedia articles Sort (Unix), uniq, Comm, Cut (Unix), Sorting algorithm and POSIX. No facts are sourced from Reddit, Stack Overflow, vendor blogs or commercial sites. Every command and output pair was run on this machine with temporary files under the build scratch directory only, each capture cached verbatim so the pair is reproducible. SAFETY LAW: no file outside the book directory's scratch area was written by any demonstration; the -o option wrote only to a scratch copy, and the check flags -c and -C never wrote anything. Honest notes: the Wikipedia titles Uniq (Unix) and Comm (Unix) are redirects (to uniq and Comm), so both were followed and read at their redirect goals. An independent guide, not affiliated with or endorsed by the Free Software Foundation, the IEEE, or the Wikimedia Foundation.
Your purchase is for personal use only. You do not have redistribution rights: please do not share, resell, or republish this book or its pages.
© 2026 Steve Hodgkiss. All rights reserved. Personal use only; no redistribution rights.
Edition 1.0 · stevehodgkiss.net
Contents
Per the Wikipedia Sort (Unix) article, the -r flag will reverse the sort order. Per the sort(1) manual page, -r, --reverse reverses the result of comparisons. This machine's capture: sort -r numbers.txt printed 9, 2, 10, 1, and sort -rn numbers.txt printed 10, 9, 2, 1.
Reverse with -r
-r is the cleanest decision in the book: per the sort(1) manual page it reverses the result of comparisons, nothing else. The Wikipedia article says the same: the -r flag will reverse the sort order.
Our capture: sort -r numbers.txt printed 9, 2, 10, 1, the text order walked backwards. Add -n and the flip lands on the numeric order: sort -rn printed 10, 9, 2, 1.
Read -rn as two decisions stacked: numeric first, reverse second.
Sort your number file with sort -rn and with sort -r, and write down which flag pair gave 10 first and why.
Per the Wikipedia Sort (Unix) article, -k2,2n specifies sorting on the key starting and ending with column 2, and sorting numerically, and if -k2 is used instead, the sort key would begin at column 2 and extend to the end of the line, spanning all the fields in between. This machine's capture: on pets.txt holding zed bird aaa and amy bird zzz, sort -k2,2 printed amy first while sort -k2 printed zed first.
The key range
A key has a start and an end. Per the Wikipedia article, -k2,2n specifies sorting on the key starting and ending with column 2. Drop the end and -k2 spans all the fields in between, to the end of the line.
Our capture on pets.txt: with sort -k2,2 the bird keys tie and the whole line breaks it, amy first; with sort -k2 the key becomes bird aaa against bird zzz, and zed lands first.
Same file, opposite answers. Write the end of the key, always, and the surprise disappears.
Sort pets.txt both ways yourself, then write one sentence on why the two orders differ.
Per the Wikipedia Sorting algorithm article, stable sorting algorithms maintain the relative order of records with equal keys. Per the sort(1) manual page, -s, --stable stabilizes sort by disabling last-resort comparison. This machine's capture: sort -k1,1n pairs.txt printed 1 x, 1 y, 2 a, 2 b, while sort -s -k1,1n pairs.txt printed 1 y, 1 x, 2 b, 2 a.
Stable order
When keys tie, something still decides. Per the Wikipedia Sorting algorithm article, stable sorting algorithms maintain the relative order of records with equal keys.
GNU sort breaks ties with a last-resort whole-line comparison; per the manual page, -s disables last-resort comparison and stabilizes the sort. Our capture: without -s, ties reorder to 1 x, 1 y; with sort -s -k1,1n, pairs keep input order: 1 y, 1 x, 2 b, 2 a.
Stability is the amber promise: your earlier care survives the later sorts.
Copy pairs.txt, run both sorts, and underline the rows that switched places when -s arrived.
Per the sort(1) manual page, -b, --ignore-leading-blanks ignores leading blanks, and if neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. This machine's capture: sort indent.txt printed the four-space apple line first and fig, kiwi before the two-space pear, while sort -b indent.txt ordered them apple, fig, kiwi, pear by name.
Blanks ignored
Indentation is data to a plain sort: our capture shows sort indent.txt ranking blank space before every letter, so the indented lines float to the top.
Per the sort(1) manual page, -b ignores leading blanks. With it, the same file orders itself by name: apple, fig, kiwi, pear, the leading spaces still printed but no longer counted.
The manual page adds the fine print: without -t or -b, field characters count from the preceding whitespace. -b removes that drift.
Make an indented file with cat -A to see the spaces, sort it plain and with -b, and compare the first lines.
Per the Wikipedia uniq article, the command sort file | uniq -c | sort -n lists the unique lines in a file, sorted by the number of times each occurs, and using uniq like this is common when building pipelines in shell scripts. This machine's capture: sort dup.txt | uniq -c | sort -n printed 1 fig, 2 apple, 3 pear, the counts now in numeric order.
The count ladder
Here is the whole book in one line, straight from the Wikipedia uniq article: sort file | uniq -c | sort -n lists the unique lines, sorted by the number of times each occurs.
Our capture ran it: 1 fig, 2 apple, 3 pear, the census re-sorted by its own counts, smallest first.
Three decisions stacked: order, count, order by the count. The article notes this pattern is common when building pipelines in shell scripts.
Build the ladder over your duplicate file, then break one rung and predict the shape of the wreckage before you look.
Per the sort(1) manual page, -o, --output=FILE writes the result to FILE instead of standard output. Per the Wikipedia Sort (Unix) article history, sort retained an option to name the output file because it was used to sort a file in place, from the days of Version 4 Unix. This machine's capture: sort -n -o numbers_sorted.txt numbers.txt then cat printed 1, 2, 9, 10.
The -o output
The screen is not the only destination. Per the sort(1) manual page, -o writes the result to FILE instead of standard output.
It is an old idea: per the Wikipedia article's history, sort retained an option to name the output file because it was once used to sort a file in place.
Our capture, scratch copy only: sort -n -o numbers_sorted.txt numbers.txt, and the cat of the destination printed 1, 2, 9, 10. The source file stayed as it was.
Sort a file into a new scratch file with -o, cat both files, and confirm the original never changed.
Per this book's plan, the last lesson hands the tool to a friend in one honest hour: five lines re-read from this book's own captures, typed by the learner, read aloud by the teacher. Per the Wikipedia Sort (Unix) article, sort prints the lines of its input in sorted order and sorting is done based on one or more sort keys extracted from each line of input. This machine's capture: the five teaching lines are sort (GNU coreutils) 9.4, then 1, 10, 2, 9 against sort -n giving 1, 2, 9, 10, then sort -c unsorted.txt reporting disorder at line 3.
Teaching sort
The last decision is not a flag: it is passing the tool on, one friend, one honest hour, per this book's plan.
They type; you read aloud. The five lines are this book's own captures: sort (GNU coreutils) 9.4, the text trap 1, 10, 2, 9, its cure 1, 2, 9, 10, the check report disorder at line 3, and the ladder's 3 pear.
When they can explain why ten landed before nine in their own words, the hour is done and the tool is theirs.
Sit with one friend and teach sort in one sitting; they type the five lines, you read each decision aloud.