How to Redirect Stderr and Stdout to a File in Bash

Abid Ullah Feb 15, 2024
  1. Standard Output and Standard Error in Linux
  2. Redirect the Standard Output to a File in Bash
  3. Redirect the Standard Error to a File in Bash
  4. Redirect the Standard Output and Standard Error to a File in Bash
How to Redirect Stderr and Stdout to a File in Bash

This article will discuss the Standard Output and Standard Error in Linux. We will see how we can redirect Standard Output and Standard Error in Bash.

Let’s begin understanding the terms Standard Output and Standard Error in Linux.

Standard Output and Standard Error in Linux

In Linux, commands take some inputs from the user, which can be a file or any attribute. When we execute these commands, they give some output of the input we entered, called the Standard Output.

This Standard Output could be a success or an error. If the output we get is an error, we call it Standard Error.

Both will be displayed on our terminal screen. Sometimes, we want to store the Standard Output and Standard Error in a file for testing or debugging the code.

In Linux, we can redirect these two outputs to a file, and the process is storing it is called redirection.

In Linux, for every process, we have three data streams, Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr).

The Standard Input (stdin) takes input from the user via the keyboard. The Standard Output (stdout) displays the output on the terminal screen.

The Standard Error (stderr) shows the error information on the screen. By default, both the Standard Output and Standard Error are printed on the terminal screen.

In Linux, each data stream has a unique numeric id:

  • For Standard Input (stdin), the numeric id is 0.
  • For Standard Output (stdout), the numeric id is 1.
  • For Standard Error (stderr), the numeric id is 2.

Let’s explain the redirection of the Standard Output and Standard Error in more detail.

Redirect the Standard Output to a File in Bash

In Linux, we can redirect the stdout to a file using its numeric id. To redirect the Standard Output of any command, we use 1 with a redirection operation that is the sign >.

In our example, we will use the ls command. We will redirect the output of the ls command to a file.

Then we will view the text file to see the output of the ls command stored in the file.

Example Code:

$ ls 1> stdout.txt

To view the file, we use this command:

$ cat stdout.txt

The output of the code:

Redirect ls Command Output to File

As shown in the terminal screen above, we used the ls 1> stdout.txt command in the terminal. This command stores the output of the ls command into the file named stdout.txt.

We use the ls command in Linux to list all the files and directories. So we redirect the output of the ls command in a file.

When we viewed the file using the cat command, we could see the Standard Output of the ls command stored in the file. This is how we can redirect the Standard Output of any command to a file using its numeric id.

Redirect the Standard Error to a File in Bash

We can also redirect the standard error to a file using its numeric id. To redirect the standard error to a file, we use its numeric id 2 with the redirection operator, >.

We can do that using the following command in the terminal.

Example Code:

$ cat file.txt 2> stderr.txt

To view the file named stderr.txt, we use this command:

$ cat stderr.txt

Redirect stderr to a File

As we can see, we used the cat file.txt 2> stderr.txt command in the terminal. Since there is no file named file.txt in the current directory, the cat command will give an error that we appended in the stderr.txt file.

When we viewed the stderr.txt file using the cat command, we can see that it shows the error that says No such file or directory. This means that we have redirected the Standard Error of the cat command into a file named stderr.txt.

This is how we can redirect the Standard Error of any command into a file.

Redirect the Standard Output and Standard Error to a File in Bash

We can also redirect these two outputs into a file using a single command. For that, we use the following command.

Example Code:

$ ls 1> stdout.txt 2> stderr.txt

To view the stdout.txt file, we use this command:

$ cat stdout.txt

To view the stderr.txt file, we use this command:

$ cat stderr.txt

Redirect stdout and stderr Using Single Command

As we can see, we redirected the Standard Output and Standard Error to files in a single command. The ls command output will be stored in the file stdout.txt, and the stderr.txt fill will remain empty because there would be no error.

This is how we can redirect the Standard Output and Standard Error to a file by just using a single command.

We hope you find this Linux article helpful in understanding how to redirect any command’s Standard Output and Standard Error to a file in Bash.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn

Related Article - Linux File