What is Redirection in Linux? [Redirect Standard Streams]

Redirection is a powerful feature of the Linux command line which is used to manage the input and output of commands by redirecting them to or from files into other commands. It is a process of manipulating the input and output of commands in Linux. Redirection enables you to connect commands together in several ways and perform complex operations in the Linux command line.

What is Redirection in Linux?

In Linux, redirection is a function that can change the flow of input and output for a process or command. All types of standard streams are redirected in several ways. Some symbols are used to do the redirection, such as, “<”, “>”, and “&”.

Significance of Redirection in Linux

Redirection is used to control standard input, standard output, and standard error of commands by redirecting them to files or from files into other commands in Linux. Using redirection in Linux, you can redirect or append the output/error of commands to files. You can also use a file as input and redirect it to other commands.

The Syntax of Redirection in Linux

The syntax of redirection varies based on the type of redirection performed. You need to use an operator to redirect a command to the files. The basic syntax of redirection is indicated below:

command > file

Here, the command is redirected to the file. The syntax will be different if you use different ways of redirection.

Standard Streams in Linux Redirection

Standard streams are redirected to files or from files in Linux. These are ways of exchanging data between the programs and their environments. There are three types of standard streams:

A. Standard Input(stdin): It reads the input from a command or from a file. The source of standard input is the keyboard.

B. Standard Output(stdout): It displays the output on the terminal or from a file. The terminal is the destination of the standard output.

C. Standard Error(stderr): It displays the error messages on the terminal. The destination of the standard error is the terminal as well.

Different Ways of Redirection in Linux

1. Redirecting Standard Output:

If you want to overwrite the file’s previous data, you can redirect standard output to the file. You have to use the “>” operator to redirect the standard output of a command to a file in Linux.Redirecting Standard Output

As an example, I will redirect the standard output of the pwd command to the stdout.txt file. To do the same you can follow the procedure below:

  1. Initially open the Ubuntu Terminal.

  2. Execute the command below in the command prompt to redirect the output to the text file:

    pwd > stdout.txt
  3. Now, tap the ENTER button.

  4. To view if the output is redirected or not, copy the following command in the command prompt:

    cat stdout.txt
  5. After that, press the ENTER key.

    In the following image, I have redirected the output of the pwd command to the text file.Redirecting Standard Output In Linux

2. Appending Standard Output:

When you want to keep the previous data of the file and add new data to the file you can append standard output to the file. To append the standard output of a command to a file you have to use the “>>” operator in Linux.Appending Standard Output

Here I am going to append the standard output of the whoami command to the stdout.txt file. You can also do the same by following the steps below:

  1. At first open the Ubuntu Terminal.

  2. To check if anything is written inside the file or not, type the following command in the command prompt:

    cat stdout.txt
  3. Then strike the ENTER key.

  4. Run the command below in the command prompt to append the output to the text file:

    whoami >> stdout.txt
  5. Now, hit the ENTER button.

  6. To view if the output is redirected or not, copy the following command in the command prompt:

    cat stdout.txt
  7. After that, tap the ENTER key.

    As you can see, I have appended the output of the whoami command to the text file.Appending Standard Output In Linux

3. Redirecting Standard Input:

You have to use the “<” operator to redirect the standard input of a command to a file in Linux.Redirecting Standard Input

In this example, I will redirect the standard input of the stdout.txt file to the cat command. To do the same you can follow the following process:

  1. To start open the Ubuntu Terminal.

  2. Write the command below in the command prompt to redirect the output to the text file:

    cat < stdout.txt
  3. Then, tap the ENTER button.

    In the image below, I have redirected the input of the text file to the cat command.Redirecting Standard Input In Linux

4. Redirecting Standard Error:

You have to use the “2>” operator to redirect the standard error of a command to a file in Linux.Redirecting Standard Error

Here I am going to redirect the standard error of the ls command to the errorlog.txt file. To do the same you can follow the instructions below:

  1. To initiate open the Ubuntu Terminal.

  2. Copy the command below in the command prompt to redirect the output to the text file:

    ls error1.txt 2> errorlog.txt
    Note: As the error1.txt does not exist, an error occurred
  3. Now, press the ENTER button.

  4. To view the errorlog.txt, type the following command in the command prompt:

    cat errorlog.txt
  5. After that, hit the ENTER key.

    As you can see, I have redirected the error of the ls command to the text file.Redirecting Standard Error In Linux

5. Appending Standard Error:

When you want to keep the previous error data of the file and add new errors to the file you can append standard error to the file. To append the standard error of a command to a file you have to use the “2>>” operator in Linux. Here “2” represents the standard error.Appending Standard Error

In this example, I am going to append the standard error of the ls command to the errorlog.txt file. You can follow the instructions below to do the same:

  1. Firstly launch the Terminal in Ubuntu.

  2. Write the following command to view if anything is written inside the errorlog.txt file or not and press the ENTER key:

    cat errorlog.txt
  3. Run the command below in the command prompt to redirect the output to the text file:
    ls error2.txt 2>> errorlog.txt
    Note: As the error2.txt does not exist, an error occurred
  4. Now, tap the ENTER button.

  5. To view the errorlog.txt, type the following command in the command prompt:

    cat errorlog.txt
  6. Finally, strike the ENTER key.

    In the image below, I have appended the error of the ls command to the text file.Appending Standard Error In Linux

Conclusion

As you’ve seen in this article, using redirection you can redirect input and output streams to or from files to perform complex operations and automate tasks. You have also got to know what standard streams are and different ways of redirection in Linux. Go through the whole article and practice accordingly to become an expert in Linux.

Frequently Asked Questions

What exactly does redirection do in Linux?

Redirection in Linux is the process of manipulating standard input and output streams. To be exact, redirection enables directing command output to files, reading input from files, combining outputs, or handling error messages, etc.

Can I redirect both the output and error to the same file in Linux?

Yes, you can. To redirect both the output and standard errors to the same file, use the redirection operators “&>” after a command followed by the file name to which you want to redirect. For instance, command &> output.txt redirects both the output of command and error to the output.txt file.

What is the difference between the redirection operators “>” and “1>”?

There is no difference between the redirection operators “>” and “1>”. Technically, they do the same thing by redirecting stdout (standard output) to a specified file. In 1>, 1 is called the file descriptor that stands for output redirection just like >. For example, pwd 1> output.txt is functionally equivalent to pwd > output.txt

What is a redirection operator in Linux?

A redirection operator redirects the output of a Linux command to a specified location. For instance, the > operator redirects the output of a command to the specified file or creates it if the file does not exist for a successful redirection. Some other redirection operators are >>, <, etc.


Similar Readings

5/5 - (1 vote)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment