← The command line

Pipes

A pipe (|) sends one command's output into the next command instead of the screen, so small commands combine: grep finds lines, wc -l counts them, sort puts them in order, uniq drops repeats, head keeps the first few, and > saves the result.

No command counts how many different pets are in a list. Three tiny commands joined together do.

Each command does one small job. A pipe, the | character, joins them: the output of the command on the left becomes the input of the command on the right.

PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

What comes out?

grep cat pets.txt prints cat, catfish and cat. What does grep cat pets.txt | wc -l print?

TryTry it on the board and compare the result with your prediction.

Try: count with a pipe

Was your prediction right?

Try it yourselfYour edits are saved in this browser.

Run grep cat pets.txt, then the same with | wc -l on the end.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: a three-part pipe

Worked exampleBefore each step, say what you think comes next.
  1. sort pets.txt\text{sort pets.txt}cat, cat, catfish, dog, hamster, parrot: same lines, in order.

Now count the lines after uniq.

pets.txt is cat, dog, catfish, parrot, cat, hamster. How many lines does sort pets.txt | uniq print?

Sam explains a pipe. Click the line where it first goes wrong.

Why does uniq need sort before it?

ModifyChange the problem on purpose and see what happens.

Vary: different pipes

Try it yourselfYour edits are saved in this browser.

How many different pets are there? Use sort, uniq and wc -l in one line.

Use the board. This ticks when you get there.

Print just the first two pets in alphabetical order.

Use the board. This ticks when you get there.

Save the lines with cat in them into a new file called cats.txt.

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

Build the pipe

A server log has a line for every visit. Which pipe counts the visits from Australia (lines with AU)?

What does sort names.txt | head -n 1 print?

Review: what does rm -r projects do?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

The treasure hunt, part 2

Then: think of a question about a long list (your playlist, a class list) that a pipe could answer in one line.

Try it yourselfYour edits are saved in this browser.

You're in the office. The clue said: the right code is the only line with the word GOLD in it. Print just that line.

Use the board. This ticks when you get there.

What is the code?

One more: how many silver codes are there? Use a pipe.

Use the board. This ticks when you get there.

For teachers

Students join commands with pipes (grep, wc, sort, uniq, head), save output with >, and finish the treasure hunt by finding an exact line and counting with a pipe (AC9TDI10K01 node 1). About 45 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Try the boards yourself; the Make finishes the treasure hunt (office/codes.txt; the answer is 2468, the only line with GOLD in capitals).

The one idea: a pipe sends one command's output into the next, so small tools combine. This is how people answer questions about huge logs on servers.

Running the lesson

  1. Predict: students who said 6 hold Sam's misconception.
  2. Worked example: run each stage on its own, then join them.
  3. Vary: ask what head -n 2 would show without sort.
  4. Make: ask why grep -i GOLD gives two lines.

The hard part (on purpose)

Students try to do everything with one command. Ask them to say each stage aloud: "find the lines, then count them".

When students get stuck

Stretch and support

Stretch: Which pet name is the longest? (No single command does it; think about what you'd need.)

Support: A pipe diagram: file → grep → wc → screen, with the data written on each arrow.

Where it leads

Next: Who can read what (permissions).

What each step is for

Now you can
Next lesson5. Who can read whatEvery file has an owner and permissions: read, write and run, for the owner and for everyone else. ls -l shows them as letters like rw-r--r-- (644). The owner changes them with chmod; nobody else can. Hiding a file protects nothing; permissions do.