Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Tee Command in Linux
3.
tee Command Options
4.
How to Use Tee Command in Linux
5.
Examples of Tee Command
5.1.
Writing Text in a File from Another File
5.2.
Appending Text in a File
5.3.
Writing in Multiple Files at Once
5.4.
Hiding the Output
5.5.
Opening Help Documentation
5.6.
Checking Version 
6.
Some Practical Applications
6.1.
Logging 
6.2.
Monitoring and Analysis
6.3.
Collaborative Editing
6.4.
Backup and Data Redundancy
7.
Use tee Command with Bash Script
8.
Frequently Asked Questions
8.1.
What is Coreutils Ubuntu?
8.2.
What does Sudo Tee mean?
8.3.
What is the difference between script and tee?
8.4.
Does Tee create a new file?
8.5.
What is Option T in Linux?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

Tee Command in Linux

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hello Ninjas! Linux has a bunch of powerful commands which help users to do their tasks efficiently. One of them is the Tee command in Linux. While its name may sound simple, the tee command is a hidden gem that enables users to redirect command output to both the screen and a file simultaneously. 

tee command in linux

In this article, we will explore its functionality, practical applications, and potential to streamline our Linux experience. For more such commands, visit this blog.

Tee Command in Linux

The primary function of the Tee command is to read input from stdin and write them on stdout and files simultaneously. It can also be combined with other commands in a pipeline. This means that you can direct the output of a preceding command to the tee, which in turn writes it to both the screen and the specified file(s). This capability allows seamless integration with other powerful Linux commands, enabling complex data manipulation and analysis workflows.

Syntax

tee [OPTION]... [FILE]...


Explanation

Let’s understand the above syntax:

  • Tee is the keyword itself.
     
  • OPTION refers to the various flags and options that can modify the command's behavior.
     
  • FILE represents the name(s) of the file(s) where the output will be redirected.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

tee Command Options

Here are some options used in conjunction with the tee command as described in the syntax:

Option Use
-a, --append It appends the given data to the specified files. 
-p It is used to diagnose errors.
-i, --ignore-interrupts It ignores the interrupt signals.
--help It is used to open documentation of tee.
--version It displays the version information.

How to Use Tee Command in Linux

The tee command in Linux is used to read from standard input and write to standard output and files simultaneously. It allows you to redirect the output of a command to both the terminal (or another command) and one or more files. The basic syntax is:

command | tee [OPTION]... [FILE]...

 

Here's a simple example to illustrate how to use the tee command:

echo "Hello, Tee!" | tee output.txt

Examples of Tee Command

The Tee command primarily works in a pipeline with other commands. The cat command returns the contents of a text file. The below examples show the same:

Writing Text in a File from Another File

We can copy the exact text from one text file to another text file while displaying the exact text as output using the below command:

Syntax

cat test1.txt | tee test2.txt


Output

copying from another file

Appending Text in a File

We can append any text in a file and display the same in the terminal using the below command: 

Syntax

echo 'Hello Ninjas!' | tee -a test3.txt


Output

appending to file

Writing in Multiple Files at Once

We can also write exact text in multiple files at once while displaying the same at the terminal using the below command:

Syntax

echo "More Hello Ninjas" | tee test4.txt test5.txt test6.txt


Output

writing multiple files

Hiding the Output

If we only want to write in a file and prevent the text from getting displayed, it can be done using the below command:

Syntax

echo "Invisible Hello!" | tee text7.txt >/dev/null


Output

hiding output

Opening Help Documentation

We can read the documentation in the terminal itself using the below command:

Syntax

tee --help


Output

help command

Checking Version 

We can check the version of the Tee command in Linux using the below command:

Syntax

tee --version


Output

version check

Some Practical Applications

The tee command finds applications in various scenarios, some of which include:

Logging 

Tee is commonly used for logging output from scripts, programs, or system processes. By capturing the output and saving it to a log file, users can review it later for troubleshooting, debugging, or performance analysis.

Monitoring and Analysis

Real-time monitoring of system logs, network traffic, or any continuously updating data can be achieved by piping the output to Tee. This enables simultaneous display on the terminal and saving to a file for further analysis.

Collaborative Editing

Tee facilitates collaborative editing of files, where multiple users can view and modify the same file simultaneously. By using Tee to redirect the output to a shared file, users can conveniently see each other's changes in real-time.

Backup and Data Redundancy

When performing critical operations or running commands with potential data loss risks, redirecting output to multiple files using Tee ensures data redundancy and safeguards against accidental data loss.

Use tee Command with Bash Script

Using the tee command in a Bash script can be particularly useful when you want to capture both the output of a command and save it to a file. Here's an example Bash script that utilizes tee:

#!/bin/bash
# Example Bash Script using tee
# Define a function that performs some operations
function perform_operations() {
   echo "Executing operations..."
   echo "Operation 1"
   echo "Operation 2"
}
# Redirect the output of the function to both the terminal and a file
perform_operations | tee output.log
# Display a message indicating the completion of the script
echo "Script execution complete."

 

Explanation of the script:

  • Function Definition: The script defines a function perform_operations that simulates some operations by printing messages to the standard output.
  • Perform Operations Function: Inside the function, messages for "Operation 1" and "Operation 2" are echoed to the standard output.
  • tee Command: The perform_operations function is called, and its output is piped to the tee command. tee output.log redirects the output to both the terminal and the file named "output.log."
  • Script Completion Message: After the tee command, the script echoes a message indicating the completion of the script.

 

Now, when you run this script, you'll see the output of the operations both on the terminal and in the "output.log" file. For example:

$ ./example_script.sh
Executing operations...
Operation 1
Operation 2
Script execution complete.

 

And the content of "output.log" will be:

Executing operations...
Operation 1
Operation 2

Frequently Asked Questions

What is Coreutils Ubuntu?

Coreutils Ubuntu is a package in Ubuntu that provides essential command-line utilities for performing basic file, text, and shell operations.

What does Sudo Tee mean?

"Sudo Tee" is a command used in Linux to redirect the output of a command to both the terminal and a file with root privileges. It allows you to write to a file that requires elevated permissions.

What is the difference between script and tee?

The "script" command records everything printed on the terminal to a file, including both input and output, while "tee" only captures the output and sends it to a file or another command.

Does Tee create a new file?

Yes, the tee command can create a new file if the specified file doesn't exist. If the file already exists, "tee" will overwrite its contents by default.

What is Option T in Linux?

In Linux, the "-t" option is often used with various commands to specify the destination or target of an operation.

Conclusion

The tee command in Linux is a powerful tool that brings efficiency, flexibility, and convenience to the command line interface. With its ability to duplicate output to both the screen and specified files, tee offers numerous applications for logging, monitoring, collaborative editing, and data redundancy. 

For more information on Linux features, read these blogs:

You may refer to our Guided Path on Code Studios to enhance your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninjas!

Previous article
Linux - Commands List
Next article
Chmod command in Linux with Examples
Live masterclass