How to suppress tar error messages in when piping
Here are couple of alternatives that involve redirecting STDERR of both tar
and grep
:
-
Use
bash
command grouping{}
:{ tar -xvf file.tar.bz2 | grep "something" ;} 2>/dev/null
-
Using a subshell
()
:( tar -xvf file.tar.bz2 | grep "something" ) 2>/dev/null
Note that if you want to redirect STDERR of a single process its better to use Oli's answer instead.
On a different note, if you want to grep
something over both the STDOUT and STDERR of tar
use :
tar -xvf file.tar.bz2 |& grep "something"
This will also cause the STDERR of tar
to be exhausted.
This is actually a shorthand for :
tar -xvf file.tar.bz2 2>&1 | grep "something"
The pipe forms a separate clause in the command so the redirection in...
tar -xvf file.tar.bz2 | grep "something" 2>/dev/null
...is redirecting the STDERR from grep
, not tar
.
To fix, simply reorder things so your redirect is with your tar
command:
tar -xvf file.tar.bz2 2>/dev/null | grep "something"