Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 852
morpheous Avatar asked May 15 '10 13:05

morpheous


People also ask

How do you pipe the output of a command to a file?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do I save output to a file in Linux?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!

How do you pipe a command in Linux?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

How do you append the output of a command to a file in Linux?

Append Text Using >> Operator The >> operator redirects output to a file, if the file doesn't exist, it is created but if it exists, the output will be appended at the end of the file. For example, you can use the echo command to append the text to the end of the file as shown.


2 Answers

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 
like image 170
John Kugelman Avatar answered Oct 05 '22 23:10

John Kugelman


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.

like image 25
Epcylon Avatar answered Oct 06 '22 01:10

Epcylon