Skip to content
Codescope

Pipes & redirection

Every process is born with three streams. Redirection just re-plugs them.

core10 min7 steps

Before you start

Every Linux program starts with three connections: **stdin** (an input stream, usually the keyboard), **stdout** (normal output, usually the terminal), and **stderr** (error output, also to the terminal by default).

The shell's superpower is that you can re-plug those connections without the program knowing. > file sends stdout to a file. < file reads stdin from a file. | connects one program's stdout to the next program's stdin.

This lesson watches those three streams get rewired: to a file, to another program, and both at once. That's the whole basis of the Unix pipeline philosophy: small tools composed into big work.

Processes

every process starts with three open streams
cat access.log
0 stdin
terminal
1 stdout
terminal
2 stderr
terminal

cat writes to descriptor 1. Today that happens to be your screen.

Step 1 of 7. Three streams, opened for you. Every process starts with three file descriptors already open: 0 is standard input, 1 is standard output, 2 is standard error. By default all three are connected to your terminal. Nothing in the program knows or cares what is on the other end. That indifference is what makes redirection possible.

01 / 07

Step 01 / 07

Three streams, opened for you

Every process starts with three file descriptors already open: 0 is standard input, 1 is standard output, 2 is standard error. By default all three are connected to your terminal.

Nothing in the program knows or cares what is on the other end. That indifference is what makes redirection possible.

session.shShell
cat access.log                      # stdout to the terminalwc -l < access.log                  # stdin from a filesort names.txt > sorted.txt         # stdout to a fileecho "line" >> sorted.txt           # append instead of truncategrep ERROR access.log | wc -l       # stdout of one becomes stdin of the nextfind / -name "*.conf" 2> /dev/null  # discard stderr onlymake > build.log 2>&1               # both streams to one filecut -d, -f2 data.csv | sort | uniq -c | sort -rn | head -3