This tutorial explains the input/output redirection in Linux. Learn what the I/O redirection is and how it works in Linux through examples.
When we open a file, start a program or process or command Linux automatically assigns three communication channels to it. These channels are STDIN, STDOUT, and STDERR. These channels allow the opened file or the started program or process or command to interact with other files, programs, processes, or devices of the Linux system.
STDIN allows the opened file or process to receive data from other files, processes, and devices while STDOUT and STDERR allow it to send the processed data and errors to other files, processes, and devices, respectively.
The keyboard is the default STDIN device. The monitor is the default STDOUT and STDERR device. This means that by default STDIN reads from the keyboard and both STDOUT and STDERR write their output to the monitor screen.
Instead of using default devices, if a process or a file wants to use other files or processes to read the input data or to send the processed output data, it can use I/O redirection. I/O redirection is a shell feature that allows an opened file or process to override default devices when using the communication channels.
In the following section, we will understand how I/O redirection works through examples.
STDIN, STDOUT, and STDERR are data streams. Linux uses these streams to transfer data between its components, processes, and devices. To learn the basic concepts of these streams, you can check the following tutorial.
I/O (Input / Output) Redirection
For I/O redirection, the shell assigns three symbols <,> and >>. The < symbol is used to override the STDIN while the > and >> symbols are used to override the STDOUT and STDERR. If the > symbol is used, the shell sends the output in the overwrite mode while if the >> symbol is used, the shell sends the output in the append mode.
Let's take some examples to understand how I / O redirection works.
Access the shell prompt and run the cat command without specifying any file name.
$cat
The cat command copies input data from the specified file and pastes that data on the default STDOUT device. In simple words, it reads the specified file and displays the contents of the specified file on the monitor (default STDOUT device).
If an input file is not specified, it reads or copies data from the default STDIN device and displays or pastes that data on the default STDOUT device. Since we didn't specify any filename, the cat command reads input data from the keyboard (default STDIN device) and displays that data on the monitor screen (default STDOUT device).
To see how this works, type anything and press the Enter key at the end of each line. The cat command reads whatever you typed in the line and displays that as it is on the monitor. To terminate the cat command and return to the shell prompt, press CTRL+D key.
The following image shows the above exercise with the output.
Now we know, if we don't specify a filename, the cat command reads input data from the keyboard and displays that data on the monitor screen. What if, instead of displaying the output on the screen, we want to store the output in a file? No problem, we can easily do this by redirecting the STDOUT to the file. Let's do this by running the following command.
$cat > myfile
Since in this command we did not specify any input file, the cat command reads input data from the keyboard. But, this time the cat command, instead of sending the output to the default monitor screen, sends the output to the file named myfile.
The following image shows the above exercise with the output.
We have stored the output in the file myfile. Let's specify this file as the input file of the cat command.
$cat myfile
This time, since we specified the filename, the cat command reads the input data from the specified file and displays that on the STDOUT.
The following image shows the output of this command.
Append mode V/s Overwrite mode
As mentioned earlier, the shell can redirect the output in two modes: the append mode and the overwrite mode. For the append mode, it uses the >> symbol. For the overwrite mode, it uses the > symbol.
Let's understand the difference between both modes through examples. We have created a file by redirecting and storing the output. If we want to redirect or store another output in the same file, we have two options: append the existing contents and overwrite the existing contents.
To append the existing contents, use the following command.
$cat >> myfile
To overwrite the existing contents, use the following command.
$cat > myfile
The following image shows the output of both commands.
Redirecting errors
When we run a program, process, script, or command an error could occur. When an error occurs, the system sends that error in STDERR. Since by default STDOUT and STDERR use the same device (monitor screen), errors are also displayed on the monitor screen.
If you want to redirect errors on another device or want to save them in a file for logging and debugging purposes, you can easily do this by using the same output redirection symbols. By default, the shell uses the symbol > and >> to redirect the STDOUT. To use these symbols to redirect STDERR, we have to use file descriptors number of STDERR with these symbols.
Linux uses a unified I/O model. In this model, it assigns a small integer value to each channel called a file descriptor. File descriptors numbers of STDIN, STDOUT, and STDERR are 0, 1, and 2. We can use these numbers to refer to the STDIN, STDOUT, and STDERR in our commands and scripts.
To instruct the shell to redirect STDERR instead of STDOUT, precedes the output redirection symbol with the number 2 (file descriptor number of STDERR). To understand it more clearly, let's do a little exercise. Run the following commands.
$ls $cat [an-existing-file] $cat [an-existing-file] 2> errorlog $cat errorlog $cat [non-existing file] $cat [non-existing file] 2> errorlog $cat errorlog
The following image shows the above exercise with the output.
In this exercise, first, we displayed the contents of an existing file by using the cat command. Later, we run the same command again after adding the '2> errorlog' to the command. When executing this command if any error occurs, the '2> errorlog' instructs the shell to redirect that error to the 'errorlog' file instead of displaying that error on the monitor screen. Since the input file exists, no error occurred. To verify this, we displayed the contents of the errorlog file.
After this, we used the cat command to display the contents of a non-existing file. Since the file doesn't exist, the shell displayed the 'No such file or directory' error. Later, we executed the same command again by adding, the '2>errorlog' to the command. This time, since we instructed the shell to redirect errors in the 'errorlog' file, instead of displaying the error on the screen, the shell sent the error to the file 'errorlog'. To verify this, we displayed the contents of the errorlog file again. The errorlog file contains the error message.
When redirecting STDOUT, we use the >> symbol to append the existing file. We can use the same >> symbol to append the existing file when redirecting the STDERR. If you will the > symbol to redirect STDERR, the shell will overwrite the contents of the specified file.
Let's do a small exercise to understand it more clearly. Run the following commands on the terminal.
$cat nofile 2> errorlog $cat errorlog $cat nonexistingfile 2> errorlog $cat errorlog $cat unknownfile 2>> errorlog $cat errolog
In this exercise, we used the cat command to display the contents of three files that do not exist. Since files do not exist, we got error each time. We redirected errors to the errorlog file. To redirect errors, the first and second times we used the overwrite mode while in the third time we used the append mode.
The following image shows this exercise with the output.
As you can see in the above image, when we used the overwrite mode, the contents of the existing file were overwritten but when used the append mode the contents of the existing file were appended.
Redirecting two or all three data streams together
If you want to redirect two or all three data streams together, you can do this by following the same way that you use to redirect one stream. For example, the following command uses all three redirections together.
$cat < testfile > outputfile 2> errorlog
This command reads/copies input data from the testfile and pastes that data in the outputfile. In this process, if any error occurs, it writes that error in the errorlog file.
The following image shows this command with the output.
Sending errors to /dev/null
The /dev/null device discards all input data streams. This device is commonly used to discard errors and unwanted messages that occur when a process, script, or command executes. For example, when you use the find command to search something globally, many "permission denied" errors might occur. Your genuine results may be lost in the clutter. To discard all errors, you can redirect STDERR to the /dev/null. Let's take an example to understand it practically.
Run the following commands and compare their outputs.
$find / -name core $find / -name core 2> /dev/null
The following image shows the output of both commands.
As you can see in the above output, when we used the find command without redirecting STDERR , we got many permission related errors. But when we used the find command with redirecting STDERR to /dev/null, we got only results. All errors were redirected to /dev/null and all results were displayed on the screen.
I/O redirection summary
The following table summarizes I/O redirection symbols along with their meanings.
Symbols | Description |
< | To redirect the input |
> | To redirect the output in overwrite mode |
2> | To redirect the errors in overwrite mode |
>> | To redirect the output in append mode |
2 >> | To redirect the errors in append mode |
>& | To redirect both the output and errors in the same place in overwrite mode |
>>& | To redirect both the output and errors in the same place in append mode |
That's all for this tutorial. If you like this tutorial, please don't forget to share it with friends through your favorite social channel.