Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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
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
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
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
Opening Help Documentation
We can read the documentation in the terminal itself using the below command:
Syntax
tee --help
Output
Checking Version
We can check the version of the Tee command in Linux using the below command:
Syntax
tee --version
Output
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:
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: