Which symbol should i use to redirect the standard output to a file (overwrite the file)?

When you redirect any command output to a file, you will notice that the error messages are printed on the terminal window. Any command executed in any Linux shell, such as bash, utilizes three regular I/O streams. A numeric file descriptor is used to represent each stream.

  • The standard input stream (stdin): 0
  • The standard output stream (stdout): 1
  • The standard error stream (stderr): 2

In this post, we will grasp the information that comes under redirecting stdout and stderr to file.

Standard output (stdout):

Each operating system based on Linux has a conviction of a default place for the executed command.  Everyone refers to this notion as “stdout” or “standard output” to make it sound easier. Your Bash or Zsh shell is constantly looking for the default output location.  When the shell detects new output, it displays it on the terminal screen for you to see it. Otherwise, it will send the output to its default location.

Standard error (stderr):

Standard error or stderr is similar to standard input and output, but it is utilized for storing error messages. The standard error can be redirected to the command line or a file using a terminal. If you want to record or store messages in a separate log file or hide the error messages, redirecting stderr will help you. Now let’s head towards the practical side of stdout and stderr redirection.

Redirecting stdout and stderr to a file:

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

We have created a file named “sample.txt” to store the redirected output in our current directory.

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

The (command > file) is considered as the classic redirection operator that only redirects the standard output with the standard error shown in the terminal. We will demonstrate different options to redirect stderr as well.

Redirecting stderr and stdout to separate files:

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

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

$ cat sample.txt > out 2>error

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

Redirecting stderr to stdout:

It is a common practice to redirect the stderr with the standard output of a program to store everything in a single file. Here 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 current location of stdout.

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

If stderr is redirected to stdout first, use the below-given command to redirect the stdout to a file.

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

$ cat samplefile.txt

Which symbol should i use to redirect the standard output to a file (overwrite the file)?
Which symbol should i use to redirect the standard output to a file (overwrite the file)?

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

“&>” is also used for the same functionality which“2>&1” performs.

$ ls &> samplefile.txt

$ cat samplefile.txt

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

Redirecting stdout and stderr to a single file:

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

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

In the upcoming section of the article, we will check out the separate example for stdout and stderr redirection.

Redirecting stdout to a file:

The standard output is represented by the “1” in the list of file descriptor numbers. For redirect command without any file descriptor number, the terminal set its value to “1”. The syntax for redirecting the stdout to a file is given as follow:

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

$ ls -al > sample.txt

$ cat sample.txt

Which symbol should i use to redirect the standard output to a file (overwrite the file)?
Which symbol should i use to redirect the standard output to a file (overwrite the file)?

$ ls 1> sample.txt

$ cat sample.txt

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

Redirecting stderr to a file:

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

Which symbol should i use to redirect the standard output to a file (overwrite the file)?
Which symbol should i use to redirect the standard output to a file (overwrite the file)?

We can combine the execution for stderr and stdout in a single redirection command.

command 2> error.txt 1> output.txt

In the below-given example, the error messages will be stored in “error.txt,” where “output.txt” will have its standard output of “ls command.”

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

$ cat output.txt

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

Conclusion:

Having the concept of redirection and file descriptors for I/O streams is very valuable while working in a Linux terminal. In this post, we have talked about the regular I/O streams, including stdout and stderr. The first section of this post brings you detailed information about the redirection, I/O streams, and the numeric file descriptor. Next, you have seen the practical example for various forms of stdout and stderr redirection.

About the author

Which symbol should i use to redirect the standard output to a file (overwrite the file)?

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.

Which symbol should I use to redirect the standard output to a file appending to the file )?

Redirecting output to append to a file When the notation > > filename is added to the end of a command, the output of the command is appended to the specified file name, rather than writing over any existing data. The >> symbol is known as the append redirection operator.

How do I redirect standard output to a file?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

Which symbol should I use to redirect the standard output to a file overwrite the file in Ubuntu?

The command | tee file pattern (which includes the tee command) redirects the standard output of the command to a file and overwrites its contents. Then, it displays the redirected output in the terminal.

Which symbol is used for redirecting the output?

The '>' symbol is used for output (STDOUT) redirection. Here the output of command ls -al is re-directed to file “listings” instead of your screen.