Learn › Linux Foundations › Text Processing
sort - a hands-on Linux lab on a real virtual machine.
Put lines in order with sort: the lexical default, -r to reverse, -u for sorted-and-unique, and -n to fix the classic trap where 10 sorts before 2. Then sort one column of a table with -t and -k, all in the practice terminal on three tiny files.
You have a file. Each line is one item: a name, a number, a size. The lines are in whatever order they happened to be written, which usually means no order at all. Somewhere in that jumble is the biggest number, or the duplicate that should not be there, or the name that comes first alphabetically. Scanning by eye, you miss it.
There is one command whose entire job is to put lines in order so the answer jumps out. It is called sort, and by the end of this lesson you will bend it to alphabetical order, numeric order, reverse order, and one column of a table. This lesson is all of it.
The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. sort never changes your file. It reads the lines, orders them, and prints the ordered version to the screen. The file on disk is left exactly as it was.
You are in a folder with a small file called names.txt. It holds four lines, in this order:
banana
apple
cherry
apple
Point plain sort at it and watch the lines fall into order. Type this exact command and press Enter:
sort names.txt
prompt: student@linuxcamp:~$ answer: sort names.txt output: apple apple banana cherry hint: Type sort, a space, then the filename: sort names.txt
The four lines came back alphabetical: both apple lines first, then banana, then cherry. sort did not touch names.txt on disk; it printed an ordered copy. Notice it kept BOTH copies of apple. Plain sort orders lines, it does not remove duplicates. Getting rid of the second apple is a job for a flag you will meet soon.
sort reads every line of a file and prints those same lines back in order. By default that order is lexical, which means dictionary order: character by character, the way words are arranged in a phone book or a glossary. a comes before b, apple before banana.
The command is one of the oldest tools in Unix, written in the early 1970s, back when "sorting" a deck of punched cards was a physical chore someone did by hand. sort turned that chore into one word you type. It has been quietly ordering lines ever since.
Lexical order compares text one character at a time, left to right. That rule is simple and it is almost always what you want for words. But it has one famous surprise when the lines are numbers, and meeting that surprise on purpose, in the sandbox, is the heart of this lesson. Hold that thought.
The first flag flips the direction. -r is short for reverse. Everything sort would print in order, -r prints from the bottom up instead: Z to A instead of A to Z.
Run it on the same names.txt and watch the ordering turn around:
sort -r names.txt
prompt: student@linuxcamp:~$ answer: sort -r names.txt output: cherry banana apple apple hint: Add -r between sort and the filename: sort -r names.txt
Exactly upside down from the first run. cherry now leads, then banana, then the two apple lines at the bottom. -r does not sort differently, it sorts the same way and then reverses the result. Reach for it when you want the largest, the newest, or the last-in-the-alphabet at the top of the list.
Plain sort kept both apple lines. Often you want the opposite: an ordered list with each line appearing only once. That is -u, short for unique. sort -u orders the lines AND drops every exact repeat, keeping just one of each.
Run it on names.txt, which you know has a repeated apple:
sort -u names.txt
prompt: student@linuxcamp:~$ answer: sort -u names.txt output: apple banana cherry hint: Add -u for unique lines: sort -u names.txt
Three lines now, not four. The second apple is gone. -u first ordered the lines, then collapsed the run of identical apple lines into one. This is the fast way to answer "what are the distinct values in this list?" Sort them and strip the repeats in a single step. There is a dedicated command for de-duplicating, uniq, which is its own lesson; sort -u is the shortcut when you also want ordering.
This is the surprise the whole lesson has been building toward, and it is far better to meet it here than in a report that quietly comes out wrong. There is a second file, nums.txt, with four lines:
10
2
1
30
Before you run anything, decide what you expect. A human reads those as one, two, ten, thirty. Now run plain sort and compare it against that expectation:
sort nums.txt
prompt: student@linuxcamp:~$ answer: sort nums.txt output: 1 10 2 30 hint: Just sort and the filename, no flag yet: sort nums.txt
Look at the order: 1, then 10, then 2, then 30. That is almost certainly not what you wanted, and it is the single most common sort mistake. Remember the default is lexical, dictionary order, one character at a time. Comparing character by character, 10 starts with 1, and 2 starts with 2, and 1 comes before 2, so 10 lands ahead of 2. sort is not broken. It sorted the text of the numbers, not their value. The fix is the next flag.
The cure for the trap is -n, short for numeric. It tells sort to read each line as a number and order by its actual value, so 2 correctly comes before 10.
Run the same nums.txt, this time with -n, and watch the order come right:
sort -n nums.txt
prompt: student@linuxcamp:~$ answer: sort -n nums.txt output: 1 2 10 30 hint: Add -n for numeric order: sort -n nums.txt
Now it reads 1, 2, 10, 30, in true numeric order. The only difference from the last run was three characters, -n, and it turned a wrong answer into a right one. Burn in the rule: the moment your lines are numbers, add -n. Without it, sort compares the digits as text and 10 slips ahead of 2. This one flag is the difference between a report you can trust and one you cannot.
Two cousins of -n are worth a mention. -f folds case, so Apple and apple sort together instead of all capitals landing first. And -h handles human sizes like 2K and 1G, ordering them by real magnitude so 1G beats 500M. Both follow the same pattern you just learned: a single flag that changes how sort reads each line.
Real data often has columns. A third file, sizes.txt, pairs a fruit with a size, separated by a comma:
apple,30
banana,10
cherry,2
You want these ordered by the SIZE, the second column, smallest first, not by the fruit name. Two flags team up for that. -t sets the delimiter, the character that splits one column from the next, here a comma. -k picks which field to sort on, counting from 1, so -k2 means the second column. Add -n because those sizes are numbers. Put together:
sort -t, -k2 -n sizes.txt
prompt: student@linuxcamp:~$ answer: sort -t, -k2 -n sizes.txt output: cherry,2 banana,10 apple,30 hint: -t sets the comma delimiter, -k2 picks column two, -n makes it numeric: sort -t, -k2 -n sizes.txt
The rows came back ordered by the number after the comma: cherry,2, then banana,10, then apple,30. The fruit names are now scrambled, and that is correct, because you told sort to ignore column one and order on column two. -t, said the columns are split by commas, -k2 said sort on the second one, and -n said treat it as a number. Drop the -n and you would hit the lexical trap all over again on 2, 10, 30. This trio, -t, -k, and -n, is how you sort any table by any column.
Scaffolding off. No command is shown this time. You have every piece you need.
Go back to names.txt, the fruit file with the repeated apple line. You want a clean list: every fruit name that appears, alphabetical, with no line printed twice. One flag on sort does both the ordering and the de-duplicating in a single pass. Point it at names.txt.
prompt: student@linuxcamp:~$ answer: sort -u names.txt output: apple banana cherry hint: Ordering plus no duplicates is one flag: the unique flag -u. Point sort -u at names.txt: sort -u names.txt
Three lines, alphabetical, no repeat, typed from memory. -u ordered the four lines and then dropped the duplicate apple, leaving one clean copy of each name. That is the everyday move: know the shape of the result you want, pick the one flag that gets you there, and type it with no prompt. It is exactly the recall the real machine will ask of you.
You earned this cheat sheet. Every row is a form of sort you have already run:
The one rule to carry away: the default sort is lexical, so it compares numbers as text and 10 lands before 2. The moment your lines are numbers, add -n. And when your data has columns, -t names the separator and -k picks the column.
sort is rarely the end of a chain. Later in the camp you will feed one command's output straight into sort, and pair sort with uniq -c to count how many times each distinct line appears, the classic "top offenders" recipe. The ordering skill you built here is the same one every one of those tricks stands on.
The practice terminal has shown you every form of sort: the lexical default, -r to reverse, and -u to de-duplicate. You met the number trap and its -n cure, and the -t plus -k pair for sorting one column of a table. Every one of those you typed yourself.
The Text Processing module ends with one real Linux machine, the text-processing capstone mission, and that is where you order real data. A VM boots just for you with its own files. Its objectives use exactly what you practiced across this module: filtering, cutting, counting, and now ordering lines. Alongside sort you will use the module's other tools, cut, uniq, tr, and the rest, each of which has its own lesson. One difference from here: the mission shows no commands. You read the objective, recall the flag, and type it. That recall is what makes it stick.
Finish the other Text Processing lessons, then go put real data in order.
Practice sort in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.