How to pipe stderr without piping stdout
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.
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.
Check more discussion of this question.
Related posts:
- Capturing STDERR and STDOUT to file using tee
- start sinatra app in background with stdout and stderr redirected(append) to a file
- How can I capture output from LFTP? (Output not written to STDOUT or STDERR?)
- Cronjob stderr to file and email
- How can I redirect an already running process’s stdout/stderr?
Leave a comment
Recent Posts
- Cron expression that runs every 5 minutes from 1:30 am – 6:00 am [duplicate]
- Understanding redundant power supplies
- Is there a way for administrators to disable users from installing Firefox extensions?
- Is there research material on NTP accuracy available?
- How to create a limited “domain admin” that does not have access to domain controllers?





