# Pipes and redirection until they feel obvious

> Drill |, >, >>, 2>, 2>&1, and < inside a throwaway directory, plus the GNU vs BSD sed -i trap.

Source: https://fullbytedeveloper.com/blog/pipes-redirection-until-obvious
Published here: 2026-11-21, updated 2026-12-03

A pipe (`|`) sends one command's stdout into the next command's stdin [1]. `>` writes stdout to a file (overwrite), `>>` appends, `2>` catches stderr, `2>&1` merges stderr into stdout's current destination, and `<` feeds a file into stdin [2][4]. Drill them in a throwaway directory so you never overwrite a real config.

## Quick start: create a throwaway demo directory

Always practice in a temp folder. Do not redirect into `~/.bashrc`, `/etc`, or your project tree:

```bash
DEMO=$(mktemp -d /tmp/pipes-demo-XXXX)
cd "$DEMO"
printf 'gamma\nalpha\nbeta\n' > words.txt
```

Captured on this host: a new directory under `/tmp` with a three-line `words.txt`. Cleanup later with `rm -rf "$DEMO"`.

## Pipe stdout into the next command

Bash pipelines connect commands left to right [1]. Sort the words file:

```bash
cat words.txt | sort
```

Captured output:

```text
alpha
beta
gamma
```

`sort` never saw the path; it only read stdin that the pipe fed. Prefer `sort words.txt` when one command can open the file itself. Use a pipe when you are chaining filters that only speak stdin and stdout [1][6].

## Overwrite and append with > and >>

```bash
echo hello > out.txt
echo world >> out.txt
cat out.txt
```

Captured output:

```text
hello
world
```

The first redirect created or truncated `out.txt`. The second appended. Run `>` twice by accident and the second run wipes the first. That is why drills live under `/tmp` [2].

## Capture stderr with 2>

File descriptor 2 is stderr. Teaching comics and the Bash redirections chapter treat 0/1/2 as stdin/stdout/stderr [2][4]:

```bash
ls /no/such/path 2> err.txt
cat err.txt
```

Captured `err.txt` on this host:

```text
ls: cannot access '/no/such/path': No such file or directory
```

The terminal stayed quiet for that failing `ls` because stderr went to the file. Without `2>`, the same message would have printed on your screen and never landed in a log you can grep later.

## Merge stderr into stdout with 2>&1

```bash
ls /no/such/path > both.txt 2>&1
cat both.txt
```

Captured `both.txt`:

```text
ls: cannot access '/no/such/path': No such file or directory
```

Put `2>&1` after the `>` so stderr follows stdout into that file [2][4]. Reverse the order (`2>&1 > both.txt`) and stderr still points at the old stdout (your terminal) while only stdout goes to the file. Order is the whole lesson.

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 280" role="img" aria-label="Dark diagram: command A stdout box pipes into a pipe box then into a command or file box labeled with greater-than, append, stderr, merge, and stdin operators, plus a footer on file descriptors 0 1 2 and 2>&1 order, no people.">
  <defs>
    <marker id="pipesArrow" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="280" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">six operators in one drill</text>
  <rect x="24" y="56" width="140" height="64" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="40" y="84" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">cmd A stdout</text>
  <text x="40" y="104" fill="#14b8a6" font-family="ui-monospace, monospace" font-size="14">fd 1</text>
  <path d="M172 88 H220" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pipesArrow)"/>
  <rect x="228" y="56" width="140" height="64" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="244" y="84" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">pipe |</text>
  <text x="244" y="104" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">to next stdin</text>
  <path d="M376 88 H424" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pipesArrow)"/>
  <rect x="432" y="56" width="260" height="64" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="448" y="84" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">cmd B / file / next</text>
  <text x="448" y="104" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">&gt; &gt;&gt; 2&gt; 2&gt;&amp;1 &lt;</text>
  <rect x="24" y="148" width="672" height="104" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="40" y="184" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">0 = stdin, 1 = stdout, 2 = stderr</text>
  <text x="40" y="212" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">2&gt;&amp;1 copies stderr onto wherever stdout points right now</text>
  <text x="40" y="236" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">put 2&gt;&amp;1 after &gt; when both streams must land in one file [2]</text>
</svg>
</figure>

*A pipe sends command A stdout into the next stdin. Greater-than, append, and stderr redirects attach streams to files instead. Merge with 2>&1 only after the stdout redirect so descriptor 2 follows descriptor 1 into that path; reverse the order and stderr still hits the terminal.*

## Feed a file on stdin with <

```bash
wc -l < words.txt
```

Captured output:

```text
3
```

`wc` never opened the path as an argument; the shell attached the file to stdin [2]. That matters when a tool only reads stdin, or when you want the command's argv free of the filename.

## Label GNU vs BSD sed -i before you copy it

**Warning:** in-place `sed` edits files. Stay inside `$DEMO`.

FreeBSD sed documents an empty backup suffix for in-place edits [3]:

```bash
sed -i '' -e 's/alpha/ALPHA/' words.txt
```

GNU sed on Linux commonly uses:

```bash
sed -i -e 's/alpha/ALPHA/' words.txt
```

Captured on this GNU sed host after rewriting a copy of the demo file:

```text
gamma
ALPHA
beta
```

On macOS, prefer the FreeBSD form from the manpage [3]. Prefer `printf` over `echo -e` when you care about portability. Paste the wrong `-i` form across OS lines and you get a backup file you did not want, or a syntax error mid-edit.

## Why a live pipe can look stuck

Many programs block-buffer when stdout is a pipe or file, so `cmd | grep` can look frozen until a buffer fills. Julia Evans documents this buffering gotcha [5]. Line-buffering flags or `stdbuf` help in some toolchains; check the tool you are using rather than inventing a universal buffer size. If the left side is still computing and the right side has printed nothing, wait or force line buffering before you kill the SSH session.

## Mistakes that burn an hour

Redirecting into a real config while "just testing" `>`. Use `$DEMO` first.

Writing `2>&1 > file` and wondering why errors still hit the terminal. Swap to `> file 2>&1` [2].

Treating `|` as a way to open a file for one program that already accepts a path. Prefer the path argument when it exists.

Copying `sed -i` from a Linux gist onto macOS without the empty suffix [3].

Blaming the network when a pipe is only waiting on a full stdout buffer [5].

## Depth: pipelines versus redirections

A pipe connects two processes. Redirection attaches a process to a file or to another descriptor [1][2][6]. You can combine them: `cmd1 2>err.txt | cmd2 >out.txt` keeps stderr of `cmd1` in `err.txt` while `cmd2` writes the filtered stdout. Read the line left to right the way the shell does: set up redirections for each command, then connect the pipe between them.

When you need process lists and job control around the same pipe, continue with [top terminal commands part 2](/blog/essential-linux-commands-developers-part-2). For the navigation and search set that comes before pipes, start at [essential Linux commands part 1](/blog/essential-linux-commands-developers-part-1). For which modern CLI replaces a classic filter, see [CLI tools matched to jobs](/blog/cli-tools-matched-to-jobs).

## Prove one compound line

Inside `$DEMO`, rebuild a single line that uses several operators so muscle memory sticks:

```bash
printf 'z\na\nm\n' > raw.txt
sort < raw.txt > sorted.txt 2> sort-err.txt
cat sorted.txt
wc -l < sorted.txt
```

You should see three sorted letters in `sorted.txt` and a line count of `3`. `sort-err.txt` should be empty on a healthy run. If `sort-err.txt` grows, read it before you chase the empty `sorted.txt`.

That compound line is the whole skill: stdin from a file, stdout to another file, stderr parked separately, no pipe required. Add `| uniq` only when a second process must filter the stream [1][2].

## File descriptors without the scare story

You do not need to memorize every number. Remember three: `0` stdin, `1` stdout, `2` stderr [2][4]. `2>&1` means "point 2 at whatever 1 currently points at." After `> both.txt`, descriptor 1 points at `both.txt`, so `2>&1` sends stderr there too. Before any `>`, descriptor 1 is still the terminal, so a leading `2>&1` only duplicates the terminal.

Named pipes (FIFOs) exist for cases where two processes need a pipe that is also a path on disk [6]. Skip them until the six operators above are automatic. Most day-to-day shell work never needs a FIFO.

## Append logs without wiping last night's run

Ops scripts often need "add a line, keep history." That is `>>`, not `>`. A one-line pattern inside `$DEMO`:

```bash
date >> run.log
echo checkpoint >> run.log
wc -l < run.log
```

Each run grows `run.log`. Swap to `>` only when you intentionally want a fresh file. Pair append with a separate `2>> run.err` when you want errors in another growing file and stdout clean in the log [2].

Here-documents (`<<EOF`) feed multi-line stdin without a temp file. They are still redirection, just with the body inline in the script. Learn the six operators first; add here-docs once `>` and `<` feel boring.

## Pipeline exit status

By default bash reports the exit status of the last command in a pipeline [1]. A failing left side can hide behind a successful right side. `set -o pipefail` changes that so a failure earlier in the pipe surfaces. Part 2 of the terminal series shows a short `pipefail` demo when you are ready for job control around the same idea: [top terminal commands part 2](/blog/essential-linux-commands-developers-part-2).

Until then, check the file you redirected into. An empty `out.txt` plus a loud `err.txt` is a clearer signal than a pipeline status you forgot to enable.

## Depth: six operators in one compound line

Build the drill under `/tmp/pipes-demo` so you never aim redirects at a real project tree. Create `a.txt` with three short lines, then practice each operator alone before you combine them. Read the man pages for your shell's redirection rules when the host is macOS or FreeBSD, because BSD `sed -i` and some builtins diverge from the Linux paste you copied [2][3].

`cat a.txt | wc -l` moves stdout into the next stdin. `cat a.txt > out.txt` replaces `out.txt`. `cat a.txt >> out.txt` appends. `cat missing 2> err.txt` parks stderr alone. `cat missing > out.txt 2>&1` merges stderr into the already redirected stdout file; reverse that order and stderr still hits the terminal while stdout alone fills the file. `wc -l < a.txt` feeds stdin from a path without a left-hand producer.

Order is the trap that burns production logs: open the target file with `>` or `>>` first, then attach `2>&1` so descriptor 2 follows descriptor 1 to that path. Confirm with `ls -l out.txt err.txt` and `cat` both files after each attempt. When a long pipeline looks empty while CPU is busy, suspect block buffering on the producer; force line buffering where your libc supports `stdbuf`, or print with an explicit flush, then re-run the same drill until the consumer prints line by line.

A second trap is discarding stderr while debugging. Keep `2> err.txt` on a named file during the drill, then open it before you declare the pipeline healthy. A third trap is writing into the current directory by habit. Stay inside `/tmp/pipes-demo` until the six operators are muscle memory, then move the pattern into a real repo with a dry-run path.

Keep this page on the six operators and file descriptors 0, 1, and 2. Broader find and grep drills stay on [essential Linux commands part 1](/blog/essential-linux-commands-developers-part-1). Process and job control stay on [part 2](/blog/essential-linux-commands-developers-part-2). Tool picking for search stays on [CLI tools matched to jobs](/blog/cli-tools-matched-to-jobs).

## Depth: why `2>&1` order is not a meme

Descriptors are numbers the process already owns. When you write `> out.txt`, the shell opens that path and points descriptor 1 at it. When you then write `2>&1`, descriptor 2 is pointed at whatever descriptor 1 currently targets. If you reverse the tokens, descriptor 2 still points at the terminal, then descriptor 1 alone moves to the file. That is why the "wrong order" demo is worth running once with your eyes on both files [1][2].

After the drill, delete `/tmp/pipes-demo` or leave it as a throwaway lab. Do not promote the folder into a project root. The skill is reading streams, not collecting sample text files.

Browse the [Dev explainers topic](https://fullbytedeveloper.com/blog/topic/dev-explainers).

Send this to the friend who still guesses what 2>&1 means every time they copy it.

Image: A horizontal teal pipeline of tubular connectors joining boxy command chambers, with a side amber overflow pipe dumping into a flat file slab and a second input slab feeding the first chamber from the left, shown on a dark navy grid with no typography or glyphs.
https://fullbytedeveloper.com/media/pipes-redirection-until-obvious/pipes-redirection-until-obvious-venice.webp
A pipe carries a stream to the next command. Redirection sends that stream into a file.

## Sources

1. [Bash manual: Pipelines, accessed 2026-11-20](https://www.gnu.org/software/bash/manual/html_node/Pipelines.html)
2. [Bash manual: Redirections, accessed 2026-11-20](https://www.gnu.org/software/bash/manual/html_node/Redirections.html)
3. [FreeBSD sed(1), accessed 2026-11-20](https://man.freebsd.org/cgi/man.cgi?query=sed&sektion=1)
4. [Julia Evans: bash redirects comic, accessed 2026-11-20](https://wizardzines.com/comics/redirects/)
5. [Julia Evans: Why pipes sometimes get stuck (buffering), accessed 2026-11-20](https://jvns.ca/blog/2024/11/29/why-pipes-get-stuck-buffering/)
6. [Linux pipe(7), man7.org, related reading](https://man7.org/linux/man-pages/man7/pipe.7.html)
