Linux Redirection and Pipelines: stdin, stdout, stderr, and tee
I composed single-line data pipelines using redirection, pipes, and tee to filter, transform, and log output in one pass. Understanding the three standard streams let me capture stderr separately and split a pipeline to both a file and the screen.
Objective & Context
The Unix philosophy is small tools composed via streams. This lab masters file descriptors 0/1/2, pipe composition, and tee for branching, the glue behind every filter and scripting lab.
Environment & Prerequisites
- A Bash shell with coreutils.
- A command that writes to both stdout and stderr.
- A log destination.
flowchart LR
C[command] -->|stdout| F[filter]
C -->|stderr 2>| E[error log]
F --> T[tee]
T --> Scr[screen]
T --> Log[file]
Step-by-Step Execution
1. Separate stdout and stderr
make 2> build-errors.log 1> build-out.log2. Filter through a pipe
journalctl -u nginx | grep -i error | tail -203. Split output with tee
dmesg | tee dmesg.log | grep -i fail[ 3.21] usb 1-2: device descriptor read/64, error -71
Validation & Testing
Run a command that emits both streams and confirm errors land in their own file while stdout is filtered. Pass criteria: stderr captured separately, the pipeline filters correctly, and tee writes the full stream to a file while passing it onward.
Advanced: Troubleshooting
- Errors not captured: redirect
2>explicitly; pipes only carry stdout by default. - Both streams merged: use
2>&1after the stdout redirect, order matters. - tee not writing: add
-ato append rather than overwrite.
Key Results
- Captured stdout and stderr to separate files in one command.
- Built filter pipelines that reduce noisy logs to relevant lines.
- Branched output to file and screen simultaneously with tee.
- Internalized the three standard streams for reliable scripting.