Skip to main content
In this article, we delve into IO redirection in Linux and explain the concept of standard streams. Every Linux command you run is automatically associated with three primary data streams:
  1. STDIN: The standard input stream that accepts text input.
  2. STDOUT: The standard output stream that displays text output on your screen.
  3. STDERR: The standard error stream that shows error messages.
Below is an example of displaying the content of a file using the cat command:
If you attempt to access a file that does not exist, the error message is directed to STDERR:

Redirecting Standard Output and Error

To redirect the standard output to a file instead of the screen, use the forward arrow symbol (>). This operator will overwrite the file with the new output. If you prefer appending the output to an existing file, use the double forward arrow symbol (>>). For instance, to save your shell information and then append a descriptive message, execute the following commands:
Similarly, to redirect only error messages to a file, prefix the redirection operator with the number 2. This tells the shell to redirect STDERR:
To append error messages instead of overwriting them, use 2>>:
If you want to run a command without displaying error messages on the screen, you can redirect STDERR to /dev/null. This special file discards any input provided to it.
Often, you might want to pass the output of one command directly to another without creating an intermediary file—that’s where piping comes into play. The pipe operator (|) transfers the standard output from the command on its left to the standard input of the command on its right. For example:
Consider a file named sample.txt with the following contents:
If you search within sample.txt using grep and redirect the output to a file, you will experience:
However, you can use a pipe to send the output directly to less for quick viewing:
For comparison, you can view the entire file by simply calling:
The pipe mechanism allows you to chain multiple commands together, enhancing efficiency and flexibility.

The tee Command

Another valuable tool for managing IO redirection is the tee command. Unlike the redirection operator that sends the output exclusively to a file, tee duplicates the output by writing it both to a file and to the screen simultaneously. For example, instead of redirecting the output to a file, you can use:
To append the output to the file rather than replacing its content, use the -a option with tee:
This technique is especially useful when you need to monitor the output in real time while also saving it for later review. By mastering these IO redirection techniques, you can efficiently manage command outputs and errors in Linux. Whether saving output to files, suppressing error messages, or chaining commands with pipes, these methods are essential for streamlining your workflow and enhancing your command-line proficiency.

Quick Reference Table

Happy learning and efficient command chaining!

Watch Video

Practice Lab