Ubuntu

Input-Output Redirection in Ubuntu

In Linux, each process has three communication channels: standard input, standard output, and standard error. These communication channels help users interact with the processes on a Linux system. The standard input (STDIN) communication channel takes the input from the user through the terminal as a command, and then after processing the command with the Linux kernel, the terminal displays the result through standard output (STDOUT) or standard error (STDERR) communication channel. The standard output (STDOUT) or standard error (STDERR) of one command can be redirected as the standard input (STDIN) for another command using the “>” I/O redirection, and similarly, a standard input (STDIN) can be redirected as the standard output (STDOUT) for another command using the “<” I/O redirection.

Standard input

The standard input or STDIN is the command that we type in our terminal to interact with the Linux kernel.

Ubuntu@Ubuntu:~$ ls -la

In the above example, “ls -la” is the standard input or STDIN.

Standard output

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

Ubuntu@Ubuntu:~$ ls -la
...snip...
-rwxr-x---  1 Ubuntu Ubuntu       89 Jan  4  2021  arith.sh
-rw-r--r--  1 Ubuntu Ubuntu   3771 Jun  3  2020  .bashrc
drwx------ 28 Ubuntu Ubuntu   4096 Aug  1 13:10  .cache
drwxr-xr-x 36 Ubuntu Ubuntu    4096 Jul 29 18:30  .config
drwx------  3 Ubuntu Ubuntu    4096 Nov 18  2020  .dbus
drwxr-xr-x  5 Ubuntu Ubuntu    4096 Jul 30 16:28  Desktop
drwxr-xr-x 13 Ubuntu Ubuntu   12288 Jul 28 19:53  Downloads
...snip...

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.

  • Standard output redirection “>”
  • Standard Input redirection “<”
  • Standard error redirection “2>”
  • Standard output and error redirection “&>”
  • Standard output redirection “>>”
  • Standard input redirection “<<”
  • Redirection using pipes “|”

Standard Output Redirection “ > “

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

Ubuntu@Ubuntu:~$ echo Hello World > file.txt

OR

Ubuntu@Ubuntu:~$ sudo apt-cache pkgnames > packegNames

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 file.txt

Ubuntu@Ubuntu:~$ cat pakegName

This “ 1> ” operator is also used for redirecting standard output.

Standard Input Redirection “ < ”

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

Ubuntu@Ubuntu:~$ less < /etc/passwd

Is the same as this.

Ubuntu@Ubuntu:~$ less  /etc/passwd

It takes input from the file /etc/passwd instead of a keyboard. The “0<” sign can also be used for redirecting standard input.

Standard Error Redirection “2>“

With the redirection method, the standard errors can be redirected and written to a file. For example

Ubuntu@Ubuntu:~$ sudo apt-get update 2> error

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.

Standard Output and Standard Error Redirection “ &> “

With the ” &> ” sign, there is a more efficient way to redirect standard output and standard error simultaneously with the ” &>” sign. For example

Ubuntu@Ubuntu:~$ sudo apt-cache pkgnames &> pkgnames

All the package names and errors will be stored in the file pkgnames.

Standard Output Redirection “ >> ”

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.

Ubuntu@Ubuntu:~$ sudo apt-cache pkgnames >> pkgnames

If the file pkgnames already exist, its content won’t be overwritten; rather, the output will be added at the end of the file.

Standard Input Redirection “ << ”

This redirection method reads the user input from the terminal and then appends it to the file.

Ubuntu@Ubuntu:~$ cat pkgnames << pkgnames
            > ubuntu
            > chrome
            > pkgnames

If the file pkgnames already exist, then its content won’t be overwritten.

Redirection using Pipes “ | “

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

Ubuntu@Ubuntu:~$ sudo apt-cache pkgnames | less

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.

Combining the redirection operators

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.

Conclusion

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.

About the author

Usama Azad

A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14