Technology and General Blog

When you redirect any command output to a file, you will detect that the mistake messages are printed on the terminal window. Any command executed in any Linux shell, these as bash, utilizes three standard I/O streams. A numeric file descriptor is made use of to represent each stream.

  • The standard enter stream (stdin):
  • The conventional output stream (stdout): 1
  • The conventional error stream (stderr): 2

In this article, we will grasp the data that arrives less than redirecting stdout and stderr to file.

Each and every functioning system dependent on Linux has a conviction of a default place for the executed command.  Absolutely everyone refers to this notion as “stdout” or “standard output” to make it audio easier. Your Bash or Zsh shell is continually seeking for the default output place.  When the shell detects new output, it shows it on the terminal display for you to see it. If not, it will send the output to its default spot.

Normal mistake (stderr):

Regular error or stderr is similar to conventional enter and output, but it is utilized for storing mistake messages. The conventional error can be redirected to the command line or a file using a terminal. If you want to document or retailer messages in a individual log file or hide the error messages, redirecting stderr will assist you. Now let us head in the direction of the realistic facet of stdout and stderr redirection.

Redirecting stdout and stderr to a file:

As redirection is a method of capturing a plan output and sending it as an enter to an additional command or file.  The I/O streams can be redirected by putting the n> operator in use, wherever n is the file descriptor amount. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

We have created a file named “sample.txt” to retail outlet the redirected output in our recent directory.

The (command > file) is regarded as the classic redirection operator that only redirects the common output with the standard error revealed in the terminal. We will reveal diverse possibilities to redirect stderr as properly.

Redirecting stderr and stdout to independent files:

Under is the command syntax for redirecting stdout and stderr to separate data files.

The under-specified command will redirect the output to the “out” file and error messages to the “error” file.

$ cat sample.txt > out 2>error

Redirecting stderr to stdout:

It is a prevalent practice to redirect the stderr with the regular output of a software to store every thing in a single file. In this article is the command syntax for redirecting stderr to stdout:

$ ls > samplefile.txt 2>&1

$ cat samplefile.txt

> out redirects redirect the stdout to samplefile.txt, and 2>&1 will redirect the stderr to the present locale of stdout.

If stderr is redirected to stdout initially, use the underneath-presented command to redirect the stdout to a file.

$ ls -al 2>&1 > samplefile.txt

$ cat samplefile.txt

“&>” is also utilised for the exact same performance which“2>&1” performs.

$ ls &> samplefile.txt

$ cat samplefile.txt

Redirecting stdout and stderr to a solitary file:

All of the shells do not support this form redirection, but bash and Zsh support it. Stdout and stderr can be redirected by employing the subsequent syntax.

In the impending part of the posting, we will examine out the separate instance for stdout and stderr redirection.

Redirecting stdout to a file:

The normal output is represented by the “1” in the listing of file descriptor figures. For redirect command without the need of any file descriptor amount, the terminal established its value to “1”. The syntax for redirecting the stdout to a file is given as observe:

We are applying the “sample.file” for storing the standard output of the “ls -al” command

$ ls -al > sample.txt

$ cat sample.txt

$ ls 1> sample.txt

$ cat sample.txt

Redirecting stderr to a file:

Use the “2>” operator for redirecting the stderr to a file.

We can incorporate the execution for stderr and stdout in a one redirection command.

command 2> error.txt 1> output.txt

In the beneath-provided example, the error messages will be stored in “error.txt,” the place “output.txt” will have its normal output of “ls command.”

$ ls 2> error.txt 1> output.txt

$ cat output.txt


Getting the notion of redirection and file descriptors for I/O streams is very precious when doing the job in a Linux terminal. In this post, we have talked about the standard I/O streams, like stdout and stderr. The first part of this article brings you specific details about the redirection, I/O streams, and the numeric file descriptor. Up coming, you have observed the simple instance for various sorts of stdout and stderr redirection.

Leave a Reply

Your email address will not be published. Required fields are marked *