Redirect Stdout and Stderr to File and Stderr to Stdout

How to redirect both stdout and stderr to a file

If you want to log to the same file:

command1 >> log_file 2>&1

If you want different files:

command1 >> log_file 2>> err_file

How to redirect and append both standard output and standard error to a file with Bash

cmd >>file.txt 2>&1

Bash executes the redirects from left to right as follows:

  1. >>file.txt: Open file.txt in append mode and redirect stdout there.
  2. 2>&1: Redirect stderr to "where stdout is currently going". In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently uses.

How to redirect stderr and stdout to the same file

The way open works, it searches for free entry in file descriptor table. Where entries 0, 1 and 2 are reserved for stdin, stdout and stderr respectively.

stdin  - 0
stdout - 1
stderr - 2

If you want to redirect stdout and stderr to log.out you could simply do the following:

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

int main()
{
int fd = - 1;
fd = open("log.out", O_RDWR | O_CREAT);

dup2(fd, 1); // redirects stdout to log.out
dup2(fd, 2); // redirects stderr to log.out

/* Print to stderr and to stdout */
fprintf(stdout, "%s", "Hello world\n");
fprintf(stderr, "%s", "Stack overflow!\n");
return 0;
}

If you care about order, a call to fflush should follow fprintf as redirecting stdout to a file will not flush buffer on newlines.

How to redirect stderr and stdout to different files in the same line in script?

Just add them in one line command 2>> error 1>> output

However, note that >> is for appending if the file already has data. Whereas, > will overwrite any existing data in the file.

So, command 2> error 1> output if you do not want to append.

Just for completion's sake, you can write 1> as just > since the default file descriptor is the output. so 1> and > is the same thing.

So, command 2> error 1> output becomes, command 2> error > output

Windows Redirect STDERR and STDOUT to same program when piping

As others already pointed out in comments you have to do this:

net use Z: \\server\share 2>&1 | TimeStampPipe.exe >> logfile.txt

You already have got the right code to redirect both STDOUT and STDERR to a file:

dir file.xxx 1> output.msg 2>&1

which is the same as:

dir file.xxx > output.msg 2>&1

because output redirection (>) reads from STDOUT (handle 1) by default.

The expression 2>&1 defines that handle 2 (STDERR) is to be redirected to the destination that handle 1 (STDOUT) is currently (re-)directed to. The position of that expression is crucial.


For a pipe (|) you have to apply the same technique. Remember that a pipe redirects the output (at STDOUT, handle 1) of the left-side command into the input (at STDIN, handle 0) of the right-side command.

Now we want to redirect both STDOUT and STDERR of the left side into STDIN of the right side. Since nothing changes for the right side we keep it untouched. The left side however needs to be modified; STDOUT already goes where we want to, but STDERR does not yet, so we have to redirect it to the target of STDOUT, by using the same expression as for output redirection (>):

net use Z: \\server\share 2>&1 | TimeStampPipe.exe >> logfile.txt

Redirecting stderr to file and stdout to console

2>&1 doesn't mean "redirect stderr to console", it means "redirect stderr to the same place as stdout currently goes". If you do the redirections in the other order -- 2>&1 1>hello.i instead of 1>hello.i 2>&1 -- then you will get the effect I believe you are looking for.

You need to think of the redirections happening one at a time. If you say 1>hello.i 2>&1 then this happens:

  • First, stdout is redirected to hello.i.
  • Then stderr is redirected to the current destination of stdout, namely hello.i.

But if you say 2>&1 1>hello.i then this happens:

  • First, stderr is redirected to the current destination of stdout, namely the console.
  • Then stdout is redirected to hello.i.

Bash redirection: save stderr/stdout to different files and still print them out on a console

Here is an answer:

./yourScript.sh > >(tee stdout.log) 2> >(tee stderr.log >&2)

If your script has STDOUT and STDERR descriptors, you get 2 files stdout.log + stderr.log and all output (Err + Out) to console.

Redirect stderr and stdout in Bash

Take a look here. It should be:

yourcommand &> filename

It redirects both standard output and standard error to file filename.



Related Topics



Leave a reply



Submit