I/O Redirection In Linux System
Tags: linux, ubuntu, computerscience, beginners
Published: 2020-06-21 16:37:48

Today we are going to have a look at one of the most important/interesting topics in Linux System **I/O Redirection**.

What is I/O Redirection?

Let's break the two words

**I/O**: Input and Output

**Redirection**: to change the direction or focus of

It means changing the **source(input)**, **destination(output)** of **data streams**.

Basically changing where the **input to a command comes from** and **output of the command is sent to**.

Okay, but what are these data streams?

**Streams in Linux System**

In Linux Systems each command, and hence every process, is initialized with three data streams:

1. Standard Input(Stdin)
2. Standard Output(Stdout)
3. Standard Error(Stderr)

**Standard Input(Stdin)**

- The **input stream** is referred to as **Standard Input**.

- By default input stream is set as **User's Keyboard**.

- The input stream is numbered **0**.

Let's take an example of input streams:-

For this example, we are going to make use of the `cat` command.

The `cat` stands for **concatination**. The `cat` command is usually used to concatenate files.

To use cat command we simply type `cat` and press `Enter`. After that, we type the input from our keyboard.

The `cat` command takes input from our Standard Input Stream and then returns the output to the terminal.

![cat-normal-input](https://dev-to-uploads.s3.amazonaws.com/i/im8nhy4rnljf2viaevie.png)

In the above example, we used the `cat` command and entered **Hello** from our Standard Input Stream(Keyboard) and the command returned our input to Standard Output Stream(terminal).

**Standard Output(Stdout)**

- The **output stream** is referred to as **Standard Output**.

- By default, the output stream is set to User's **Terminal**.

- The output stream is numbered **1**.

Let's take an example of output stream:-

For this example, we are going to make use of the `echo` command.

The `echo` command is usually used to display a line of text.

![echo-hello-text](https://dev-to-uploads.s3.amazonaws.com/i/pi9iimm273jwppj4c3of.png)

In the above example, we used the `echo` command and provided the value "Hello World" which was printed to the Standard Output Stream **terminal**. 

**Standard Error(Stderr)**

- The **error stream** is referred to as **Standard Error**.

- The error stream is needed because you don't always want your error to be displayed on your terminal. Sometimes there can be **huge errors that could crowd the terminal**. So you would want to save it somewhere else.

- By default, the output stream is set to User's **Terminal**.

- The error stream is numbered **2**.

Let's take an example of output stream:-

For this example, we are going to make use of the `cat` command.

We will knowingly use the try to access a file that is not present and look at the error.

![cat-error](https://dev-to-uploads.s3.amazonaws.com/i/jo9j7u0y61dymr9b0yb4.png)

In the above example, we used the `cat` command and provided the file name "hello" which is not present, and hence it prints the error message to the Standard Error Stream **terminal**.

These were the different data streams but what is **redirection**?

**Redirection**

Each Data Stream uses redirection commands. Let's look at each of them one by one:

**1. Output Stream**
The output stream uses `>` to redirect output.

Let's take an example for better understanding

![cat-output-single](https://dev-to-uploads.s3.amazonaws.com/i/qz0ean063mfmpflj79di.png)

In the above example, we used the `cat` command to create a file hello-world.txt and then inserted multiple lines to the file using Standard Output Data Stream.

Using the Standard Output Stream we have redirected the output to **a file** instead of **terminal**.

The contents of the file "hello-world.txt" currently are 
```
Hello
World
!!!
<Anything-you-may-have-added>
```

Let's try repeating the same steps again but with different content and check the result

![cat-insert-again](https://dev-to-uploads.s3.amazonaws.com/i/ryg0v1jv1y8dmx12zg25.png)

Contents of the file "hello-world.txt" currently

```
New
Content
!!!
``` 
Now since we have used the `>` all the previous content of the file will be overwritten by new content.

But what can be done to keep the previous content as well?

To keep the contents and append the new content after the previous content we use **`>>`**

![cat-double](https://dev-to-uploads.s3.amazonaws.com/i/9tp9s87487uvkbe0qaco.png)

In the above example, we first created and inserted content into a file "hello-world2.txt".

After that, we again inserted some data into the file but this time we used `>>` instead of `>`.

So the current contents of the file "hello-world2.txt"

```
Hello
World
!!!
New
Content
!!!
```

Wait, but there was also a number specified for the output stream. So why are we not using it?

So the `>` and `>>` is by default set to `1` and hence we don't need to use `1>` or `1>>` explicitly.

**2. Input Stream**

The input stream uses `<` to redirect input.

Let's just use `cat` to read from our recently created file "hello-world2.txt"

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s1sh80p2591uu9ti12dv.png)

In the example, we used the command `cat < hello-world2.txt` to read the content from the file as input and print it out on the terminal.

**3. Error Stream**

The error stream uses `>` to redirect output where

![error-basic](https://dev-to-uploads.s3.amazonaws.com/i/verc90czwuew4es9dpgk.png)

In the above example, we used the wrong command `lecho` so as to produce an error. The first time we used the command and printed the error on the terminal and the next time we used the `2>` and redirected the error to file instead of the terminal.

**Note:** We need to use `2` along with `>` to specify that we want to redirect the **"Error Stream"**.

Similarly, to avoid the overwriting of file contents we use `2>>` instead of `2>`.

This was all about **Redirection in Linux System**. Please, let me know if there are any questions and suggestions on what you want us to explore next.