morpheous
morpheous

Reputation: 17016

How to pipe the output of a command to file on Linux

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.

Upvotes: 22

Views: 83403

Answers (5)

John Kugelman
John Kugelman

Reputation: 362187

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

Upvotes: 50

simranjeet
simranjeet

Reputation: 165

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.

Upvotes: 1

Epcylon
Epcylon

Reputation: 4737

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.

Upvotes: 4

GregMeno
GregMeno

Reputation: 31

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

Upvotes: 1

Delan Azabani
Delan Azabani

Reputation: 81492

Use 2> rather than just >.

Upvotes: 1

Related Questions