9

I'm running a memory check tool (prog1) on a C++ code (prog2), both of which give me a huge and detailed output. In order to chase down some bugs I need to save this output to a file.

I tried redirecting the stdout to a file:

prog1 prog2 > outfile.txt

But that gives me a file containing the output of prog2, while the output of prog1 is still going to the terminal.

Anyone know a way to specify that I want BOTH outputs to go to a file? Like (prog1 prog2) > outfile.txt?

19

There’s a good chance that prog1 is writing its output to standard error. You can redirect both outputs to a single file with

prog1 prog2 > outfile.txt 2>&1

or you can split the outputs with

prog1 prog2 > outfile.txt 2> errors.txt

This doesn’t separate the individual programs’ output, it separates the output channels. See What are the shell's control and redirection operators? for details.

If you look at prog1’s documentation, you might find an option to tell it to store its output in a named file instead. For example, with strace,

strace -o strace.txt prog2 > outfile.txt

would store strace’s output in strace.txt, and everything written to standard output in outfile.txt.

  • 5
    2>&1 has to go after > outfile.txt. Order of redirections matter. – Barmar Dec 17 '18 at 17:14
  • Note to future readers: @Barmar is absolutely correct that the relative order of redirections matter with respect to each other. For those less familiar, the order of redirections with respect to program arguments only matters in a few cases. The beginning and end are common locations, but the middle happens sometimes as well (please don't do this). echo >&2 hello world is equivalent to echo hello world >&2. More info here: tldp.org/LDP/abs/html/io-redirection.html – Morgen Dec 18 '18 at 4:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.