Mastering the tee Command in Linux

Mastering the tee Command in Linux

Learn how to effectively track and save Linux command output with the tee command Easily redirect output to a separate file, write to multiple files, append output, hide it, and even redirect output of one command to another Discover how to utilize tee in a bash script and monitor processes on your Linux system

Key Takeaways

The "tee" command on Linux saves the output of a command to a file while also displaying it on the terminal.

The output can also be written to multiple files at the same time using the "tee" command, making it convenient for backing up and monitoring system logs.

In addition, the "tee" command can be utilized with other commands by piping, which permits additional manipulation of the output while simultaneously saving it to a file. For instance, you can employ it with the "ls" command as follows: "ls ~ | tee list.txt".

The tee command facilitates saving the output of commands for future review. In addition to displaying the output on the screen, this command also logs it in a separate file. For example, if you wish to retain the df command's output in a file to monitor disk space usage over time, this command assists in troubleshooting processes by maintaining a written record.

What Is the tee Command on Linux?

The Linux tee command is a valuable tool for improving efficiency and saving time on Linux. It allows you to read the standard input (stdin) and write it simultaneously to both the standard output (stdout) and a specified file or files. Similar to a T-shaped pipe splitting water, tee enables you to view program output while also saving it to a file.

By combining both functionalities, the tee command facilitates copying output to chosen files or variables as well as displaying it to the user. It is frequently employed in shell scripts and terminal commands to direct output to different destinations. With tee, you can create backups, identify errors in scripts, and maintain system logs.

The tee command can be combined with other commands, allowing you to save the output to a file and perform additional processing using various commands. The tee command is pre-installed in most Linux distributions as it is included in the Coreutils package.

The tee command follows a similar syntax as other Linux commands. It has two arguments, --OPTIONS and FILES:

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

To find out which version of the tee command you are using, simply run the following command:

tee --version

Mastering the tee Command in Linux

If you need help with the syntax and available arguments for the tee command, type this:

tee --help

Mastering the tee Command in Linux

tee Command Options

The tee command has several options to modify its functionality. The below table shows a few options that will help you to use the tee command efficiently:

Option

Description

-a or --append

Append the output to the end of the files instead of overwriting them.

-i or --ignore-interrupts

Ignore interrupt signals such as Ctrl+C.

-p or --output-error

Print an error message on standard error for each error that occurs when writing to files.

--help

Display a basic help related to the command options.

--version

Display the tee command version.

Save Output to a File in Linux Using tee

Use the tee command to display and save the output of a command simultaneously. For instance, you can use tee to view the files and directories in your home directory while also storing them in a separate file. To accomplish this, simply pipe the ls command into tee, as shown below:

ls ~ | tee list.txt

Mastering the tee Command in Linux

This command will show you a list of all the files and directories in your home directory and save them to a file called "list.txt". To view the contents of the "list.txt" file, you can use a text editor or commands such as cat, less, or more.

cat list.txt

Mastering the tee Command in Linux

Perform another example by using the echo command to display text on the terminal and simultaneously save it in a file named "output.txt".

Mastering the tee Command in Linux

Finally, use the cat command to verify the contents of the "output.txt" file.

cat output.txt

Mastering the tee Command in Linux

Write the Output to Multiple Files in Linux Using tee

In addition to writing output to a single file, the tee command offers the capability to simultaneously write output to multiple files. To achieve this, simply specify the desired file names after the command, separated by spaces.

For example, to save the output of the echo command to three different files, use the following syntax:

echo "Welcome to Ubuntu" | tee file1.txt file2.txt file3.txt

Mastering the tee Command in Linux

The string "Welcome to Ubuntu" will be written to three files, namely file1.txt, file2.txt, and file3.txt, and will also be displayed on the terminal. To access the content of these files, you can utilize the cat or head command.

head -v file1.txt file2.txt file3.txt

Mastering the tee Command in Linux

Similarly, the cat command will also display the identical output:

cat -v file1.txt file2.txt file3.txt

Mastering the tee Command in Linux

You can write the output to any number of files with the tee command. Just type the file names after the tee command with spaces between them.

Append Output to a File Using tee

By default, the tee command on Linux overwrites the content of a file. However, you can use the -a or --append option with the tee command to append the output to the end of the files instead of replacing their contents.

Before appending data to the file, let’s check the present data placed in the file using the below command:

cat output.txt

Mastering the tee Command in Linux

Now, we can append the new data without overwriting it by typing this:

echo "tee Command on Linux" | tee -a output.txt

Mastering the tee Command in Linux

This will append the output of a command to the end of output.txt, without deleting any previous content in it. To verify, run the cat command:

cat output.txt

Mastering the tee Command in Linux

Hide the Output Using tee

To hide the output of the echo command, you can redirect it to the /dev/null device.

echo "Welcome to Ubuntu" | tee output.txt > /dev/null

Mastering the tee Command in Linux

The output of a command can be redirected to both output.txt and /dev/null. This will effectively conceal it from the screen. However, you can use the cat command to inspect the file's content and verify the output.

Mastering the tee Command in Linux

Redirect Output of One Command to Another Using tee

The tee command allows you to redirect the output of a command to a file or another command. By using a pipe (|) with tee, you can send the output of the initial command to both the standard output and a subsequent command or file. Here is an example:

echo "This is Ubuntu" | tee output.txt | wc -c

Mastering the tee Command in Linux

The output.txt file contains the result of the echo command, which writes "Welcome to Ubuntu". To count the total characters in the file, the pipeline operator is utilized with the tee command. This passes the file content to the wc command which outputs an integer value representing the total character count.

To verify if the tee command has also written output to a file, use the cat command to show the file

cat output.txt

Mastering the tee Command in Linux

Using tee Command with sudo

When using the tee command, it is possible to write the output of a command to a regular file. Nonetheless, certain files and directories, such as system directories or protected files, necessitate superuser privileges for modification. To overwrite these files or files owned by other users, employ tee in conjunction with sudo.

In the following example, attempting to write to a "file.conf" file owned by the root user without utilizing sudo will result in a permission denied error.

echo "This is Ubuntu" | tee -a /etc/file.conf

Mastering the tee Command in Linux

Using the sudo command with the tee command allows for error-free execution. By running sudo tee as the root user or file owner, you can append the text "This is Ubuntu" to the /etc/file.conf file.

Mastering the tee Command in Linux

First, the tee command will take the echo command output. After that, it elevates to sudo permissions and writes the text to the file.

Examples of Using tee in a Bash Script

The tee command is a valuable tool for a variety of scripting scenarios. It enables you to conveniently log or capture the output of a command, facilitating further processing or debugging. In addition to displaying the output, the tee command allows you to save it to a file or multiple files for future use.

For example, if you want to see the date and time on the terminal and also write it to a file named log.txt, use the following bash script:

#!/bin/bashdate | tee log.txt

Mastering the tee Command in Linux

The standard input in this scenario is the output generated by the date command, which displays the current date and time. By utilizing the tee command, this output is simultaneously directed to both the terminal and the file log.txt. In case the log.txt file is not present, it will be created. However, if it already exists, it will be overwritten, unless the -a option is used to append to the file.

cat log.txt

Mastering the tee Command in Linux

You can also use the tee command to write to multiple files by specifying more file names as arguments.

#!/bin/bashdate | tee log1.txt log2.txt

Mastering the tee Command in Linux

This script prints the date and time to the terminal and to two files named log1.txt and log2.txt. Read both files content using cat command.

cat log1.txt log2.txt

Mastering the tee Command in Linux

Consider this simple bash script that prompts the user for input and saves it in a log file using the tee command.

#!/bin/bash

log_file="user_input.log"

echo "Please enter some text:"

read user_input

echo "$user_input" | tee -a "$log_file"

echo "User input has been logged to $log_file"

Mastering the tee Command in Linux

In this bash script, you can create a variable called "log_file" and set it to the desired log file name, for example, "user_input.log". Afterwards, utilize the echo command and the read command to prompt for some text input and save it in a variable. Then, employ the tee command with the -a option to both display the entered input on the terminal and append it to the log file.

To conclude, employ the echo command once more to provide feedback, indicating that your input has been successfully logged into the file. This approach enables the creation of a bash script that both saves your input to a log file and displays it on the screen. Execute the bash script using the bash command.

bash test.sh

Mastering the tee Command in Linux

Monitoring Processes on Your Linux System

In order to monitor the performance of your Linux system effectively, it is important to closely monitor the activities of its processes. This involves keeping track of CPU and memory usage, disk I/O operations, and network activity. By identifying any bottlenecks in performance, you can optimize your system's resources and ensure efficient operation.

Linux offers several commands, such as ps, top, and pgrep, which, like the tee command, facilitate the monitoring of processes. Given that Linux systems often handle multiple processes concurrently, utilizing these commands enables efficient task prioritization, proper resource allocation, and the prevention of resource conflicts.