- Read long outputs without losing them on the screen
- Save command output for later analysis or auditing
- Capture only error messages
- Chain commands so the output of one becomes the input to another

Both
stdout and stderr go to the terminal by default. Redirection changes those destinations so you can store output in files, pass it to other commands, or separate normal output from errors.
Common redirection examples
- Redirect
stdoutto a file (overwrite):
ls -l to out.txt, replacing any existing contents.
- Redirect
stdoutto a file (append):
stdout to out.txt instead of overwriting.
- Redirect
stderrto a file:
stderr) are written to errors.txt.
- Redirect both
stdoutandstderrto the same file (portable form):
stdout to all-output.txt and then redirects stderr (file descriptor 2) to wherever stdout (1) is currently going.
- Redirect both
stdoutandstderrto the same file (Bash shorthand):

- A pipe (
|) passes thestdoutof the command on its left to thestdinof the command on its right. - If you need both
stdoutandstderrto flow through a pipe, mergestderrintostdoutbefore piping:
- Beware of placement:
some-command | other-command 2>&1will redirectother-command’sstderrto itsstdout, notsome-command’sstderr.

tee utility reads from stdin, writes the data to stdout, and also writes it to files. Use it when you want to monitor output in real time while preserving a copy.
- Write and display:
- Append rather than overwrite:
Remember: the order of redirections matters. To capture both
stdout and stderr into a pipe or file, merge streams using 2>&1 (or use the Bash shorthand &>). If you only redirect stdout, stderr will still appear on the terminal unless you redirect it explicitly.- Use
>/>>to redirect or appendstdoutto files. - Use
2>/2>>to redirect or appendstderr. - Use
2>&1(or&>) to combinestderrandstdoutinto a single destination. - Use pipes (
|) to passstdoutfrom one command to another. - Use
teewhen you must both display output and write it to a file.