Search code examples
unixstdoutstderr

stdout to a file , stderr to a command


How should I do it?

Let's say I use ls / /NotExisting And I want the result of / to go to a file results.txt and the /NotExisting ( assuming this is an error ) to go to a command cksum

I can do that separately like ls / /NotExisting > results.txt

and ls / /NotExisting 2> >(cksum) which gives me something similar of what I want to achieve, but not quite there yet. So how do I join these two lines together?


Solution

  • ls / /NotExisting 2>&1 >results.txt | cksum
    

    You know how if you want to send both stdout and stderr to a file, you need to write
    somecommand >file 2>&1 and the other order doesn't work? Well the way that it "doesn't work" is exactly what you want for this example.

    First, this command redirects stderr to where stdout is currently going (which will be the input of the pipe to cksum), then it redirects stdout to results.txt (which will not affect stderr).