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
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:
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 >:
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:
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
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.
Done with this one?
Skipped the challenge? You can mark the lesson complete by hand.