tee Command in Linux to Manage Command Output
Linux Terminal is a valuable advantage because it is a powerful component of Linux, making Linux system management more flexible and efficient for administrators. That’s why most experienced and expert Linux users prefer using the terminal to interact with and manage the Linux system over a graphical user interface (GUI). While using the Linux terminal may initially seem challenging and complex for beginners, learning comprehensive Linux commands improves the user experience and increases productivity. Therefore, novice users who have recently started using the Linux system or intend to buy Linux VPS can effectively manage their Linux system by learning Linux commands. This enables them to interact with the Linux system without challenges and optimize it for their goals.
Every command executed in the Linux terminal provides output in the standard output (stdout) stream. Sometimes, administrators need to inspect, archive, and save the output of a command for debugging and troubleshooting purposes. The tee command in Linux fulfills this need by allowing the redirection of the output of a command to a file.
The tee command in Linux is used to display the standard output of a command and save the output of a command to a file. This allows you to archive and save the output of a command while still viewing it on the screen, enabling you to identify error sources and issues. This article teaches how to use the tee command in Linux to manage the command output. By the end of this article, you will become proficient as a Linux system administrator in utilizing the Linux tee command to meet your specific needs.
What is tee command?
The Linux tee command captures the standard input (stdin) provided through the keyboard or a program, displays it as standard output (stdout), and simultaneously saves it to a specified file. While the output is visible on the terminal screen, it is also archived in a specified file. The tee command acts as a valuable solution for archiving and storing the output of commands in a file, allowing for examination of potential errors and troubleshooting. The tee command in Linux is typically used in a pipeline, and its default behavior can be modified when combined with various commands.
tee command basic syntax
The basic syntax of the tee command in all Linux distributions including Debian, Ubuntu, CentOS, Fedora, etc. is as follows:
<Command> | tee <options> <filename>
- Command: Indicates the command whose output you want to capture.
- Options: Additional options of tee command that customize its behavior.
- Filename: Replace this field with the name of the file in which you want the tee command to store the output. If the specified file already exists, the tee command will overwrite its contents; otherwise, a new file is created. Always specify at least one file to store the output of a command after the tee command.
For example, if you want to list files in a directory and save the generated list in a file named “file_list.txt,” execute the following command:
ls | tee file_list.txt
another example, if you want to obtain information about network interface configurations in Linux, you can combine the ifconfig
command with tee
:
ifconfig | tee network_info.txt
The Linux ifconfig command is used to display information about all network interfaces. By executing the previous command, the output of the ifconfig command is piped (|) to the tee command, and in addition to displaying network information as standard output in the terminal, the same information is saved in the “network_info.txt” file. The “network_info.txt” file is automatically created if it does not exist.
To ensure that the tee command has written the output of the ifconfig command to the “network_info.txt” file, use the cat
command:
cat network_info.txt
The cat command is used to display the file content and manipulate the file in Linux. We have already provided a comprehensive guide to learn how to use the cat command in Linux.
tee command options
Like other Linux commands, the tee command supports various options you can use to modify its behavior:
Options | Description |
---|---|
-a or --append | Instead of overwriting the file content, appends the output to the file. |
-i or --ignore-interrupts | Ignores interrupt signals such as Ctrl+C during the execution of the tee command. |
--version | Displays version information for tee command. |
--help | Displays the help and available options along with their descriptions. |
-p | Prints an error message in case of a process failure. |
Basic Usage of tee command in Linux
The primary purpose of executing the tee command in Linux is to redirect and save the output of a command to a file. However, the tee command is versatile and is combined with other commands and additional options for various scenarios. In the following examples, you will comprehensively learn how to use tee command in ubuntu and other distributions.
Saving output to Multiple Files
The tee command can simultaneously write the output of a command to multiple files. To achieve this, use the following command:
<command> | tee <option> <filename1> <filename2>...
By running this command, the command’s output is sent to as many files as you specify. For example, if you want the tee command to simultaneously write the output of the ls
command to two files, “file1.txt” and “file2.txt,” execute the following command:
ls | tee file1.txt file2.txt
Appending output to a File using Ubuntu tee command
If you specify a file that already exists when using the tee command, by default, the tee will overwrite the existing file’s content. To change the default behavior of tee and append the command output to the end of the existing file, use the “-a” or “–append” option. For this purpose, use the following syntax:
<command> | tee -a <filename>
For example, if you want to create a list of files and append this list to the content of the existing “file_list.txt” file, use the following command:
ls | tee -a file_list.txt
Then, you can use the cat command to check whether the list of files has been appended to the end of the “file_list.txt” file.
Ignoring Interrupts Signal
Interrupt signals such as CTRL+C, commonly used to terminate a process, can disrupt the execution of the tee command. To prevent interruptions and disturbances during the execution of the tee command, you can use the “-i” argument with tee command in Ubuntu and other Linux distributions:
<command> | tee -i <filename>
For example, to allow the tee command to save the output of the ping
command to the “output.txt” file while ignoring any interruption signals from CTRL+C, execute the following command:
ping OperaVPS.com | tee -i output.txt
As a result, even if an interruption signal with Ctrl+C exists, the ping command successfully performs its task, and its output is saved by the tee command in the “output.txt” file.
Combining tee with sudo
Sometimes, executing a command requires elevated privileges, meaning it needs sudo privileges. The tee command has the capability to be combined with the sudo command, allowing you to write the output of a command to a specific file owned by the root user. Combining tee
with sudo
is a helpful way for writing to files owned by the root user or another user. When attempting to write to a file owned by the root user or another user, you may encounter a “Permission denied” error. Combining the sudo
and tee
commands provide a solution to this error. In such cases, you can use tee with sudo like this:
<command> | sudo tee <options> <filename>
For example, if you want to append the list of files to the “root_document.txt” file, which belongs to the root user, you should use the following command:
ls | sudo tee -a root_document.txt
As a result, by executing the previous command, you can successfully append a list of files to the content of a file owned by the root user.
Suppressing the output
The default behavior of the tee command in linux is to display the output of a command as standard output and simultaneously save it in a file. In some scenarios, you may prefer to save the output of a command directly to a specific file without displaying it in the terminal. In such cases, to hide the output, you need to redirect it to the /dev/null device:
command | tee options filename >/dev/null
For example, if you want to save the list of files to the “file_list.txt” file and suppress the standard output of the command, use the following command:
ls | sudo tee file_list.txt >/dev/null
This command skips the output, and you won’t receive the output in the terminal. However, you can use the cat
command to ensure save the output in the file_list.txt file:
cat file_list.txt
Integration of tee command and Vim Editor
When a file is owned by the root user, as a regular user without sudo privileges, you cannot edit its content, and you will encounter an error like “Can’t open file for writing“. The tee command in ubuntu provides a useful solution for fixing such errors. In these situations, you need to execute the following command in the Vim text editor:
:w !sudo tee %
Output:
[sudo] password for user:
Some text
Added text
Some more text
W12: Warning: File " example.txt" has changed and the buffer was changed in Vim as well; see ": help W12" for more info.
Press Enter or type command to continue
As a result, after entering the sudo password, the Privileged file will open and be ready for updating. Although you may encounter warning messages, you can proceed with editing the file.
Forward output as input with Linux tee command
As mentioned, the tee command is used for various scenarios, and sometimes, you may want to use the output of one command as the input for another in addition to saving the output of a command. In such cases, you can use the tee command. The tee command combined with the pipe operator (|) supports forwarding the output of one command as the input of another command. You can do this using the following command:
<command> | tee <options> <filename> | <command>
For example, if you are searching for an “error” pattern among all files and directories, you can use the following command:
ls | tee error.text |grep "error"
By running the previous command, you saved a list of files and directories in the “error.txt” file and used this content as input for the grep command. As you know, the grep command in Linux is used to search and match patterns within text files. Therefore, the grep command, when applied to the output of the ls command, finds and presents all instances of “error.”
Printing error messages when diagnosing errors
You can use the “-p” argument to instruct the tee command to display an error message if the process fails and an error writing to a pipe is detected:
<command> | tee -p <filename>
Conclusion
The tee command in Linux is a powerful command-line tool for displaying the standard output of a command and redirecting the output to a file. The tee command can cover various scenarios and is versatile for different purposes. By reading this tutorial guide, you have now become more proficient in managing the output of commands and learned how to use the tee command for various objectives in different Linux distributions such as Ubuntu, Debian, CentOS, Fedora, etc. Running the tee --help
command will provide you with insights into additional options for the tee command. If you need further guidance, feel free to share your questions in the comments section.
Thank you for your choice.