Redirection

Controlling input and output streams using redirection operators for file operations and logging

Redirection is a fundamental Linux concept that allows you to control where command input comes from and where output goes. Instead of displaying results on screen, you can send them to files, other commands, or devices. This is crucial for automation, logging, and data processing.

Key Concepts

  • stdin (0): Standard input stream (keyboard)
  • stdout (1): Standard output stream (screen)
  • stderr (2): Standard error stream (screen)
  • File Descriptors: Numeric identifiers for streams
  • Pipes: Connect output of one command to input of another

Command Syntax

command > file - Redirect stdout to file command < file - Redirect stdin from file command >> file - Append stdout to file command 2> file - Redirect stderr to file

Common Options

> - Redirect stdout (overwrite) >> - Redirect stdout (append) < - Redirect stdin 2> - Redirect stderr &> - Redirect both stdout and stderr | - Pipe output to another command

Practical Examples

Example 1: Basic Output Redirection

1
2
ls -l > directory_listing.txt
cat directory_listing.txt

Saves directory listing to file instead of screen

Example 2: Append to File

1
2
echo "New entry" >> logfile.txt
echo "Another entry" >> logfile.txt

Adds content without overwriting existing data

Example 3: Error Redirection

1
find /root -name "*.txt" 2> errors.log

Sends permission errors to file, shows results on screen

Example 4: Redirect Both Streams

1
2
3
command > output.txt 2>&1
# or
command &> all_output.txt

Captures both normal output and errors

Example 5: Input Redirection

1
2
sort < unsorted_list.txt
mail [email protected] < message.txt

Reads input from file instead of keyboard

Example 6: Here Documents

1
2
3
4
cat << EOF > config.txt
Setting1=value1
Setting2=value2
EOF

Creates multi-line input without external file

Use Cases

  • Logging: Save command output for later review
  • Automation: Scripts that process files silently
  • Data Processing: Chain commands together with pipes
  • Error Handling: Separate errors from normal output
  • Batch Operations: Process multiple files efficiently

tee - Write output to both file and stdout cat - Display file contents or concatenate sort - Sort lines of text grep - Search text patterns wc - Count lines, words, characters

Tips & Troubleshooting

Common Issues

  • File Overwrite: Use >> to append, not >
  • Permission Denied: Check write permissions on target
  • Mixed Output: Redirect stderr separately if needed

Best Practices

  • Always redirect errors in scripts: 2>/dev/null
  • Use absolute paths in automated scripts
  • Test redirections with small datasets first
  • Combine with tee to see output AND save it

Advanced Techniques

1
2
3
4
5
6
7
8
# Redirect to multiple files
command | tee file1.txt file2.txt

# Discard output completely
command > /dev/null 2>&1

# Swap stdout and stderr
command 3>&1 1>&2 2>&3

Security Notes

  • Be careful with > - it overwrites without warning
  • Redirect sensitive output to protected directories
  • Use proper file permissions on log files