1. Definition / Conclusion

In Linux, every program fundamentally operates with three I/O streams.
stdin (0) is input, stdout (1) is standard output, and stderr (2) is error output.
Redirection is the mechanism that changes where these streams are sent.

2. Key Summary

stdout is the channel for normal results, while stderr is the channel for error messages.
Because they are separated, results and errors can be handled independently.
Redirection allows these streams to be sent to files, other processes, or discard targets.

3. Why It Matters

If a program had only a single output stream, normal results and error messages would be mixed together.
In such a case, saving output to a file would also include error messages, which can break downstream processing or parsing.

For example, imagine saving command output to a file.
If error messages are included, the next step that consumes this file may fail unexpectedly.
This problem arises because all output is combined into a single channel.

Linux solves this by separating output into two streams.
stdout carries only valid data, while stderr carries only error messages.
This separation allows you to store only results, collect only errors, or combine them when needed.

Redirection is the mechanism that controls where these separated streams go.
In other words, the output structure exists first, and redirection operates on top of it.

The key point is not controlling the content, but controlling the destination of output.

4. Examples

Example 1: Save stdout to a file

echo "hello" > output.txt

→ Result: hello is written to output.txt
→ Reason: > redirects stdout (fd 1) to a file
→ Practical use: Commonly used to store command results

Example 2: Save only stderr

ls not_exist 2> error.txt

→ Result: Error message is written to error.txt
→ Reason: 2> redirects stderr (fd 2)
→ Practical use: Collect only error logs

Example 3: Save both stdout and stderr

ls file1 not_exist > all.txt 2>&1

→ Result: Both normal output and errors are saved in all.txt
→ Reason: stderr is redirected to the same destination as stdout
→ Practical use: Store full execution logs in a single file

Example 4: Discard all output

ls not_exist > /dev/null 2>&1

→ Result: No output is produced
→ Reason: Both stdout and stderr are sent to the null device
→ Practical use: Suppress unnecessary logs

5. Tools That Operate on This Structure

On top of this output model, the following tools operate:

  • > : Redirect stdout to a file
  • 2> : Redirect stderr to a file
  • >> : Append output to an existing file
  • 2>&1 : Redirect stderr to the same destination as stdout
  • /dev/null : Discard output
  • | : Pipe stdout into another process’s stdin

All of these share the same purpose: controlling output flow.

Each concept can be explored further:

6. Practical Usage

1) Separating logs in batch jobs

Situation: Running a batch script
Problem: Mixed logs make debugging difficult
Solution: Separate stdout and stderr into different files
Effect: Faster error analysis

command > success.log 2> error.log

2) Storing unified logs

Situation: Need a single log file
Problem: Output is split into two streams
Solution: Merge stderr into stdout
Effect: Easier log management

command > app.log 2>&1

3) Removing unnecessary logs

Situation: Repeated scripts generate excessive logs
Problem: Disk usage and noise
Solution: Discard all output
Effect: Reduced logging overhead

command > /dev/null 2>&1

7. Common Mistakes

1) Incorrect order

command 2>&1 > file.txt

→ Result: stderr goes to terminal, stdout goes to file
→ Reason: Redirection is applied from left to right
→ Fix:

command > file.txt 2>&1

2) stderr not included

command > file.txt

→ Result: Errors still appear on the terminal
→ Reason: Only stdout was redirected
→ Fix: Explicitly handle stderr

3) Misunderstanding >>

command >> file.txt

→ Result: Output is appended
→ Reason: This is append mode
→ Fix: Use > to overwrite

4) Misusing /dev/null

command > /dev/null

→ Result: Only stdout is discarded, errors remain
→ Reason: stderr is a separate stream
→ Fix:

command > /dev/null 2>&1
  • File descriptors (fd): Input/output channels 0, 1, 2
  • Pipe (|): Data transfer between processes
  • /dev/null: Special file that discards data
  • “Everything is a file”: I/O abstraction in Unix

9. Deeper Understanding

In Linux, stdout and stderr are implemented as file descriptors.
Each process starts with three default descriptors: 0, 1, and 2.

This design comes from the abstraction that treats all I/O as files.
Terminals, files, and network sockets are all handled through the same interface.
This is why changing output destinations can be done with simple syntax.

Redirection works by reassigning file descriptors to different targets.
It is not a new feature, but a rearrangement of the existing I/O structure.
This design enables small tools to be combined into powerful pipelines.

10. Summary

Linux separates output into stdout and stderr.
Redirection controls where these streams are sent.
2>&1 redirects stderr to the same destination as stdout.
Understanding this structure simplifies log handling and pipeline design.