Redirection is a feature in Linux which can be used to change the standard input device (keyboard) or standard output device (screen) during the execution of a command. The basic process of any Linux command is that it takes an input and gives output but the standard/input and output can be changed using the redirection technique.
Standard Output and Standard Error
In Linux, there are two primary output streams:
- Standard Output (stdout): This stream is used for regular output, such as the results of a command or program execution.
- Standard Error (stderr): This stream is used for error messages and diagnostic information.
By default, both stdout and stderr are displayed on the terminal. However, you can redirect these streams using special file descriptors and redirection operators.
Redirecting Standard Error
The redirection operator >
only redirects the standard output (stdout) of a command. To redirect the standard error (stderr), you need to use the file descriptor 2
.
2> stderr
The 2>
operator redirects the standard error (stderr) of a command to a file. This separates error messages from the regular output, providing a cleaner output. The syntax is as follows:
command 2> file
Consider an example:
eccho "gfg" 2>file.txt
echo "gfg" 2>file.txt
There is no command as eccho and hence error message will be displayed. But if 2>file.txt is used, then the error will be redirected to file.txt and no error is displayed on the screen. The cat command can be used to display the content of the file named file.txt which is the error message for the previous command. While executing the second command, gfg is displayed as the echo is a command in Linux to display. Hence, 2> does not redirect standard output.
2>&1
The 2>&1
operator redirects both standard error (stderr) and standard output (stdout) to the same file or stream.
Syntax:
command > file 2>&
Consider an example:
eccho "gfg" >error.txt 2>&1
echo "gfg" >error.txt 2>&1
There is no command as echo and hence the error message is redirected to the file error.txt and no error is displayed. The second command echo "gfg" is correct but still, no output is displayed, as the standard output is also redirected to the file error.txt. The content of the file can be displayed after each step using the cat command.

Note: >& can be used to redirect both standard output and standard error, but it is not supported in all shells. sh and ksh do not support >& while bash and zsh support it.

Conclusion
Redirection in Linux lets you control where command input and output goes. Using different operators like >, >>, and 2>, you can send regular output and error messages to files instead of the screen. This helps keep things neat and organized. Redirection is a handy tool that makes it easier to work with commands and manage output on the Linux command line, whether you're running scripts, troubleshooting issues, or processing data.
Similar Reads
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
Oralyzer : Linux Tool To Identify Open Redirection Open Redirection is the security vulnerability in a web-based application that causes it to fail to properly authenticate URLs. When any web-based application receives requests for URLs, they are supposed to prove that the requested URLs are an integral part of their domain. No other URLs should be
4 min read
Linux Terminal Linux is one of the most powerful operating systems used for development, system administration, cybersecurity, and cloud computing. However, installing Linux on a physical machine is not always convenient. This is where a Linux terminal becomes an ideal solution.What is a Linux Terminal?The Linux t
9 min read
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
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read