Redirect Command

In the realm of Linux and Unix-based systems, the command line serves as the primary interface for system administration, development, and automation. By default, most commands operate within a predictable cycle: they read input from the keyboard, process it, and display the results—both successful outputs and error messages—directly onto the terminal screen.

However, in professional environments involving complex workflows, automated scripts, or massive log files, this default behavior is often insufficient. To manage data effectively, you need to control where information comes from and where it goes. This is where redirection comes into play. Redirection allows you to divert data streams, saving outputs to files, feeding file contents into commands, or isolating error logs for easier debugging.
Before mastering redirection operators, it is essential to understand the concept of Standard Streams. In Linux, every process is associated with three data streams, each identified by a unique integer known as a File Descriptor (FD):

  1. Standard Input (stdin - FD 0): The stream used to provide data to a command. By default, this is your keyboard.
  2. Standard Output (stdout - FD 1): The stream where a command sends its successful results. By default, this is your terminal screen.
  3. Standard Error (stderr - FD 2): A separate stream used specifically for error messages and diagnostic data. By default, this also prints to your terminal screen.

Redirection is essentially the act of changing the destination or source of these file descriptors.

Output Redirection: Capturing Results

Output redirection is used to intercept the results of a command and write them to a file instead of letting them clutter the terminal.

Overwriting Output with >

The > operator redirects the Standard Output (stdout) to a specified file. It is important to note that if the target file already exists, the > operator will overwrite its entire content, effectively wiping the old data. If the file does not exist, the system will create it.

# Redirect the output of the echo command to file.txt (overwrites existing content)
echo "Hello, Linux" > file.txt

Appending Output with >>

If your goal is to preserve existing data and simply add new information to the end of a file, use the >> operator. This is particularly useful for maintaining continuous logs.

# Append a new line to the end of file.txt without deleting previous content
echo "Welcome to the world of redirection." >> file.txt

Error Redirection and Stream Merging

One of the most powerful aspects of Linux is the ability to separate "what went right" from "what went wrong." This is achieved by targeting the Standard Error (stderr) stream specifically.

Redirecting Error Messages with 2>

Since error messages are sent to file descriptor 2, you can redirect them independently of the standard output using the 2> syntax. This allows you to keep your main output files clean while capturing errors in a dedicated log.

# Attempt to list a non-existent file and redirect the error to error.log
ls non_existent_file.txt 2> error.log

Merging Stdout and Stderr

In many scenarios, such as debugging a complex script, you may want to capture both the successful output and the error messages in a single file. There are two primary ways to achieve this:

  • The Classic Method: command > output.log 2>&1
    This tells the shell to redirect stdout to output.log, and then redirect stderr (2) to the same location currently held by stdout (&1).
  • The Modern Shortcut: command &> output.log
    In modern shells like Bash (version 4.0+), this concise syntax redirects both stdout and stderr to the specified file simultaneously.
# Capture all output (both success and errors) into all.log
find / -name "config" &> all.log

Input Redirection: Feeding Data to Commands

While output redirection moves data out of a command, input redirection moves data into a command, replacing the keyboard as the source.

Reading from Files with <

The < operator tells a command to read its input from a file rather than waiting for user interaction via the terminal.

# Use the wc (word count) command to process the contents of file.txt
wc -l < file.txt

Multi-line Input via Here Documents (<<)

A Here Document (often called a "heredoc") is a specialized form of input redirection. It allows you to pass multiple lines of text directly into a command within a script or a terminal session. The input continues until the shell encounters a specific "delimiter" word (commonly EOF).

# Use a heredoc to create a multi-line file named summary.txt
cat << EOF > summary.txt
This is the first line.
This is the second line.
EOF

Redirection vs. Pipes: Knowing the Difference

A common point of confusion for beginners is the distinction between redirection and piping. While both involve moving data, their purposes are distinct:

  • Redirection (>, <, >>) connects a command to a file. It moves data between a process and the filesystem.
  • Pipes (|) connect a command to another command. It takes the standard output of the first command and feeds it directly as the standard input to the second command, allowing for complex data processing chains.

You can, of course, combine them to create highly efficient workflows:

# Use a pipe to filter text, then use redirection to save the result
cat system.log | grep "error" > filtered_errors.txt

In this example, cat reads the file, the pipe | passes the data to grep for filtering, and the redirection > saves the final filtered list to a new file.

Conclusion

Mastering redirection is a transformative step in a Linux user's journey. It moves you from being a passive observer of command outputs to an active architect of data flows. Whether you are managing system logs, automating repetitive tasks, or building complex data processing pipelines, understanding how to manipulate stdin, stdout, and stderr is essential for efficient and professional system administration.