Categories: 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 “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 “

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

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.

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

3 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

3 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

4 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

5 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

5 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

6 days ago