Redirecting command output in Linux is a powerful feature that helps in logging, debugging, and managing command-line outputs efficiently. Whether you want to save the output to a file, redirect errors separately, or view the output while saving it, Linux provides various redirection operators to achieve this.
Understanding Standard Streams: stdin, stdout, and stderr
Linux follows a structured way to handle input and output using three fundamental data streams:
- Standard Input (stdin) - File Descriptor 0
- Standard Output (stdout) - File Descriptor 1
- Standard Error (stderr) - File Descriptor 2
These streams are the backbone of how Linux interacts with user inputs and outputs, enabling smooth communication between users, programs, and the shell. Understanding them is crucial for command-line redirection, shell scripting, and error handling.
1. What is Standard Input (stdin) - File Descriptor 0?
stdin (standard input) is the default source from which a program reads data. By default, it takes input from the keyboard, but it can be redirected to accept input from a file or another command.
Examples of stdin usage in Linux
- Using
cat
to accept input from stdin:
cat
Note: When you type text and press Enter, cat
echoes it back. Pressing Ctrl+D ends the input session.
- Using stdin redirection (
<
) to read input from a file instead of the keyboard:
cat < filename.txt # This makes cat
read from filename.txt
instead of waiting for user input.
2. What is Standard Output (stdout) - File Descriptor 1?
stdout (standard output) is the default output stream where programs send their results. Normally, stdout displays output on the terminal, but it can be redirected to a file or another command.
Examples of stdout usage in Linux:
- Printing output to the terminal (default behavior):
echo "Hello, World!"
- Redirecting stdout to a file using
>
(overwrite mode):
echo "Logging message" > log.txt
- Appending stdout output using
>>
(append mode):
echo "New entry" >> log.txt # Adds "New entry" to log.txt
without deleting previous content.
3. What is Standard Error (stderr) - File Descriptor 2?
stderr (standard error) is the default stream where error messages are sent. Unlike stdout, stderr is not affected by stdout redirection. This ensures that error messages are visible even when stdout is redirected.
Examples of stderr usage in Linux:
- A command that prints error messages (stderr):
ls non_existent_file
- Redirecting stderr to a file using
2>
(overwrite mode):
ls non_existent_file> error_log.txt
- Appending stderr output using
2>>
(append mode):
ls non_existent_file 2>> error_log.txt
- Redirecting stdout and stderr separately:
command > output.txt 2> error.txt
How Redirection Works in Linux
Redirection allows changing the destination of stdin, stdout, and stderr to control where commands send their output or receive input.
Symbol | Description | Example |
---|
> | Redirects stdout to a file (overwrite) | ls > output.txt |
>> | Redirects stdout to a file (append) | ls >> output.txt |
2> | Redirects stderr to a file (overwrite) | ls non_existent 2> error.txt |
2>> | Redirects stderr to a file (append) | ls non_existent 2>> error.txt |
&> | Redirects both stdout and stderr to the same file | ls &> all_output.txt |
2>&1 | Redirects stderr to stdout (combines both outputs) | command > file 2>&1 |
< | Redirects stdin from a file | sort < data.txt |
<< | Here-document redirection for multi-line input | cat << EOF |
Redirecting Standard Output (stdout) to a File
To redirect standard output (stdout) to a file, the >
operator is used. This command will execute and overwrite the specified file with the output.
command > output.txt # This overwrites output.txt
with the command output
This operator stores the command output in txt fileAppending Output to an Existing File
If you need to append new output to an existing file without overwriting its content, use the >>
operator.
command >> output.txt
Redirecting Standard Error (stderr) to a File
By default, error messages (stderr) are displayed on the terminal. However, if you want to capture only errors, you need to redirect stderr using the 2>
operator.
command 2> error.txt
Appending Errors to an Existing File
If you want to append errors to an existing file rather than overwriting it, use the 2>>
operator.
command 2>> error.txt
Redirecting stdout and stderr to the Same File
Combining standard output (stdout) and standard error (stderr) into a single file is essential when debugging scripts, logging application output, or consolidating command results.
Method 1: Using >
and 2>&1
command > output.txt 2>&1
- The
>
operator redirects stdout (file descriptor 1
) to output.txt
, creating the file if it doesn't exist or overwriting it. - The
2>&1
redirects stderr (file descriptor 2
) to where stdout is currently redirected (output.txt
).
Method 2: Using &>
(Simplified Approach)
command &> output.txt
- The
&>
operator is a shortcut in Bash and Zsh that redirects both stdout and stderr to output.txt
. - This method is not POSIX-compliant, meaning it may not work in older shells like sh.
Appending stdout and stderr to an Existing File
If you want to append the output instead of overwriting:
command >> output.txt 2>&1
- The
>>
operator appends stdout to output.txt
. - The
2>&1
ensures stderr is appended along with stdout.
Use Cases:
- Capturing full logs of a program execution (
bash script.sh > log.txt 2>&1
) - Debugging errors and standard messages together for easier troubleshooting
- Ensuring all output is stored in one place for later analysis
Redirecting stdout and stderr to Separate Files
Separating stdout and stderr helps in structured logging and debugging by keeping regular command output separate from error messages
command > output.txt 2> error.txt
>
sends stdout to output.txt
.2>
sends stderr to error.txt
Using tee
to Redirect and View Output Simultaneously
By default, when you redirect output, you see nothing in the terminal. If you want to see the output while saving it to a file, use the tee
command.
command | tee output.txt
Appending Instead of Overwriting:
command | tee -a output.txt
- The
-a
option appends instead of overwriting output.txt
.
Capturing Both stdout and stderr with tee
:
command 2>&1 | tee output.txt
- The
2>&1
first merges stderr into stdout. - The
tee
command then saves the combined output in output.txt
while also displaying it on the screen.
Advanced Redirection Techniques in Linux
Using exec
to Redirect Output Permanently
The exec
command in Linux allows you to permanently redirect standard output (stdout
) and standard error (stderr
) for an entire script or session. Unlike temporary redirections (>
or >>
), exec
applies redirection to all subsequent commands within the script.
To redirect both stdout
and stderr
to a file for an entire script, use:
exec > output.txt 2>&1
Note: This ensures that every command in the script logs output to output.txt
, making it ideal for logging shell scripts.
Example Use Case
Consider a script that runs a series of operations and logs everything:
#!/bin/bash
exec > log.txt 2>&1 # Redirects all output to log.txt
echo "Starting script execution..."
ls /nonexistentdir # This error will be logged in log.txt
echo "Script completed."
Redirecting Output to /dev/null
(Suppressing Output)
Sometimes, you may want to execute a command without displaying its output. This is useful when running background processes, suppressing unwanted logs, or preventing command-line clutter.This is useful for running commands silently.
command > /dev/null 2>&1
>/dev/null
→ Redirects standard output (normal messages) to /dev/null
, a special null device.2>&1
→ Redirects standard error (error messages) to standard output, which is already redirected to /dev/null
.
Running a system update but discarding all outputsUsing find
with Output Redirection
The find
command in Linux is essential for searching files based on name, size, permissions, or timestamps. When working with find
, redirecting results and errors separately improves usability and debugging.
To search for all .log
files and save results while capturing errors separately use the below command:
find /path -name "*.log" > found_files.txt 2> errors.txt
find /path -name "*.log"
→ Searches for .log
files in the given path.> found_files.txt
→ Saves standard output (matching file names) in found_files.txt
.2> errors.txt
→ Logs errors (e.g., permission issues) in errors.txt
.
Troubleshooting Redirection Issues
- Permission Denied: If you encounter a "Permission Denied" error when redirecting output, it means you lack the necessary write permissions. Use
sudo
- Output Not Showing: Some commands buffer output, causing delays in redirection. If you don't see output immediately:
command | unbuffer tee output.txt
or
stdbuf -oL command > output.txt # use stdbuf
to disable buffering:
- Confused Order: Incorrect redirection order can cause unexpected behavior. Ensure correct sequence (
2>&1
must follow >
).
Conclusion
Efficiently managing stdout and stderr redirection in Linux enhances logging, debugging, and automation. Whether saving output to a file, appending logs, or suppressing errors, mastering redirection operators like >
, >>
, 2>
, &>
, and tee
simplifies command-line workflows.
By using advanced techniques such as exec
for persistent redirection, /dev/null
to discard unwanted output, and find
with redirection for structured logging, you can streamline system administration and scripting.
Similar Reads
How to Redirect Standard (stderr) Error in Bash Bash (Bourne Again SHell) is the scripting language that is generally used to automate tasks. It is a Linux shell and command language written by Brain Fox. Bash is the default shell for Linux and macOS operating systems. What is a Standard Error in Bash?When a code is run in Bash shell and if the c
7 min read
How to Save Output of Command in a File in Linux? Linux has a robust command-line interface (CLI) that makes system interaction efficient for users. Saving a command's output to a file is among the most frequent activities in Linux. When working with big outputs, generating logs, or needing to save command results for later use, this is quite helpf
4 min read
Input Output Redirection in Linux In Linux, whenever an individual runs a command, it can take input, give output, or do both. Redirection helps us redirect these input and output functionalities to the files or folders we want, and we can use special commands or characters to do so. For example, if we run the "date" command, it giv
4 min read
How to Create a Text File Using the Command Line in Linux The Linux command line is a powerful tool that allows you to manage your system with precision and speed. One of the most common tasks is creating a text file, which can be achieved using several commands and methods.Here we will learn some quick ways to create a text file from the Linux Command Lin
6 min read
How to Create a File in the Linux Using the Terminal? In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read