
tee is a built-in Linux command that reads from standard input (STDIN) and simultaneously writes it to standard output (STDOUT) and one or more files. It can be seamlessly integrated with other commands through piping, allowing for flexible and customized workflows. The tee command allows you to view the output in real time, unlike redirections, making it ideal for logging or debugging within pipelines.
This article explains how to use the tee command to write and process data using multiple options.
tee Command SyntaxThe tee command follows the syntax below:
Within the above command, OPTIONS modify the command behavior while FILE specifies the target file to write the changes to. Below are the supported tee command options.
-a or --append: Appends the output to a file instead of overwriting it.-i or --ignore-interrupts: Ignores interrupt signals.-h or --help: Displays the command usage and supported options.-v or --version: Displays the tee version information.Follow the basic usage steps below to test the tee command on your Linux workstation.
View the tee command basic usage information.
Output:
View the tee command version.
Your output should be similar to the one below.
Use the -i option to prevent tee command from terminating when receiving an interrupt signal such as Ctrl + C.
The -i option is useful when running long commands to ensure the command is successful without any user interruption. Press Ctrl + Z to terminate the tee command when using the -i (ignore-interrupts) option.
tee CommandYou can use the tee command to create or update files. The -a (append) option updates a file if it exists while running the command without any option overwrites the file if it exists. Follow the steps below to create files using the tee command on your Linux workstation.
Create a new output.txt file using the tee command with a Hello World string.
Output:
View the output.txt file contents and verify that the Hello World string is available.
Output:
Create another file merged.txt using the contents of the output.txt file.
Output:
tee CommandYou can use the tee command to overwrite existing files and replace the existing contents. The tee command overwrites existing files by default unless the append option is specified. Follow the steps below to use the tee command to overwrite files.
View the output.txt file and verify that it includes a Hello World string.
Output:
Overwrite the output.txt file with a new Greetings from Vultr string.
Your output should be similar to the one below:
View the output.txt file and verify the change.
Output:
tee CommandYou can use the tee command to append existing files without overwriting the existing content. Use the -a (append) option to append a file with new contents as described in the following steps.
Append the output.txt file with a new Hello From Vultr Docs string.
Output:
View the output.txt file contents to verify the change.
Output:
tee CommandYou can use the tee command to write similar content to multiple files simultaneously. This is useful when creating or updating multiple files in different locations while displaying the output in your terminal session.
Write to multiple files using the tee command by specifying the file names directly.
For example, create multiple files using the tee command with a The Data Backup Process is Complete string as the common value.
View the file1.txt and file2.txt contents to verify the change.
Output:
Based on the above output, the tee command created multiple files, file1.txt and file2.txt with the same The Data Backup Process is Complete string. You can use the tee command to create, overwrite, or append data in multiple files.
Write multi-line content to multiple files such as test1.txt, test2.txt using the tee command and Heredoc.
The above here document uses the tee command and the <<EOF delimiter to write multi-line content to the test1.txt and test2.txt files.
View the test1.txt and test2.txt file content to verify the change.
Output:
tee Command with Sudo PrivilegesFollow the steps below to use the tee command with sudo to perform write specific files that require administrative or elevated user privileges.
Add a new 192.0.2.1 mycustomhost.local entry to the /etc/hosts file using the tee command with sudo privileges.
The above command appends the 192.0.2.1 mycustomhost.local line to the /etc/hosts file which requires sudo user privileges. Without using the sudo command, you will receive a permission denied error because the /etc/hosts file is owned by the root user.
Output:
View the /etc/hosts file contents to verify the change.
Your output should be similar to the one below.
tee Command with Multiple Commands to Filter and Redirect OutputYou can use the tee command to redirect and capture command output while displaying it on the terminal at the same time. Combining the tee command with other commands is important in scenarios that involve complex pipelines where you need to save or filter the output of a specific command. Follow the steps below to combine the tee command with other commands to filter and redirect output to one or more files.
Pipe the output of the ls command to the tee command to write the results to a file, such as list.txt.
The above command lists files in your working directory and uses the tee command to write the results in a list.txt file.
View the list.txt file contents to verify the change.
Your output should be similar to the one below:
Use the tee command with multiple pipes to filter and redirect output. For example, run the following command to filter the ls command using tee and grep to redirect output to a final.txt file.
The above command pipes the output of the ls command to the tee command which saves it as intermediate.txt. Then, the grep command filters the intermediate.txt output to sort all .txt files and redirects the output to the final.txt file.
View the intermediate.txt file contents to verify the tee command change.
View the final.txt file contents to verify the filtered and redirected output.
Your output should be similar to the one below.
Use the tee command with advanced piping to filter and sort output. For example, use the tee command with awk and sort to filter the active processes to save the final output to a sorted_process_list.txt file.
The above command uses advanced piping to write the active process information using the tee command to a process_list.txt file. Then, the awk command extracts the first and second columns (user and process ID), while the sort command uses the default ascending lexicographical order to write the final output to a sorted_process_list.txt using the tee command.
View the process_list.txt file and verify that the full process information is available.
View the sorted_process_list.txt file and verify that a sorted process information list is available.
You have used the tee command in Linux to create and update files. You can use the tee command to perform complex file modification tasks by combining it with multiple tasks. For more information and command options, run the man tee command to view the command manual page.
0 Comments
Be the first to comment and share your perspective with the community.