Redirection Command Guide: Sending Output Where You Want It
Redirection is the shell’s ability to reroute a command’s input and output away from the terminal and into files, other commands, or the void — and it’s what turns a collection of small tools into a working pipeline.
What is Redirection?
By default, a command reads its input from the keyboard (stdin) and writes its results to the terminal (stdout). Redirection lets you rewrite those connections: output can go to a file instead of the screen, input can come from a file instead of the keyboard, and errors can be sent somewhere other than where results go.
Every redirection operator in this guide acts on one of the three streams covered in stdin, stdout, and stderr: 0 (stdin), 1 (stdout), 2 (stderr).
Why Learn Redirection?
- You stop losing output. Terminal scrollback is temporary; a file is permanent. Anything you might want to re-read gets saved.
- You stop contaminating results. Separating errors from data keeps files clean and makes scripts reliable.
- It’s the glue of the shell. Redirection plus pipes (
|) is the entire mechanism behind command chaining — no other skill unlocks more of Linux. - Logs and backups start here.
command >> log.txtis the simplest logging there is.
Redirection answers the question “where does this command’s output go?” — and once you can answer it for every stream, you can wire any number of commands together.
1. > — Redirect stdout to a File (Overwrite)
The most common operator. command > file sends standard output to file, replacing whatever was there:
ls -la > listing.txt
The terminal now shows nothing; the directory listing is in listing.txt. If listing.txt already existed, its old contents are gone.
To append instead of overwrite, use >>:
echo "Backup finished at $(date)" >> backup.log
>> adds to the end of the file, creating it if it doesn’t exist. This is the classic way to keep a running log.
2. 2> — Redirect stderr to a File
Errors travel on file descriptor 2, so a normal > won’t catch them:
find / -name "*.conf" 2> find-errors.txt
The found paths print to the screen; every permission complaint goes to find-errors.txt. To append errors instead:
find / -name "*.conf" 2>> find-errors.txt
3. < — Redirect stdin from a File
The mirror image of >. Instead of typing input on the keyboard, the command reads it from a file:
wc -l < notes.txt
wc -l normally counts lines it’s given; here it’s given the file itself. This is handy when a command only accepts input one way and you want to test it quickly.
4. &> — Redirect Both stdout and stderr
Bash’s shorthand for “send everything to one place”:
make > build.log 2>&1
# or the short form:
make &> build.log
Both produce the same result: results and errors together in build.log. Use the long form (> file 2>&1) in scripts you want to run under other shells; use &> when you’re certain you’re in bash.
5. tee — Write to a File and the Screen
Plain redirection forces a choice: output goes to the file or to the screen. tee splits the stream and sends it to both:
./build.sh | tee build.log
The build output appears in your terminal and lands in build.log. Append with -a:
./deploy.sh | tee -a deploy.log
A common rescue trick — redirect output but keep watching by piping through tee:
noisy-command | tee output.txt | grep -i error
6. /dev/null — The Black Hole
/dev/null is a special file that swallows everything written to it and reports nothing back. Redirecting output there discards it:
./noisy-script.sh > /dev/null 2>&1
Everything is silenced — useful for cron jobs and background tasks where you don’t want spam, and for “check if this command exists without printing errors”:
command -v ffmpeg > /dev/null 2>&1 && echo "installed"
7. Redirecting Inside Pipelines
Once you’re chaining commands with pipes (see Pipes), redirection still applies — at the end of the chain:
grep -r "TODO" src/ | sort | uniq -c > todo-report.txt
The pipeline runs, and only the final result is saved. You can also redirect individual stages:
cat access.log 2> parse-errors.txt | awk '{print $1}' | sort | uniq -c > top-visitors.txt
parse-errors.txt catches anything cat complained about; top-visitors.txt gets the final tally.
Common Pitfalls
>silently destroys data. There’s no confirmation. Before runningcommand > important.txt, check whetherimportant.txtalready exists — or useset -o noclobberin bash to make>refuse to overwrite.- Forgetting that
>only affects stdout. Errors still hit the screen. Add2> err.txtor&>when you want total capture. - The
2>&1 > filetrap. Redirections apply left to right.2>&1 > filesends stderr to the terminal and stdout to the file — write> file 2>&1to capture both. - Appending when you meant to replace.
>>grows the file forever; a stale log can confuse later analysis. Decide per use whether you want truncate or append. - Expecting pipes and redirection to be interchangeable.
cmd > filesaves output;cmd | otherfeeds it into a command. You need both for the full picture — see Pipes.
Related Pages
| Page | Why You’d Read It Next |
|---|---|
| stdin, stdout, and stderr | The three streams every redirection operator acts on. |
| Pipes | Feeding one command’s stdout directly into another’s stdin. |
| tee is part of coreutils | The split-stream tool, covered in depth by its own manual. |
| head and tail | Saving and slicing stream fragments. |
Redirection Cheatsheet
| Task | Command |
|---|---|
| Send stdout to a file (overwrite) | command > file.txt |
| Append stdout to a file | command >> file.txt |
| Send stderr to a file | command 2> err.txt |
| Append stderr | command 2>> err.txt |
| Send stdin from a file | command < file.txt |
| Both streams to one file | command > all.log 2>&1 (bash: command &> all.log) |
| Discard everything | command > /dev/null 2>&1 |
| Output to screen and file | command | tee file.txt |
| Screen and file, appending | command | tee -a file.txt |
| Prevent accidental overwrite | set -o noclobber |