Showing results for 
Search instead for 
Did you mean: 

Redirect standard error

Go to solution
Ryan Clerk
Frequent Advisor

Redirect standard error

Hi unix experts:

I have an engineer who insists upon using the C shell and he asked me a very simple question. How can I redirect standout output and standard error to separate files? I have read the man page many times and dones searches but my best attempt simply combines standard output and standard error into one file.

Here is as close as I have been able to do:

% find . -print >&/tmp/file1

but that also includes all of the "find: cannot open xxxx" error messages.

This should be so easy but I just can't seem to get the syntax right.

Please help.

A. Clay Stephenson
Acclaimed Contributor

Re: Redirect standard error

First, get a baseball bat and whack your engineer with a baseball bat for using csh -- it is evil incarnate. Secondly, you question is simple and the answer is equally simple, "you can't (at least not directly)". I remember fighting this problem decades ago and this is the way to outbushwhack the C shell:

(find . -print > /var/tmp/mystdout) >&/var/tmp/mystderr

As you have discovered the ">&" redirects both stdout and stderr but I cheated by starting a sub-process and redirecting stdout inside it so that outside the sub-process all that is really left is the output from stderr.

Oh, and don't feel bad because it took me hours to find this trick the first time I needed it.

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Redirect standard error

Hi Ryan:

You should find this classic document helpful in solving your problem *and* perhaps convincing to your engineer that there are far better tools than 'csh':


A. Clay Stephenson
Acclaimed Contributor

Re: Redirect standard error

Hi Ryan,

While Jim's approach of getting your engineer to read a paper might work, it assumes that the engineer can read (and that he is willing to read it, if able). The "whack" approach makes no assumptions and is guaranteed to work. Seriously, there is really no good reason to run csh these days and most scripts can be easily converted. Many, many years ago there was no easy way to set ulimits for data and stack within the Bourne shell and the csh supported it --- allowing the soft ulimits to be increased. That was a valid reason for using it but ksh and POSIX also allow these values to be set, so that reason has gone away.

Food for thought, Clay
If it ain't broke, I can fix that.
Respected Contributor

Re: Redirect standard error

from "man csh"
exactly...26th page in csh man page:

>> name
>>& name
>>! name
>>&! name Uses file name as standard output the same as >,
but appends output to the end of the file. If the
variable noclobber is set, it is an error for the
file not to exist unless one of the ! forms is
given. Otherwise, it is similar to >.

Be Tomorrow, Today.
Ryan Clerk
Frequent Advisor

Re: Redirect standard error


Thanks to everybody. Clay, your solution worked! JRF, I am just a lowly IT worker and engineers are engineers and know their approach is always superior. Sekar, I had already read the man pages.

The bat may be a little drastic but it is tempting!

Thanks again,
A. Clay Stephenson
Acclaimed Contributor

Re: Redirect standard error

Well, Ryan, I've done IT support for engineers and I've had no trouble making them listen after I had earned their respect. It's actually rather easy -- just know more Calculus than they do. In any event, if you can convince them that there are few valid reasons to stick with the C shell these days then the biggest hurdle is converting the scripts. Have a look at this:

I've used the korner utility with varying degrees of success but it will actually convert most well-written csh scripts to their equivalent Korn (which is close enough for HP-UX's POSIX) shell scripts.
If it ain't broke, I can fix that.
Marvin Strong
Honored Contributor

Re: Redirect standard error

If the engineer is so superior to you, why is he/she asking such a simple question. :) Clearly they should have no need for such a lowly IT worker as yourself, and be able to solve the problem on their own.

Personally I take the "Sorry I don't support the antiquated csh shell anymore, if you wish to begin using a more current shell I will happily instruct you in its proper use." *Attitude*

Respected Contributor

Re: Redirect standard error

While agree with the majority of the responses
this far, I can't resist pointing out that the
csh-whynot FAQ is a firm condemnation for csh
*programming*. I know plenty of old-timers that
still prefer csh as their interactive shell. It's
not clear from the base post if the engineer in
question is writing shell scripts.

I came to UNIX later if life and started
straightaway with ksh, not because I had heard
that csh was inherently evil, but because it
lacked command line editing (readline-style
command line editing). However, old (bad) habits
die hard. Vi is the shining example. Again,
coming to UNIX somewhat later in life I had to
laugh at vi and went straight to emacs (with
custom bindings so it behaved like editors I was
used to).

Bottom line is that engineers can certainly get
set in their ways despite things that offer
obvious productivity improvements such as csh ->
, vi -> vim or emacs. The
whack with a 2x4 approach, while satisfying in the
short term, seldom is the best method to move
these folks. They are stubborn enough, and usually
sophisticated enough to KNOW that csh is bad and
they should move... But a shell, like an editor,
is a pretty large comfort zone/acquired taste and
hard to move from. It's just a matter of their
current priorities. Usually it's seeing how other
tools/approaches offer large productivity boosts
that gets them to move. Peer pressure works best

So I'm guessing that this engineer is an older
gentleman/gentlewoman who's been around UNIX for a
while (although I agree that that doesn't quite
fit the profile since the average csh user knows
this trick and has read the whynot FAQ). IOW, this
is not some young Turk who has capriciously
latched on to csh. So instead of the 2x4, just
wish him luck. Make it clear that this is the tip
of the iceberg so he's going to need it. And if
he's the never read the whynot FAQ, you should be
tattooing it on his forehead. It's his first step
toward a long recovery ;-)
Ryan Clerk
Frequent Advisor

Re: Redirect standard error

Hi guys:

Thanks for all the ideas but there is one fact that I omitted. My boss is an engineer and he usually sides with the other engineers. His attitude is give them want they want. Clay, my college math ended with College Algebra so I can't impress them with my knowledge of math. This one engineer is an older guy but he has passed on many of his ideas to the younger engineers. These are mainly scripts that have been around for a very long time that occasionally need some minor changes.


A. Clay Stephenson
Acclaimed Contributor

Re: Redirect standard error

Well, Ryan, being mathematically limited is a personal problem but it's not too late to fix. In all seriousness, once the bunch of engineers that I once had to support saw that I could handle differential equations, it was duck soup. Of course, I told them it was alright to be engineers; at least they weren't chemists. Using "The Force" I sense that you are probably supporting a bunch of guys doing Finite Element Analysis; if so, I am very familiar with the vendor supplied C Shell scripts and they were ugly. You may also be doing CFD but I really sense FEA. In my case, the only reason the C shell was used was to increase soft data ulimit values which both the Korn and POSIX shells now do.

If it ain't broke, I can fix that.

Re: Redirect standard error

The following command saves stdout and stderr to the files "out.txt" and "err.txt", respectively.

[root@server /root]# ./cmd 1>out.txt 2>err.txt

The following command appends stdout and stderr to the files "out.txt" and "err.txt", respectively.

[root@server /root]# ./cmd 1>>out.txt 2>>err.txt

The following command functions similar to the above two commands, but also copies stdout and stderr to the files "stdout.txt" and "stderr.txt", respectively.

[root@server /root]# (((./cmd | tee stdout.txt) 3>&1 1>&2 2>&3\
|tee stderr.txt) 3>&1 1>&2 2>&3) 1>out.txt 2>err.txt

It Has Been...
A. Clay Stephenson
Acclaimed Contributor

Re: Redirect standard error

Note that this was a C shell question so the last solution proposed is a non sequitur --- although valid for other (and arguably better) shells.
If it ain't broke, I can fix that.
Mike Seidel
Occasional Visitor

Re: Redirect standard error


Redirect stdout first, then redirect to stderr

(find / -name foo -print > ~/bar) &> ~/bat

would be equivalent to

find / -name foo -print 1> ~/bar 2> ~/bat

in bash/korn

not that

( find / -name gimp -print > foo ) > & /dev/null &;tail -f foo is as easy as 2>/dev/null, but...