Skip to main content
Explain

Explained: Input, Output and Error Redirection in Linux

Redirection is an essential concept in Linux. Learn how to use stdin, stdout, stderr and pipe redirection in Linux command line.

Abhishek Prakash

If you are familiar with the basic Linux commands, you should also learn the concept of input output redirection.

You already know how a Linux command functions. It takes an input and gives you an output. There are a few players in the scene here. Let me tell you about them.

Stdin, stdout and stderr

When you run a Linux command, there are three data stream that play a part in it:

  • Standard input (stdin) is the source of input data. By default, stdin is any text entered from the keyboard. It’s stream ID is 0.
  • Standard output (stdout) is the outcome of command. By default, it is displayed on the screen. It’s stream ID is 1.
  • Standard error (stderr) is the error message (if any) produced by the commands. By default, stderr is also displayed on the screen. It’s stream ID is 2.

These streams contain the data in plain text in what’s called buffer memory.

Linux redirection normal flow

Think of it as water stream. You need a source for water, a tap for example. You connect a pipe to it and you can either store it in a bucket (file) or water the plants (print it). You can also connect it to another tap, if needed. Basically, you are redirecting the water.

Linux also has this concept of redirection, where you can redirect the stdin, stdout and stderr from its usual destination to another file or command (or even peripheral devices like printers).

Let me show how redirection works and how you can use it.

The output redirection

stdout redirection

The first and simplest form of redirection is output redirection also called stdout redirection.

You already know that by default, the output of a command is displayed on the screen. For example, I use the ls command to list all the files and this is the output I get:

abhishek@linuxhandbook:~$ ls
appstxt  new.txt  static-ip.txt

With output redirection, you can redirect the output to a file. If this output files doesn’t exist, the shell will create it.

command > file

For example, let me save the output of the ls command to a file named output.txt:

abhishek@linuxhandbook:~$ ls > output.txt

The output file is created beforehand

What do you think the content of this output file should be? Let me use the cat command to show you a surprise:

abhishek@linuxhandbook:~$ cat output.txt 
appstxt
new.txt
output.txt
static-ip.txt

Did you notice that the inclusion of output.txt there? I deliberately chose this example to show you this.

The output file to which the stdout is redirected is created before the intended command is run. Why? Because it needs to have the output destination ready to which the output will be sent.

Append instead of clobber

One often ignored problem is that if you redirect to a file that already exists, the shell will erase (clobber) the file first. This means the existing content of the output file will be removed and replaced by the output of the command.

You can append, instead of overwriting it, using the >> redirection syntax.

command >> file

Tip: You can forbid clobbering in current shell session using: set -C

Why would you redirect stdout? You can store the output for future reference and analyze it later. It is specially helpful when the command output is way too big and it takes up all of your screen. It’s like collecting logs.

Pipe redirection

Pipe Redirection

Before you see the stdin redirection, you should learn about pipe redirection. This is more common and probably you’ll be using it a lot.

With pipe redirection, you send the standard output of a command to standard input of another command.

command 1 | command 2

Let me show you a practical example. Say, you want to count the number of visible files in the current directory. You can use ls -1 (it’s numeral one, not letter L) to display the files in the current directory:

abhishek@linuxhandbook:~$ ls -1
appstxt
new.txt
output.txt
static-ip.txt

You probably already know that wc command is used for counting number of lines in a file. If you combine both of these commands with pipe, here’s what you get:

abhishek@linuxhandbook:~$ ls -1 | wc -l
4

With pipe, both commands share the same memory buffer. The output of the first command is stored in the buffer and the same buffer is then used as input for the next command.

You’ll see the result of the last command in the pipeline. That’s obvious because the stdout of earlier command(s) is fed to the next command(s) instead of going to the screen.

Pipe redirection or piping is not limited to connecting just two commands. You may connect more commands as long as the output of one command can be used as the input of the next command.

command_1 | command_2 | command_3 | command_4

Remember the stdout/stdin is a chunk of data, not filenames

Some new Linux users get confused while using the redirection. If a command returns a bunch of filenames as output, you cannot use those filenames as argument.

For example, if you use the find command to find all the files ending with .txt, you cannot pass it through pipe to move the found files to a new directory, not directly like this:

find . -type f -name "*.txt" | mv destination_directory

This is why you’ll often see find command used in conjugation with exec or xargs command. These special commands ‘convert the text with bunch of filenames into filename’ that can be passed as argument.

find . -type f -name "*.txt" | xargs -t -I{} mv {} ../new_dir

The input redirection

Stdin Redirection

You can use stdin redirection to pass the content of a text file to a command like this:

command < file

You won’t see stdin being used a lot. It’s because most Linux commands accept filenames as argument and thus the stdin redirection is often not required.

Take this for example:

head < filename.txt

The above command could have just been head filename.txt (without the <).

It’s not that the stdin redirection is completely useless. Some commands rely on it. Take the tr command for example. This command can do a lot but in the example below, it converts the input text from lower to upper case:

tr a-z A-Z < filename.txt

In fact, using the stdin is advisable over pipe specially to avoid the unnecessary use of cat command.

For example, many people would use the above example with cat and then use tr on it. Frankly, there is no need of using cat here.

cat filename.txt | tr a-z A-Z

Combine redirections

You can combine the stdin, stdout and pipe redirection as per your need.

For example, the command below lists all the .txt files in the current directory and then make a count of those .txt files and saves the output to a new file.

ls *.txt | wc -l > count.txt

Error redirection

Sometimes when you run some command or script, you’ll see that it displays an error message on the screen.

abhishek@linuxhandbook:~$ ls -l ffffff > output.txt
ls: cannot access 'ffffff': No such file or directory

In the beginning of this article, I mentioned that there are three data streams and stderr is one of the output data streams that is displayed on the screen by default.

You may redirect the stderr as well. Since it’s an output data stream, you can use the same > or >> redirection symbol that you used for stdout redirection.

But how do you distinguish between the stdout and stderr when they are both output data stream? By their stream ID (also called file descriptor).

Data Stream Stream ID
stdin 0
stdout 1
stderr 2
-t, –list
-u, –update
-x, –extract, –get
-j, –bzip2
-z, –gzip, –gunzip, –ungzip

By default when you use the output redirection symbol >, it actually means 1>. In words, you are saying that data stream with ID 1 is being outputted here.

When you have to redirect the stderr, you use its ID like 2> or 2>>. This signifies that the output redirection is for data stream stderr (ID 2).

Stderr redirection examples

Let me show it to you with some examples. Suppose you just want to save the error, you can use something like this:

abhishek@linuxhandbook:~$ ls fffff 2> error.txt
abhishek@linuxhandbook:~$ cat error.txt 
ls: cannot access 'fffff': No such file or directory

That was simple. Let’s make it slightly more complicated (and useful):

abhishek@linuxhandbook:~$ ls -l new.txt ffff > output.txt 2> error.txt 
abhishek@linuxhandbook:~$ cat output.txt 
-rw-rw-r-- 1 abhishek abhishek 0 May  5 10:25 new.txt
abhishek@linuxhandbook:~$ cat error.txt 
ls: cannot access 'ffff': No such file or directory

In the above example, the ls command tries to display two files. For one file it gets success and for the other, it gets error. So what I did here is to redirect the stdout to ouput.txt (with >) and the stderr to the error.txt (with 2>).

You can also redirect both stdout and stderr to the same file. There are ways to do that.

In the below example, I first send the stderr (with 2>>) to combined.txt file in append mode. And then, the stdout (with >>) is sent to the same file in append mode.

abhishek@linuxhandbook:~$ ls -l new.txt fff 2>> combined.txt >> combined.txt 
abhishek@linuxhandbook:~$ cat combined.txt 
ls: cannot access 'fff': No such file or directory
-rw-rw-r-- 1 abhishek abhishek 0 May  5 10:25 new.txt

Another way, and this is the preferred one, is to use something like 2>&1. Which can be roughly translated to “redirect stderr to the same address as stdout”.

Let’s take the previous example and this time use the 2>&1 to redirect both of the stdout and stderr to the same file.

abhishek@linuxhandbook:~$ ls -l new.txt fff > output.txt 2>&1
abhishek@linuxhandbook:~$ cat output.txt 
ls: cannot access 'fff': No such file or directory
-rw-rw-r-- 1 abhishek abhishek 0 May  5 10:25 new.txt

Keep in mind that you cannot use 2>>&1 thinking of using it in append mode. 2>&1 goes in append mode already.

You may also use 2> first and then use 1>&2 to redirect stdout to same file as stderr. Basically, it is “>&” which redirects one out data stream to another.

Summary

  • There are three data streams. One input, stdin (0) and two output data streams stdout (1) and stderr (2).
  • Keyboard is the default stdin device and the screen is the default output device.
  • Output redirection is used with > or >> (for append mode).
  • Input redirection is used with <.
  • The stderr can be redirected using 2> or 2>>.
  • The stderr and stdout can be combined using 2>&1.

Since you are learning about redirection, you should also know about the tee command. This command enables you to display to standard output and save to file simultaneously.

Tee Command in Linux Explained with Examples
If you want to display the output of a command and save it to a file simultaneously, tee command is what you need. Learn various ways of using tee command in Linux.

I hope you liked this detailed guide on redirection in Linux. If you still have doubts or if you have suggestions to improve this article, please let me know in the comment section.

Abhishek Prakash