Use I/O Redirection in Linux Using Command Examples

This guide intends to teach you to Use I/O Redirection in Linux Using Command Examples. In Linux distributions, the I/O (Input/Output) redirection allows you to change the standard output (stdout) and standard input (stdin) when you execute a command in your Linux terminal. You have the following streams in your Linux distro:

  • standard input: stdin
  • standard output: stdout
  • standard error: stderr

Also, they are numbered as shown below:

  • stdin (0)
  • stdout (1)
  • stderr (2)

The standard input is your keyboard that you write commands and the standard output and error are the results shown in the terminal.

Command Examples of stdin, stdout, and stderr in Linux

As we said the stdin device is your keyboard that you put the data to the program.

The stdout is the results that are displayed in your Linux terminal. For example, we run the following echo command in our Linux terminal:

echo this is a test from orcacore
Output
this is a test from orcacore

When the stdout is not redirected, it will show the result directly to the terminal.

The stderr will show the error results in your Linux terminal. For example, we run the following ls command in the terminal:

ls %
Output
ls: cannot access '%': No such file or directory

As you can see, the result shows that there is no file with the % argument. This is a standard error that you get.

Now proceed to the following steps to see how to use the I/O redirection in Linux.

Steps To Use I/O Redirection in Linux Using Command Examples

The stdin, stdout, and stderr have a redirection. You can use them to change the results. At this point, you can overwrite and append the results by using the following symbols:

Overwrite:

  • > – standard output
  • < – standard input
  • 2> – standard error

Append:

  • >> – standard output
  • << – standard input
  • 2>> – standard error

Let’s see how you can use them with examples in Linux.

Step 1 – Use Output Redirection Command in Linux

As you saw, the symbol of stdout redirection is >. For example, we run the following echo command and redirect the results to a file:

echo "this is test from orcacore" > stdout.txt

This will create the file if doesn’t exist.

Then, use the cat command to check the file:

cat stdout.txt
Output
this is test from orcacore

As you can see, the results are redirected to a new file and don’t show directly.

You can use any command and redirect your results anywhere you want.

Also, you can use the output append redirection by using the >> symbol. In this way, you can add new content to the file. For example:

echo "you can learn Linux with orcacore" >> stdout.txt

Check the file:

cat stdout.txt
Output
this is test from orcacore
you can learn Linux with orcacore

As you can see with the append option, the previous results will not deleted and the new ones added to the file.

Step 2 – Use Input Redirection Command in Linux

At this point, you can use the < symbol for input redirection. You just remember that only commands that get their input from the keyboard can have their input redirected.

For example, we use the grep command that receives input from the stdout.txt file:

grep -w learn < stdout.txt

In the output, we just see the line that has the word learn:

Output
you can learn Linux with orcacore

Step 3 – Use Error Redirection Command in Linux

At this point, you can redirect your stderr messages in the Linux terminal. For example, we run the command below:

cat stdout-file stdout.txt > outfile
Output
cat: stdout-file: No such file or directory

As you can see, you will get the error message directly from the terminal.

To redirect the error messages, you should use the 2> symbol. For example:

cat stdout-file stdout.txt > outfile 2> stderr-file

In your terminal, you shouldn’t see any error results.

Now check your file:

cat stderr-file
Output
cat: stdout-file: No such file or directory

Note: You can also redirect your results to a file that will be discarded. In this case, you should use the /dev/null file. To get more information and see how you can use it, you can check this guide on dev/null in Linux and Use It with Examples.

How is I/O redirection implemented in Shell?

As you can see, the I/O redirection is accomplished using a redirection operator which allows the user to specify the input or output data to be redirected to (or from) a file. 

How do I use the redirect command?

You can easily use the operators discussed in this guide to redirect your input and output results. These include the following:

  • > – standard output
  • < – standard input
  • 2> – standard error

Conclusion

At this point, you have learned Linux streams which are stdin, stdout, and stderr. Also, you have learned to use the I/O redirection in Linux with command examples to redirect your standard input, output, and errors to your desired file.

Hope you enjoy it. You may be interested in these articles:

Create Linux Permanent Aliases for All Users

3 Ways to Transfer Files from Linux Server to Local Machine

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!