In this tutorial, we will show and explain how to pipe output from a command to a file in Linux.
Saving a command’s output to a file in Linux is made incredibly simple thanks to redirection. To redirect a command’s output to a file, you only need to use the greater than symbol (>
).
Since you are using redirection, Linux will not display the redirected output within the terminal.
Even without redirection, you can use a Linux pipe to send the command results to a command like tee
, which will, in turn, save the contents to a file. The vertical bar symbol (|
) identifies a pipe in the Linux terminal.
The big difference between a pipe and redirection is that the output will still be sent to the terminal. To stop the result from being printed to the terminal, you will need to employ redirection.
Over the following sections, we will explore two different ways that you can pipe the output from the terminal to a file.
Using Linux’s Redirection to Save a Command Output to a File
Let us start by exploring the most common way to save your command output to a file in Linux: using the redirection feature of the Bash terminal.
The symbol used for redirection is the greater than symbol. With this any output from the left side will be directed to a file specified on the right side.
There are two different ways you can write the redirection symbol. These change the behavior of how Linux pipes the output of a specific command.
>
– With a single greater-than symbol, Linux will pipe the output of a command to the specified file. If that file already exists, it will be overwritten, and the original contents will be wiped.>>
– Using two greater than symbols change the behaviour of the redirection. Instead of overwriting the file, Linux will instead append the piped command to the specified file.
Over the following few sections, we will be showing you a few of the different ways that you can use this symbol to
Redirecting Standard Output to a File using the Linux Terminal
By default, redirection in Linux will only redirect the standard output of a command. So, if that command throws an error to “stdrerr
” it will not be saved to the file.
To redirect the output of a command to a file, you just need to specify the command, the greater than symbol, followed by the name of the file.
COMMAND > FILENAME
The double greater than symbol works the same way and will append any errors to your specified file.
COMMAND >> FILENAME
Only Pipe Errors to a File
As mentioned in the previous section, Linux will by default, only redirect the standard output of a command to a file. Luckily, redirection allows you to pipe the standard error to a specified file.
All you need to do to get Linux to pipe the error output of a command to a file is to user the number two directly before the greater than symbol.
COMMAND 2> FILENAME
The double greater than symbol works the same way and will append any errors to your specified file.
command 2>> FILENAME
Redirect Bash Output and Errors to File in Linux
Linux also allows you to use redirection to pipe the standard output and error output to a specified file. To combine these outputs and save them to a file, you will want to use the ampersand symbol (&
) directly before the greater than symbol.
This will pipe any error or standard output directly to your chosen file. Since we are using a single symbol, Linux will overwrite the file’s contents.
COMMAND &> FILENAME
If you prefer to save all of the output, you can change it to append the output using two greater-than symbols.
COMMAND &>> FILENAME
Pipe Output of a Command to a File in Linux
Now that we have seen how to use redirection to save a command’s output to a file in Linux let’s explore using a pipe to achieve the same thing.
In Linux, a pipe redirects the output of one command to another. For example, you can use this to output the contents of a command to a tool like grep to get just what you need.
Since a pipe allows us to redirect the output of a command, we can use it to pipe the contents of a command to a tool like tee, which will allow us to save the output.
With this, you will want to specify the command that you want to save the output of, followed by the pipe symbol (|
), the tee command, and the file where you want to save the output.
Please note that by default, tee will overwrite the contents of the chosen file.
COMMAND | tee FILENAME
For example, save the text “PiMyLifeUp
” to a file called “examplefile
“.
echo "PiMyLifeUp" | tee examplefile
If we want to append the text “Append Example
” to a file called “examplefile
“, we just need to include the “-a
” option.
echo "Append Example" | tee -a examplefile
Conclusion
Hopefully, at this stage, you will now have a good understanding of how to pipe a command’s output to a file in Linux.
Most terminals on Linux operating systems come packed with features that make saving the output of commands easy.
Please feel free to drop a comment below if you have any questions or concerns about redirecting output to a specified file.
If you liked this tutorial, we highly recommend exploring our many other Linux guides.