Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am compiling a program using make and want the output of make to be written to a file. I have tried using > operator like

make > build_log.txt

and using tee command like

make | tee build_log.txt

but the problem is that some of the ouput goes into the file but rest keeps appearing on the screen.

I can simply copy/paste the text from terminal into a file after running make but that is not a solution.

So my question is how do I save i.e redirect all the output to file so that it goes into file only without appearing on screen.

share|improve this question

1 Answer 1

up vote 9 down vote accepted

The text that is displayed in the terminal comes from the stderr stream (2). If you do just make > build_log.txt, only the stdout (1) stream is redirected to the build_log.txt file.

  • stdout is the standard output stream and has file descriptor number 1. This is the default stream being redirected in shells.
  • stderr is the standard error stream and has file descriptor number 2

To redirect the stderr stream to that build_log.txt file too, use:

make > build_log.txt 2>&1
  • make is executed and
    • the stdout stream is redirected (>) to build_log.txt
    • the stderr stream is redirected (2>) to the stdout stream (&1), which was redirected to build_log.txt

The order is important, you cannot switch switch the redirection operators like make 2>&1 > build_log.txt.

Alternative command:

make 2>&1 | tee build_log.txt > /dev/null

The redirection to /dev/null is needed to hide output, tee writes its input to build_log.txt and outputs it too.

share|improve this answer
    
can you plz explain '2>&1'. is 2 on commandline always going to mean error stream? and why is & used with 2 but not 1. –  binW Jun 16 '11 at 12:08
    
@binW: I've updated it, is it more clear now? –  Lekensteyn Jun 16 '11 at 12:16
    
only a small thing is unclear :( why use &1. If stdout is 1 then shouldnt 2>1 do the job instead of 2>&1. when is & used with stream number? –  binW Jun 16 '11 at 12:21
1  
2>1 would redirect the standard error stream to a file named 1, where 2>&1 redirects to the standard output stream. & makes the distinction between a file name and a file descriptor number. –  Lekensteyn Jun 16 '11 at 12:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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