Aug 22, 2011
tom

How to pipe stderr without piping stdout

Question

How do I pipe the standard error stream without piping the standard out stream?

I know this command works, but it also writes the standard out.

Command 2>&1 | tee -a $LOG

How do I get just the standard error?

Note: What I want out of this is to just write the stderr stream to a log and write both stderr and stdout to the console.

Answer

To do that, use one extra file descriptor to switch stderr and stdout:

find /var/log 3>&1 1>&2 2>&3 | tee foo.file

Basically, it works, or at least I think it works, as follows:
The re-directions are evaluated left-to-right.

3>&1 Makes a new file descriptor, 3 a duplicate (copy) of fd 1 (stdout).

1>&2 Make stdout (1) a duplicate of fd 2 (stderr)

2>&3 Make fd 2, a duplicate (copy) of 3, which was previously made a copy of stdout.

So now stderr and stdout are switched.

| tee foo.file tee duplicates file descriptor 1 which was made into stderr.

Related posts:

  1. Capturing STDERR and STDOUT to file using tee
  2. start sinatra app in background with stdout and stderr redirected(append) to a file
  3. How can I capture output from LFTP? (Output not written to STDOUT or STDERR?)
  4. Cronjob stderr to file and email
  5. How can I redirect an already running process’s stdout/stderr?

Leave a comment