Pipes and Redirection
Chain commands together with | and control where output goes with > and >>.
What You'll Learn
- Pipe operator (|) to chain commands
- Output redirection (> and >>)
- stdin, stdout, stderr streams
- tee, here documents, and /dev/null
The Pipe Operator |
The pipe (|) is the most powerful concept in Unix. It takes the output of one command and feeds it as input to the next command.
๐ง Real-World Analogy: Think of pipes like a factory assembly line. Each worker (command) performs one specialised task and passes the result to the next worker. No single worker does everything โ they chain together to produce the final product.
# Count files in current directory $ ls | wc -l # Find all .tsx files $ ls -R | grep '.tsx' # Top 10 most frequent errors $ cat server.log | grep ERROR | sort | uniq -c | sort -rn | head -10 # Find largest files $ du -sh * | sort -rh | head -5
๐ก Pro Tip โ Unix Philosophy
"Do one thing and do it well." Each Unix command is simple on its own, but when piped together, they become incredibly powerful. This is the Unix philosophy that makes the command line so effective.
Pipes
Chain commands together to filter and process data.
// Pipes โ Chain Commands Together
console.log("=== The Pipe Operator | ===");
console.log("Takes OUTPUT of one command and feeds it as INPUT to the next");
console.log();
// Simulated pipeline
const files = [
"README.md", "package.json", "tsconfig.json",
"src/App.tsx", "src/main.tsx", "src/index.css",
"src/components/Header.tsx", "src/components/Footer.tsx",
"src/pages/Home.tsx", "src/pages/About.tsx",
"public/index.html", "public/favicon.ico",
];
console.log("$ ls -la | wc -l
...Redirection
Redirection controls where a command's input comes from and where its output goes:
# Output redirection $ echo "Hello" > file.txt # Write (overwrites!) $ echo "World" >> file.txt # Append # Error redirection $ command 2> errors.log # Errors to file $ command > output.log 2>&1 # Both stdout + stderr to file $ command &> all.log # Shorthand for both # Discard output $ command > /dev/null 2>&1 # Silence everything
โ ๏ธ Common Mistake
Using > when you mean >>. A single > overwrites the file completely! Use >> to append. This is a common cause of data loss.
Redirection
Control where command output goes.
// Redirection โ Control Input/Output
console.log("=== Output Redirection ===");
console.log();
console.log("$ echo 'Hello' > file.txt (overwrite file)");
console.log("$ echo 'World' >> file.txt (append to file)");
console.log();
console.log("=== Standard Streams ===");
console.log("Every command has 3 streams:");
console.log(" stdin (0) โ input (keyboard by default)");
console.log(" stdout (1) โ output (screen by default)");
console.log(" stderr (2) โ errors (screen by default)");
...๐ Quick Reference
| Operator | Description |
|---|---|
| cmd1 | cmd2 | Pipe output of cmd1 to cmd2 |
| > file | Redirect output (overwrite) |
| >> file | Redirect output (append) |
| 2> file | Redirect errors to file |
| &> file | Redirect all output to file |
| | tee file | Output to screen AND file |
๐ Lesson Complete!
You've mastered pipes and redirection โ the glue of Unix! Next: shell scripting to automate everything you've learned.
Sign up for free to track which lessons you've completed and get learning reminders.