Sun. Dec 10th, 2023

Key Takeaways

  • The “tee” command on Linux saves the output of a command to a file while also displaying it on the terminal.
  • It can also write the output to multiple files simultaneously, allowing for easy backup and tracking of system logs.
  • The “tee” command can also be used with other commands through piping. This enables further processing of the output while saving it to a file. For example, you could use it with “ls” in this command: “ls ~ | tee list.txt”

The tee command can save the output of your commands for later review. This command not only displays the output on screen but also records it in a separate log file. Let’s say you want to save the output of the df command to a file so that you can track your disk space usage over time. This command helps you to troubleshoot a process as it maintains a written record of processes.

What Is the tee Command on Linux?

The Linux tee command is a useful tool for saving time and improving efficiency on Linux. It can read the standard input (stdin) and write it to both the standard output (stdout) and the file or files you specify. The tee command works like a T-shaped pipe that splits water into two directions. It lets you view the output of a program and save it in a file simultaneously.

The tee command does both things together. It lets you copy the output to the files or variables you choose and display it to you. This command is also used in shell scripts and terminal commands to send output to different locations. You can use the tee command to make backups, find errors in scripts, and keep track of system logs.

The tee command also lets you pipe it with other commands. This way, you can save the output to a file and also process it further with any other command.

Almost all Linux distributions come with the tee command pre-installed, which is part of 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

Version of tee command on terminal

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

tee --help

tee command help

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:



-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.


Display a basic help related to the command options.


Display the tee command version.

Save Output to a File in Linux Using tee

The tee command saves the output of a command to a file while also displaying it on the terminal. For example, the tee command will let you see the files and directories in your home directory and also save them in a separate file. To do this, pipe the tee command with ls command as follows:

ls ~ | tee list.txt

ls with tee command and save output to a text file

This will display all the files and directories in your home directory and write them to “list.txt”. To view the content of the “list.txt”, use any text editor or command like cat, less, or more command.

cat list.txt

cat command to read a list text file

Let’s perform another example with a echo command to save and view the output. First, use the echo command to print text on the terminal. After that, piped the tee command with the echo command to write the same text to a file called “output.txt”.

echo "Welcome to Ubuntu" | tee output.txt

echo command pipe with the tee command

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

cat output.txt

cat command to read text file content

Write the Output to Multiple Files in Linux Using tee

The tee command can also write output to multiple files simultaneously. You just have to define the file names after the tee command that you want to write to. Simply separate them with 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

echo command piped with tee command with multiple file names

This will write the string “Welcome to Ubuntu” to three files: file1.txt, file2.txt, and file3.txt. It also displays them on the terminal. To view the content of these files, use the cat or head command:

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

head command to read multiple text file content

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

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

cat command to read multiple text file content

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

The tee command on Linux overwrites the file content by default. The -a or --append option with the tee command lets you 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

cat command to read the data from the output.txt file

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

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

echo command piped with a tee and -a option to append output to the existing file

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

cat command that reads the output text file

Hide the Output Using tee

Sometimes, you may want to store the output of a command in a file without showing it on the terminal. This can be useful if you want to run a command silently, without cluttering your terminal with unnecessary output. In such cases, you have to direct the command output to the /dev/null device. The /dev/null is often referred to as a “null device” or “null file.” It acts as a data sink, meaning that any data written to it is discarded and doesn’t actually get stored anywhere.

Here is a way to hide the output of the echo command:

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

echo command piped with tee command and dev null device

This will write the output of a command to output.txt and also send it to /dev/null, which will effectively hide it from the screen. However, the cat command will let you verify the output by viewing the file content:

cat output.txt

cat command to read text file content-1

Redirect Output of One Command to Another Using tee

The tee command can also redirect the output of one command to a file or any other command. The tee command with a pipe (|) will let you send the output of the first command to both the standard output and the second command or file. Consider the following example:

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

echo command piped with tee and wc to redirect the output

The echo command output “Welcome to Ubuntu” is written to the output.txt file. After that, the pipeline operator is used with the tee command. This will pass the file content to the wc command. The wc command will output the total characters counted and display an integer value.

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

cat output.txt

Cat command that reads output text file

Using tee Command with sudo

When you use the tee command, it writes the output of a command to a regular file. However, some files and directories such as system directories or protected files require superuser privileges to modify. To write to these files or files owned by other users, use tee in conjunction with sudo.

In the example below, when you try to write a root-owned “file.conf” file without using sudo, it will give you a permission denied error.

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

echo command piped with tee command without sudo permission

However, when you use the sudo with the tee command, this will run without any error. You can use the sudo command to run the tee command as the root user or the owner of the file. Simply prepend sudo with the tee command:

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

echo command piped with tee command with sudo permission

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 can be useful in various scripting scenarios. It helps you to log or capture the output of a command for further processing or debugging. The tee command will not only display output but also save it to a file or files for later 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:

date | tee log.txt
Bash script to save date to log file

In this case, the standard input is the output of the date command, which shows the current date and time. The tee command writes this output to the terminal and the file log.txt. If the file log.txt does not exist, it will be created. If it exists, it will be overwritten, unless you use the -a option to append to the file.

cat log.txt

Cat command that reads the log file content

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

date | tee log1.txt log2.txt
Bash script to write multiple files using the tee command

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

Cat command that reads multiple log text file content

Let’s consider another simple bash script that takes an input and stores it inside a log file—using the tee command.

echo "Please enter some text:"
read user_input
echo "$user_input" | tee -a "$log_file"
echo "User input has been logged to $log_file"
Bash script that reads user input and displays output to the terminal

In the given bash script, define a variable called “log_file” and assign it the name of the log file you want to use, such as “user_input.log”. Then, use the echo command and the read command to prompt you to enter some text and store it in a variable. Next, use the tee command with the -a option to display the enter input on the terminal and append it to the log file.

Finally, use the echo command again to give feedback. This will tell you that your input has been logged into the file. This way it lets you create a bash script that saves your input to a log file and shows it on the screen.

Run the bash script using bash command.


bash command that run test script

Monitoring Processes on Your Linux System

To keep tabs on how well your Linux system is running, you should observe the activities of its processes. This includes CPU and memory usage, disk I/O, and network activity. Identifying performance bottlenecks helps optimize system resources and ensures that your system operates efficiently.

Like the tee command, Linux has multiple other commands that help you monitor the processes easily. Some of the main commands include ps, top, and pgrep command. Linux’s systems often run multiple processes simultaneously. Using these commands you can prioritize critical tasks, allocate resources appropriately, and prevent resource contention.

Source link

By John P.

Leave a Reply

Your email address will not be published. Required fields are marked *