> For the complete documentation index, see [llms.txt](https://devops-3.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devops-3.gitbook.io/devops/docs/module-1-linux-introduction/redirections.md).

# Redirections

In Linux, whenever an individual runs a command, it can take input, give output, or do both.&#x20;

Redirection helps us redirect these input and output functionalities to the files or folders we want, and we can use special commands or characters to do so.

These redirections can come in handy when we work with multiple and large outputs or inputs since we can use file data directly as input and store results in files.

All this can be done easily on the terminal using some simple commands.

### Types of Redirection

#### 1. Overwrite Redirection

Overwrite redirection is useful when you want to store/save the output of a command to a file and replace all the existing content of that file.

* “>” standard output
* “<” standard input

```bash
cat > file.txt // This is standard output redirection.  
```

```bash
cat < file.txt // This is standard input redirection.
```

#### 2. Append Redirection

With the help of this Redirection, you can append the output to the file without compromising the existing data of the file.

* “>>” standard output
* “<<” standard input

{% code overflow="wrap" %}

```bash
cat >> file.txt // This is standard output redirection with Append
```

{% endcode %}

{% code overflow="wrap" %}

```bash
cat >> file.txt // This is standard input redirection with Append
```

{% endcode %}

#### 3.  Merge Redirection

This allows you to redirect the output of a command or a program to a specific file descriptor instead of standard output. the syntax for using this is “>&” operator followed by the file descriptor number.

* “p >& q” Merges output from stream p with stream q
* “p <& q” Merges input from stream p with stream q

#### 4. Error Redirection

Error redirection is transferring the errors generated by some false commands to a file rather than STDOUT.

Whenever a program is executed at the terminal, 3 files are generated: standard input(0), standard output(1), standard error(2). These files are always created whenever a program is run.&#x20;

By default, an error stream is displayed on the screen.

* "2>"
* "2>&1"

```bash
ls 2>error.txt
```

**2(STDERR)**. Using “**2>**” re-directs the error output to a file named “error.txt” and nothing is displayed on **STDOUT.**

```bash
ls GUVI > error.txt 2>&1
```

**2>&1** means that **STDERR** redirects to the target of **STDOUT.** More formally, the error message generated by “**2**” gets merged with the current output “**1**“.&#x20;

The error output is merged with the standard output which in turn is being re-directed to “**error.txt**“.&#x20;

#### 5. Program Redirection

Pipe redirects a stream from one **program** to another.&#x20;

When pipe is used to send standard output of one program to another program, first program's data will not be displayed on the terminal, only the second program's data will be displayed.

Pipe redirects data from one program to another while brackets are only used in redirection of files.

```bash
ls *.txt | cat > txtFile
```

command **"ls \*.txt | cat > txtFile"** has put all the '.txt' files into a newly created file 'txtFile'.

{% hint style="info" %}
Redirection is used to control standard input, standard output, and standard error of commands.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devops-3.gitbook.io/devops/docs/module-1-linux-introduction/redirections.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
