Sending Both Output and Error Messages to Different Files
Problem
You are expecting output from a program but you don’t want it to get littered with error messages. You’d like to save your error messages, but it’s harder to find them mixed among the expected output.
Solution
Redirect output and error messages to different files.
$ myprogram 1> messages.out 2> message.err
Or more commonly:
$ myprogram > messages.out 2> message.err
Discussion
This example shows two different output files that will be created by the shell. The first, messages.out, will get all the output from the hypothetical myprogram redirected into it. Any error messages from myprogram will be redirected into message.err.
In the constructs 1> and
2> the number is the file
descriptor, so 1 is STDOUT and 2 is STDERR. When no number is specified,
STDOUT is assumed.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access