Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using cppcheck tool to analyze source code.

At the shell prompt in gcc compiler, I have given the command as follows

$ cppcheck abc.cpp  

The output is

checking abc.cpp...   
(error) Possible null pointer dereference: <var> - otherwise it is redundant to check if   <var> is null at line 100    
segmentation fault

I want this output to be saved in a file, so I have given the command as follows

$ cppcheck abc.cpp 2> abc.txt

But the contents of abc.txt file is as follows

checking abc.cpp...  

It is only saving the first line, the main error part is missing in abc.txt file.

Which command should I use to get all the output lines in a file? The manual of cppcheck tool is here.

share|improve this question

Drop the 2 from your command and just use > as the redirect operator.

By using 2> you are redirecting the error stream (STDERR) to a file; what it sounds like you want is the regular output stream (STDOUT).

cppcheck abc.cpp > abc.txt

If you want both streams to go to your file, you can connect 2 (the error stream) to 1 (the standard output stream) them like this:

cppcheck abc.cpp > abc.txt 2>&1

Or use the shortcut syntax lain suggested to just grab everything.

share|improve this answer
Nice one. I'd never seen 2> before. Learn something new every day here. :) – boehj May 3 '11 at 12:04

You probably need to redirect stdout as well as stderr to your file

cppcheck abc.cpp  &>abc.txt
share|improve this answer
@lain this answer is exactly backwards. If you read the question you'll see he is catching just stderr instead of stdout. – Caleb May 3 '11 at 10:00
@Caleb: As you can see I realised that and edited it but the command is the same – Iain May 3 '11 at 10:03
@lain ... and I changed my vote :) I know the solution worked but the explanation was misleading to somebody who clearly doesn't have a grasp on io redirection issues. – Caleb May 3 '11 at 10:14

Caleb is right about the use of > but you can also use the tee command if you would like to see the logs at the same time as you save then to a file.

cppcheck abc.cpp | tee my.file.txt

And it should work with the 2>&1 as well.

cppcheck abc.cpp 2>&1 | tee my.file.txt
share|improve this answer
Why the downvote? don't like the tee command? – Johan May 3 '11 at 10:10
Downvote wasn't me, but it's canceled out now :) Good additional suggestion. I might suggest writing it up with this users exact command though so they see better how your suggestion fits into the big picture. – Caleb May 3 '11 at 10:17
I wouldn't downvote it as it's not wrong, but probably because using tee only for redirection is superfluous. tee only really helps if you have more commands piped after it. – hometoast May 3 '11 at 15:04
@hometoast: You use it to "debug" a long pipe chain. So you later can look at the output from the different stages? Example "find / | tee a.txt | grep -v bin | tee b.txt" and then compare a.txt and b.txt to see what grep -v removed in the middle? Or what did you mean? – Johan May 3 '11 at 15:54
@Johan: ah. I see what you mean now. Clever. – hometoast May 3 '11 at 17:33

Your Answer


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.