Pipes & Redirection
Intermediate
Overview
Every command has three standard streams: stdin (0), stdout (1), and stderr (2). Redirection lets you send these to files instead of the terminal.
Use > to overwrite a file, >> to append, and 2> to capture errors. The pipe | sends one command's stdout into the next command's stdin — the heart of the Unix philosophy.
Combine them to build powerful one-liners, like filtering, counting, and saving results in a single line.
Cheatsheet
cmd > out.txtRedirect stdout, overwritingcmd >> out.txtAppend stdout to a filecmd 2> err.txtRedirect stderrcmd > out 2>&1Send stderr to the same place as stdoutcmd1 | cmd2Pipe stdout into the next commandcmd < in.txtRead stdin from a fileTry it
A safe, simulated terminal. Run the suggested commands to see typical output.
simulated terminal
Type a command and press Enter, or click a suggestion below to run it.
Quick quiz
1. What does the pipe | do?
2. Which redirection appends to a file instead of overwriting it?