Skip to content
Beginner LinuxLesson 5 of 27 2 min read

Pipes and Redirection

Chain commands with pipes, and send output to files with > and >>.

On this page5 sections

Every command you run has three data streams attached to it:

  • stdin (standard input) - where it reads input from; by default, your keyboard.
  • stdout (standard output) - where it writes results; by default, your screen.
  • stderr (standard error) - where it writes error messages; also your screen, by default.

Two shell features let you rewire those streams: redirection (send a stream to or from a file) and pipes (send one command's stdout straight into the next command's stdin).

Redirecting output to a file

bash
ls > listing.txt          # overwrite listing.txt with the output of ls
echo "hello" > note.txt   # overwrite note.txt with "hello"

> always overwrites the target file, creating it if it does not exist. Use >> to append instead of overwriting:

bash
echo "first line" > log.txt
echo "second line" >> log.txt
cat log.txt

Redirecting input from a file

< feeds a file's contents in as a command's stdin - the opposite direction from >:

bash
wc -l < log.txt          # count the lines in log.txt

Pipes: connecting commands

A pipe (|) takes the stdout of the command on its left and feeds it in as the stdin of the command on its right. Instead of one giant command, you chain small, single-purpose tools together:

bash
ps aux | grep root       # only the process lines mentioning "root"
ls | wc -l               # count how many entries are in the current directory
history | grep cd        # find cd commands you've already run

You can chain more than two: cat access.log | grep ERROR | wc -l counts how many lines mention "ERROR".

The practice terminal keeps things approachable and does not separately model stderr (2>) - everything a command prints goes through the same stream you see redirected with >.

Try it

Run these in the practice terminal (opens in a new tab). It opens in a new tab, so you keep your place here.
bash
echo "queued" > status.txt
cat status.txt
echo "shipped" >> status.txt
cat status.txt
wc -l < status.txt

Further reading

  • "The Linux Command Line" by William Shotts (No Starch Press) - its chapter on redirection is the clearest treatment of this topic you'll find, and covers file descriptors and 2> in full.
  • "How Linux Works" by Brian Ward (No Starch Press) - explains the same ideas from the kernel's point of view, including how file descriptors actually work under the hood.

Challenge

Write the text deploy ready into a new file called status.txt using a single redirected command.

Want to experiment first? Try things in the practice terminal (opens in a new tab), then enter the command that solves the task here.

Quiz

Lock in the concepts. Score at least 80% to mark the lesson complete.

0 of 2 answered

Done with this one?

Skipped the challenge? You can mark the lesson complete by hand.

Your notes

Kept on this browser only.