Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

Here is a simple program.

class Redirection {

public static void main (String args[]){
    System.out.println("Hello World_Stdout");
    System.err.println("Hello World_Stderr");
}

}

I want to see the all the outputs on a console, but at the same time I want to store stdout and stderr in different files. I tried the following command, to no avail.

$java Redirection 3>&1 2>stderr 1>stdout 1>&3 2>&3

stderr& stdout files have the file size of 0.

So basically I want to do what "tee" command does but I also want to capture stderr as well.

like image 415
Alby Avatar asked Oct 17 '12 05:10

Alby


People also ask

How do I redirect stderr and STDOUT to a file in bash?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.

How do I redirect stderr and STDOUT 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. We have created a file named “sample.

Does stderr print to terminal?

Stdout and stderr point to your terminal output (or the terminal process that ran the program) and are written to by the running program. What you type in is sent to and interpreted by the running program, and what the program wants to communicate to you is displayed in your terminal screen.


1 Answers

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.

like image 95
Maxim Shoustin Avatar answered Sep 19 '22 21:09

Maxim Shoustin