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, overwriting
cmd >> out.txtAppend stdout to a file
cmd 2> err.txtRedirect stderr
cmd > out 2>&1Send stderr to the same place as stdout
cmd1 | cmd2Pipe stdout into the next command
cmd < in.txtRead stdin from a file

Try 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?