Input Output Redirection in Linux
Placement-ready Courses: Enroll Now, Thank us Later!
Input Output Redirection in Linux is one of the most interesting and important topics, it is this feature that allows you to redirect the input and/or output of commands. In this article, you will learn all about the redirection of output and input in Linux-based operating systems. So pay attention, grab a snack if you need, and read right till the end.
What is Input Output Redirection in Linux?
First of all, I/O is the abbreviation for input/output. In Linux-based operating systems, I/O redirection is the ability of the operating system that allows us to change the STDIN (standard input) and STDOUT (standard output) while executing commands in the terminal.
We all know that the STDIN device is your keyboard and the STDOUT device is your screen – at least for the terminal. The term redirection in computer language refers to the way from where commands read input to where commands send output.
There are hundreds of shell commands, and every single one of them does one of the following – take input, give output or do both. With the help of the special redirection characters, we can redirect these input and output functionalities.
Let us understand this better with an example. Say we run any arbitrary command in the terminal that will print the output on the terminal screen. But, we don’t want the output printed on the terminal, instead, we
want to save it into a file, using the redirection operator we can do exactly that. To sum up in one line, redirecting simply means diverting the input or output.
Similarly, if we have another random command that needs to be given an input. But instead of giving the input directly, we want it to be taken from a specific file called “file.txt”. To do the same, we simply make use of the redirecting operator. In this article, we will look at how to make use of it in multiple ways to redirect input and output.
Linux I/O Redirection Terminologies
Before we proceed further and look at how to use the redirection operator, let us familiarize ourselves with some terminology of the standard streams of the input/output redirection.
1. STDIN
STDIN stands for standard input. The bash shell takes input from the STDIN. The standard input stream is numbered as stdin(0). It is connected to the keyboard.
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
2. STDOUT
STDOUT stands for standard output. The bash shell sends output to the STDOUT. The standard output stream is numbered as stdout(1). It is connected to the screen.
3. STDERR
STDERR stands for standard error. The bash shell sends the error messages to STDERR. The standard error stream is numbered as stderr(2). It is connected to the screen.
These are the 3 streams that come into the picture when we talk about input/output redirection.
Types of redirection operators in Linux
Before we look at how to use the redirection operator is important to understand how to combine the redirection operator to perform different actions.
1. Overwrite
- “>” – standard input
- “<” – standard output
2. Appends
- “>>” – standard output
- “<<” – standard output
3. Merge
- “p>&q” – merge output from stream p with stream q
- “p>&q” – merge input from stream p with stream q
Now that we have covered the basics, let us look at some beautiful practical examples of the redirection command in the terminal.
Redirecting STDOUT
We know that the “top” command will print all the details of the process happening on the computer, when we run the top command on its own, it will print its output on the screen, but if we want to redirect the output and save it into a file, we can use the redirector operator as shown below:
top -bn 5 >top.log
Let us take a closer look at the command mentioned above:
The top command is followed by the 2 options: “-b” which enables the top to run in batch mode, so that you can redirect its output to a file or another command, and the option “-n” which specifies the number of iterations before the command terminates. Now if we open the file “top.log” (where the output of the top command is saved) using “cat”, we get the output shown below:
Appending STDOUT
We saw earlier that if we want to append the STDOUT we use the operator “>>”. When we use this operator, it will not overwrite the entire content that already exists in the file, it instead appends the content at the end of the already existing content of the file. We can use the redirecting operator as follows:
top -bn 5 >>top.log
Similarly, we can also use the stream number, as stdin stands for (1) as follows:
top -bn 5 1>top.log
Redirecting STDERR
To redirect the standard error of a command to a file you need to explicitly specify the file descriptor number, 2 for the shell to understand that you are redirecting the standard error [stderr(2)]
Let us take the simple example of listing the contents of the root directory, for which you either need to be a root user or need sudo privileges, else you will get an error saying “permission denied”
To redirect this error message into a file called “ls-error.log” use the following command:
ls -l /root/ 2>ls-error.log
Once you have successfully redirected the stderr into the file, you can open it using the “cat” command as follows:
cat ls-error.log
Appending STDERR
To append and not overwrite the error message into the file, we use the “>>” operator as shown below:
Notice that the first time I opened the file, there was only one error message, but since I appended another error message, there are now 2 error messages in the file.
Redirecting STDOUT/STDERR
We can also capture all the output of a command (both standard output and standard error) into the specified file, in fact, there are 2 methods to do this, let us look at both:
1. The first method is a relatively old method, it goes as shown below:
ls -l /root/ >ls-error.log 2>&1
Before we proceed to the second method, let us take a moment to understand how the command shown above works. The command will first send the output of the “ls” command to the file ls-error.log and then writes all error messages to the file descriptor 2, which is nothing but standard output It does this as it has been redirected to the file ls-error.log using the “2>&1” operator. Hence the standard error and standard output are both sent to the same file.
2. The second method is pretty direct and self-explanatory:
Appending STDOUT/STDERR
If you want to append the output and error messages in the file and not overwrite them each time use the command shown below:
ls -l /root/ &>>ls-error.log
Redirecting STDIN
95% of all shell commands get their outputs from the standard input, and the standard input is connected to the keyboard. To redirect input use the “<” operator
Redirecting STDIN/STDOUT to a file
If you want to redirect STDIN and then redirect the STDOUT to a file, we make use of both “<” and “>” operators. For example, if we want to sort the contents of a file and then save the output of it as a file, use the following command:
sort <domains.list >sort.output
In the command shown above, the sort command takes the input from the file “ubuntu.txt”, sorts it alphabetically, and then redirects the output to the file “sort.output”
I/O redirection using pipes
We can also redirect the output of one command as the input of the other command using pipes, we can use this technique for complex operations.
For example, the command shown below will list the last 5 recently modifies files.
Before we proceed further, let us look at the command shown above in slightly more depth. With the “ls” command the option “ -l” enables long listing format and the option “-t” sorts the files with respect to the modification time.
The option “-n” used with the “head” command will print out the first “n” number of lines. In the above command, the output of the “ls” command was taken as the input of the “head” command.
Another example of redirecting using piping is shown below:
#image#
In the above example, we piped the echo command with the tee command which reads from standard input and writes to standard output and files.
Summary
As you have seen, the redirecting operator is really simple and helps us do complex tasks in just one line of command, You have now learned how to redirect and append using the redirecting operator apart from the different terminology and types of redirection. In the end, we have also seen how to redirect STDIN and STDOUT using pipes.
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google