Normally in terminal when you run a command, it prints and shows the command output directly in the terminal. If you want to store that output in a external text file, then you can also do it in bash based terminals. Ex:Ubuntu. It redirect the output and saves in a text file. It will help to view the output at later. In this article we will see the all available methods to print the terminal output to a file.
1.Syntax:
command > /path/to/file.txt
In the above command the output redirected to the file and it overwritten the file if it already exists.
In terminal, the output is not visible.
Here you can replace command with your actual command.
Example:
uname -a > /home/manikandan/file.txt
2.Syntax:
command >> /path/to/file.txt
In the above command the output redirected to the file and the new data will get appended to the end of the file, if the file already exists. In terminal, the output is not visible.
Example:
uname -a >> /home/manikandan/file.txt
3.syntax:
command 2> /path/to/file.txt
In the above command the error redirected to the file and it overwritten the file if it already exists.
In terminal, the error is not visible.
Example:
uname -a 2> /home/manikandan/file.txt
4.syntax:
command 2>> /path/to/file.txt
In the above command the error redirected to the file and the new data will get appended to the end of the file, if the file already exists. In terminal, the error is not visible.
Example:
uname -a 2>> /home/manikandan/file.txt
5.syntax:
command &> /path/to/file.txt
Above command both the output and error redirected to the file and it overwritten the file if it already exists. In terminal, the output and error is not visible.
Example:
uname -a &> /home/manikandan/file.txt
6.syntax:
command &>> /path/to/file.txt
Both the output and error redirected to the file and the new data will get appended to the end of the file, if the file already exists. In terminal, the output and error is not visible.
Example:
uname -a &>> /home/manikandan/file.txt
7.syntax:
command | tee /path/to/file.txt
In the above command the output redirected to the file and it overwritten the file if it already exists.
In terminal, the output is visible.
Example:
uname -a | tee /home/manikandan/file.txt
8.Syntax:
command | tee -a /path/to/file.txt
The output redirected to the file and the new data will get appended to the end of the file, if the file already exists. In terminal, the output isvisible.
Example:
uname -a | tee -a /home/manikandan/file.txt
9.Syntax:
command |& tee /path/to/file.txt
Both the output and error redirected to the file and it overwritten the file if it already exists. In terminal, the output and error is visible.
Example:
uname -a |& tee /home/manikandan/file.txt
10.Syntax:
command |& tee -a /path/to/file.txt
Output and error redirected to the file and the new data will get appended to the end of the file, if the file already exists. In terminal, the output and error isvisible.
Example:
uname -a |& tee -a /home/manikandan/file.txt