22
votes

I am running a task on the CLI, which prompts me for a yes/no input.

After selecting a choice, a large amount of info scrolls by on the screen - including several errors. I want to pipe this output to a file so I can see the errors. A simple '>' is not working since the command expects keyboard input.

I am running on Ubuntu 9.1.

5
What is the name of the command are you running? make? - Cristina

5 Answers

49
votes
command &> output.txt

You can use &> to redirect both stdout and stderr to a file. This is shorthand for command > output.txt 2>&1 where the 2>&1 means "send stderr to the same place as stdout" (stdout is file descriptor 1, stderr is 2).

For interactive commands I usually don't bother saving to a file if I can use less and read the results right away:

command 2>&1 | less
4
votes
echo yes | command > output.txt

Depending on how the command reads it's input (some programs discard whatever was on stdin before it displays it's prompt, but most don't), this should work on any sane CLI-environment.

1
votes

Use 2> rather than just >.

1
votes

If the program was written by a sane person what you probably want is the stderr not the stdout. You would achieve this by using something like

foo 2> errors.txt

1
votes

you can use 2> option to send errors to the file.

example:

command 2> error.txt

(use of option 2>) --- see if their would be any error while the execution of the command it will send it to the file error.txt.