Kevin Burke
Kevin Burke

Reputation: 64854

Piping program output to less does not display beginning of the output

I am trying to make a bunch of files in my directory, but the files are generating ~200 lines of errors, so they fly past my terminal screen too quickly and I have to scroll up to read them.

I'd like to pipe the output that displays on the screen to a pager that will let me read the errors starting at the beginning. But when I try

make | less

less does not display the beginning of the output - it displays the end of the output that's usually piped to the screen, and then tells me the output is 1 line long. When I try typing Gg, the only line on the screen is the line of the makefile that executed, and the regular screen output disappears.

Am I using less incorrectly? I haven't really ever used it before, and I'm having similar problems with something like, sh myscript.sh | less where it doesn't immediately display the beginning of the output file.

Upvotes: 20

Views: 8715

Answers (2)

geekosaur
geekosaur

Reputation: 61389

Error output is sent to a slightly different place which isn't caught by normal pipelines, since you often want to see errors but not have them intermixed with data you're going to process further. For things like this you use a redirection:

$ make 2>&1 | less

In bash and zsh (and csh/tcsh, which is where they borrowed it from) this can be shortened to

$ make |& less

With things like make which are prone to produce lots of errors I will want to inspect later, I generally capture the output to a file and then less that file later:

$ make |& tee make.log
$ less make.log

Upvotes: 14

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

The errors from make appear on the standard error stream (stderr in C), which is not redirected by normal pipes. If you want to have it redirected to less as well, you need either make |& less (csh, etc.) or make 2>&1 | less (sh, bash, etc.).

Upvotes: 20

Related Questions