The standard input or STDIN is the command that we type in our terminal to interact with the Linux kernel.
In the above example, “ls -la” is the standard input or STDIN.
The standard output or STDOUT is the output of a running process or command given to the Linux kernel through standard input and is displayed on the terminal. For example, given above, the output be like
Every output has a pre-defined default place to go in the Unix-based operating systems. Some of the methods for redirecting output are given below.
Output redirection is a method in which the standard output of a command can be redirected to files or as standard input for another command. The “>” sign is used for output redirection. The terminal does not show the output; instead, it is written to a file or redirected as input to another command. For example
OR
Instead of the output shown on the terminal, it will be redirected to the file.txt or pakegsNames file. In order to confirm whether the output was redirected or not, use the cat command to read the files.
Ubuntu@Ubuntu:~$ cat pakegName
This “ 1> ” operator is also used for redirecting standard output.
Input redirection is a method in which the standard input of a command is redirected from a file or a standard output of another command. The “<” sign is used for input redirection. It is mostly the default action of a command. For example
Is the same as this.
It takes input from the file /etc/passwd instead of a keyboard. The “0<” sign can also be used for redirecting standard input.
With the redirection method, the standard errors can be redirected and written to a file. For example
If any error occurs, it will not show on the terminal window; rather, it will be stored in an error file. If the error file already exists, then it will be overwritten.
With the ” &> ” sign, there is a more efficient way to redirect standard output and standard error simultaneously with the ” &>” sign. For example
All the package names and errors will be stored in the file pkgnames.
This redirection method redirects the standard output of a command or a file to another file. The difference is while using “>>” redirection, If the file already exists, the data will be appended to the file; hence the file will not be overwritten.
If the file pkgnames already exist, its content won’t be overwritten; rather, the output will be added at the end of the file.
This redirection method reads the user input from the terminal and then appends it to the file.
If the file pkgnames already exist, then its content won’t be overwritten.
Pipes are often used when you want to combine multiple commands. With pipes, the output of the first command can be used as the input of the second command. For example
In the above command, the standard output of the command left to the pipe “|” sign is the standard input of the command right to the pipe “|” sign.
You can combine some of the redirection operators because they are conveniently easy to use and take less time; some of the combined redirection operators are given below.
2>>: | This operator is used for redirecting standard errors to the file. |
<>: | This operator is used for specified files as both standard input and standard output. |
>&: | This operator redirects the output of one file to another. |
<&: | This operator redirects the input of one file to another. |
2>&1: | This operator is used for redirecting standard error to standard output. |
1>&2: | This operator is used for redirecting standard output to standard error. |
As a Linux administrator, input-output redirection is a very common routine in daily work. Storing the output and errors for later use and combining the multiple commands make your work easier and reduce time so that you can work diligently and efficiently. Above are some tips to do so that will help you to understand input-output redirections.
Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…
Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…
At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…
I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…
Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…
Now I want to share the DNS server installation process on your Ubuntu 20.04 server.…