Redirecting STDERR message to STDOUT & file at same time

Friends
I have to redirect STDERR messages both to screen and also capture the same in a file.
2 > &1 | tee file works but it also displays the non error messages to file, while i only need error messages.
Can anyone help??

prog 2 > | tee file

it doesn't work:(
it neither print the ERROR messages on the screen nor on file

prog 1>/dev/null | tee fil

Not tested ... :confused:

no man. The requirement is this.

while running an application I have to print STDERR messages both on screen(STDOUT) and file at the same time while the normal STDOUT messages should be on screen only.

Use this:
Script is "cmd"

#!/bin/sh
echo "This is Standard Out"
echo "This is Standard Error" >&2

Use cmd script like this:

((./cmd 3>&1 1>&2 2>&3) | tee /dev/tty) > err.log

The output is:

((./cmd 3>&1 1>&2 2>&3) | tee /dev/tty) > err.log
This is Standard Out
This is Standard Error

File err.log contains only stderr:

cat err.log
This is Standard Error

I think it's good now ! :cool::b:

thanks man,
It works for most cases. But suppose if i give
cat nofile 3>&1 1>&2 2>&3) | tee /dev/tty > err.log

cat: cannot open nofile
is shown on screen only but not on err.log file

IS that not a STDERR msg

You have to put parenthesis !!!

$ ((cat foo 3>&1 1>&2 2>&3) | tee /dev/tty) > err.log
cat: 0652-050 Cannot open foo.
$ cat err.log
cat: 0652-050 Cannot open foo.

Why don't you try out the followings ---:

Step 1 - " tail -f my_err_file & "
Step 2 - " make 2>my_err_file "

//If you are compiling through make

Let me know if it worked for you.

am not creating a makefile. And the previous solution works great.
Thanks for ur help anyways

vnix$ echo bar >bar

vnix$ (cat foo bar 2>&1 1>&3) 3>&2 | tee err.log
bar
cat: foo: No such file or directory

vnix$ cat err.log 
cat: foo: No such file or directory

The output is in slightly different order than I'd otherwise get because of buffering.