Pipes and Redirection
Connect commands and control where their input and output go
Overview
The real power of the shell comes from combining small commands into pipelines. Redirection sends a command's output to a file instead of the screen, and pipes feed one command's output into another's input. Together they let you build complex data flows out of simple tools.
Syntax / Usage
Use > and >> to redirect output, < to redirect input, and | to pipe between commands.
ls > files.txt # write output to a file (overwrites)
ls >> files.txt # append output to a file
sort < files.txt # feed a file in as input
command 2> errors.txt # redirect standard error
command > out.txt 2>&1 # redirect both stdout and stderr
cat access.log | grep "404" | wc -l # pipe: count 404 lines
Examples
Count how many files are in a directory:
ls | wc -l
Save only error lines from a log to a separate file:
grep -i "error" app.log > errors.txt
Find the five largest processes by piping several tools together:
ps aux | sort -rk 4 | head -n 5
Common Mistakes
- Using
>when you meant>>, which silently overwrites the file - Forgetting that errors go to stderr and aren't captured by a plain
> - Piping into a command that reads arguments, not stdin (use
xargsinstead) - Assuming pipeline stages run one after another — they run concurrently
- Redirecting to the same file you're reading from, which can truncate it
See Also
command-line-text-processing command-line-processes