Learning Linux Basics Course & Labs

Working with Shell II

IO Redirection

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:

[~]$ cat sample_text.txt
This is the file contents

If you attempt to access a file that does not exist, the error message is directed to STDERR:

[~]$ cat sample_text.txt
This is the file contents
cat: sample_text.txt: No such file or directory

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:

[~]$ echo $SHELL > shell.txt
[~]$ cat shell.txt
/bin/bash

[~]$ echo "This is the Bash shell" >> shell.txt
[~]$ cat shell.txt
/bin/bash
This is the Bash shell

Similarly, to redirect only error messages to a file, prefix the redirection operator with the number 2. This tells the shell to redirect STDERR:

[~]$ cat missing_file 2> error.txt
[~]$ cat error.txt
cat: missing_file: No such file or directory

To append error messages instead of overwriting them, use 2>>:

[~]$ cat missing_file 2>> error.txt

Note

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:

command1 | command2

Consider a file named sample.txt with the following contents:

[~]$ cat sample.txt
hello there!
Nice to see you here!

If you search within sample.txt using grep and redirect the output to a file, you will experience:

[~]$ grep Hello sample.txt > file.txt
[~]$ less file.txt

However, you can use a pipe to send the output directly to less for quick viewing:

[~]$ grep Hello sample.txt | less
Hello there!
(END)

For comparison, you can view the entire file by simply calling:

[~]$ less sample.txt
hello there!
Nice to see you here!
sample.txt (END)

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:

[~]$ echo $SHELL | tee shell.txt
/bin/bash
[~]$ cat shell.txt
/bin/bash

To append the output to the file rather than replacing its content, use the -a option with tee:

[~]$ echo $SHELL | tee -a shell.txt

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

TechniquePurposeExample Command
Redirect STDOUTSend command output to a fileecho $SHELL > shell.txt
Append STDOUTAppend command output to an existing fileecho $SHELL >> shell.txt
Redirect STDERRSend error messages to a filecat missing_file 2> error.txt
Append STDERRAppend error messages to an existing filecat missing_file 2>> error.txt
Use PipesPass output from one command as input to anothergrep Hello sample.txt | less
Utilize tee CommandDuplicate output to file and screen simultaneouslyecho $SHELL | tee shell.txt; echo $SHELL | tee -a shell.txt

Happy learning and efficient command chaining!

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Searching for Files and Patterns