Pipes and redirects
Chain commands together with pipes, save their output with redirects, and learn why the terminal feels like a workshop. Part 5 of the Terminal Basics series.
By the end of this tutorial you will know how to:
- connect commands with a pipe (
|) so one command's output feeds the next - sort and count with
sort,uniqandcut - save output into a file with
>and>> - keep errors separate from normal output
- chain several of these together to answer a real question
Prerequisites: you can read and search files with cat, grep and wc (part 3).
How this was checked: every command was run on Ubuntu 24.04 with bash 5.2, GNU coreutils 9.4 and GNU grep 3.11, in a real terminal session as an ordinary (non-admin) user, and the output shown is the real output. Your username will differ from ours (gerald).
To follow along: type each command and press Enter. A few commands here write files, and they only write inside the practice mansion.
1. Small tools, connected
Every command you have met does one small thing. grep finds lines. wc counts them. sort puts them in order. None of them is impressive alone.
The trick of the terminal is that you can connect them. Every command has an input and an output. By default the output goes to your screen. But you can send it somewhere else, such as into another command, or into a file. That is all this post is about, and it is the moment the terminal stops being a pile of tools and becomes a workshop.
Think of an assembly line. One machine does one job and passes the result along. The product, in this mansion, is facts about haunted kitchens.
2. Rebuild the mansion
Start from a fresh mansion with the setup script from part 1 (the whole script is in that post if you do not have it), then go to the kitchen, where the complaints live:
bash ~/mansion-setup.shThe mansion is built. Try not to wake anyone.cd ~/mansion/kitchen3. The pipe: |
The pipe is the vertical bar, |. It takes the output of the command on its left and feeds it into the command on its right. For example, count the lines in the complaint log by sending cat's output into wc:
cat complaints.log | wc -l12Twelve complaints. You could have written wc -l complaints.log and got the same answer, and that is fine. The point is the shape: wc has no file name here, because its input is whatever came down the pipe.
Pipes get useful when the left side does something. Find the errors, then count them:
grep ERROR complaints.log | wc -l5Or find the errors, and show only the first two:
grep ERROR complaints.log | head -n 22026-10-31 00:02 ERROR the fridge is screaming
2026-10-31 03:12 ERROR the stove door opened by itselfYou can chain as many as you like. Each | hands its output to the next command, left to right, like a conveyor belt.
4. sort, uniq and cut
Three small tools make pipes powerful.
sort puts lines in order. uniq removes adjacent duplicate lines, and with -c it counts them instead. cut pulls a column out of each line.
cut: take a column
Each line of the complaint log has fields separated by spaces: the date, the time, the level, then the message. cut takes -d (the delimiter, here a space) and -f (which field to keep). Field 3 is the level:
cut -d' ' -f3 complaints.logINFO
WARN
ERROR
WARN
ERROR
ERROR
INFO
WARN
ERROR
INFO
ERROR
INFOsort, then uniq
Those levels repeat. uniq removes repeats, but only ones that sit next to each other, so the lines have to be sorted first. Watch what uniq does on the unsorted list of guests:
uniq ../library/guests.txtgerald
brenda
gerald
casper
brenda
gerald
morticia
casperNothing was removed, because no two identical names happened to be neighbours. Sort first, and the duplicates line up:
sort ../library/guests.txt | uniqbrenda
casper
gerald
morticiaAdd -c and uniq tells you how many of each:
sort ../library/guests.txt | uniq -c 2 brenda
2 casper
3 gerald
1 morticiaThat is the most useful pipe in the terminal. To put the most frequent first, sort the counts again with -r (reverse) and -n (treat them as numbers, so 10 comes after 9, not before):
sort ../library/guests.txt | uniq -c | sort -rn 3 gerald
2 casper
2 brenda
1 morticiaFour commands, one line, and you know who haunts the mansion most. (Where two names tie, they come out in reverse alphabetical order, because -r reverses everything.)
You should see 3 gerald at the top. If the counts look wrong, check that sort comes before uniq: uniq only notices duplicates that are next to each other.
5. Redirects: save output into a file
A pipe connects a command to another command. A redirect connects a command to a file.
> sends output into a file instead of the screen. Save the errors from the log:
grep ERROR complaints.log > errors.txtNothing appeared on the screen, because the output went into the file. Look at what is in it:
cat errors.txt2026-10-31 00:02 ERROR the fridge is screaming
2026-10-31 03:12 ERROR the stove door opened by itself
2026-10-31 03:13 ERROR the stove door closed by itself
2026-11-01 02:21 ERROR the toaster named a name
2026-11-02 03:00 ERROR the fridge is screaming>> is similar, but it adds to the end of the file instead of replacing it. Report a new error by hand with echo, which prints text, and append it:
echo "2026-11-03 02:00 ERROR the mirror blinked" >> errors.txt
tail -n 2 errors.txt2026-11-02 03:00 ERROR the fridge is screaming
2026-11-03 02:00 ERROR the mirror blinked> overwrites without asking
> is the same story as cp and rm. If the file already exists, > replaces everything in it, immediately, with no warning and no trash can. Watch:
echo "oops" > errors.txt
cat errors.txtoopsSix errors, gone. One careless > is enough. If you want to add to a file, you want >>, with two arrows. If you want to replace it, you want >, and you want to be sure.
Saving a whole pipeline
A redirect goes on the end of a pipeline. This counts every level in the log and saves the result:
cut -d' ' -f3 complaints.log | sort | uniq -c | sort -rn > report.txt
cat report.txt 5 ERROR
4 INFO
3 WARN6. Errors travel separately
A command has two outputs: its normal output, and a separate channel just for error messages. Both show up on your screen, which is why they look like one thing. But > only catches the normal output. Try listing a directory that does not exist, and redirecting:
ls nope > output.txtls: cannot access 'nope': No such file or directoryThe error still appeared on the screen, because it did not go through >. And the file we asked for is empty:
cat output.txtTo redirect the error channel, put a 2 in front of the arrow:
ls nope 2> problems.txt
cat problems.txtls: cannot access 'nope': No such file or directoryThe first command printed nothing, because its error went into the file. There is also a special place to send things you never want to see again, called /dev/null. It accepts anything and keeps nothing, which makes it the only thing in the mansion that never complains:
ls nope 2> /dev/nullNothing printed, and nothing saved.
7. Putting it together
Time to answer a real question. Which complaint does the mansion hear about most? Take only the errors, cut away the date, time and level so just the message remains (-f4- means "field 4 to the end"), then count and rank:
grep ERROR complaints.log | cut -d' ' -f4- | sort | uniq -c | sort -rn | head -n 3 2 the fridge is screaming
1 the toaster named a name
1 the stove door opened by itselfFive commands, and you have the answer: the fridge is screaming. Again. Each stage did one small job, and the pipes did the rest.
8. Common errors
| What you see | What it usually means | Fix |
|---|---|---|
| A file you wanted is now tiny or empty | You used > where you meant >> | Check which file > points at. Keep copies of anything that matters |
command not found after a pipe | You put a file name after ` | instead of>` |
uniq removed nothing | The duplicates were not next to each other | sort first |
An error shows on screen despite > | Errors travel separately | Use 2> for the error channel |
Here is the pipe-versus-redirect mix-up, which you will do at least once:
grep ERROR complaints.log | errors.txtbash: errors.txt: command not found
grep: write error: Broken pipeThe terminal tried to run errors.txt as a command, because that is what comes after a pipe. You may also see a second line from grep saying write error: Broken pipe. That is grep complaining that nobody was listening to its output.
Practice
Predict the result before you run each one. Answers follow. Start from a fresh mansion:
bash ~/mansion-setup.sh
cd ~/mansion/kitchenThe mansion is built. Try not to wake anyone.Exercise 1. How many different complaint levels are there? (Not how many complaints: how many different levels.)
Exercise 2. Save the WARN lines into a file called warnings.txt, then count the lines in it.
Exercise 3. Show the two most frequent guest names in ../library/guests.txt.
Answers
Exercise 1. Cut out the level, sort, remove repeats, then count what is left:
cut -d' ' -f3 complaints.log | sort | uniq | wc -l3There are three levels: ERROR, INFO and WARN.
Exercise 2. A redirect to save, then wc -l on the file:
grep WARN complaints.log > warnings.txt
wc -l warnings.txt3 warnings.txtExercise 3. The counting pipe from before, cut off after two lines with head:
sort ../library/guests.txt | uniq -c | sort -rn | head -n 2 3 gerald
2 casperWhat you learned
- A pipe (
|) feeds one command's output into the next command's input. sortorders lines,uniqremoves adjacent duplicates (-ccounts them), andcut -d' ' -f3pulls out a column.sort | uniq -c | sort -rnis the classic recipe for "what is most common?">saves output to a file and replaces the file.>>adds to the end.- Errors travel separately from normal output.
2>redirects them, and/dev/nullthrows them away. - A pipe goes to a command. A redirect goes to a file.
Next in the series: a terminal cheat sheet. One page with every command from the whole series, ready to keep.