- 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”
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?
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.
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.
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.
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:
If you need help with the syntax and available arguments for the
tee command, type this:
tee Command Options
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:
Append the output to the end of the files instead of overwriting them.
Ignore interrupt signals such as Ctrl+C.
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.
Save Output to a File in Linux Using tee
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
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
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
Finally, use the
cat command to verify the contents of the “output.txt” file.
Write the Output to Multiple Files in Linux Using tee
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
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
head -v file1.txt file2.txt file3.txt
cat command will also display the identical output:
cat -v file1.txt file2.txt file3.txt
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
tee command on Linux overwrites the file content by default. The
--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:
Now, we can append the new data without overwriting it by typing this:
echo "tee Command on Linux" | tee -a output.txt
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
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 "Welcome to Ubuntu" | tee output.txt > /dev/null
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:
Redirect Output of One Command to Another Using tee
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 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:
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
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
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
echo "This is Ubuntu" | sudo tee -a /etc/file.conf
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
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
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.
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
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 log1.txt log2.txt
Let’s consider another simple bash script that takes an input and stores it inside a log file—using the
echo "Please enter some text:"
echo "$user_input" | tee -a "$log_file"
echo "User input has been logged to $log_file"
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
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.
tee command, Linux has multiple other commands that help you monitor the processes easily. Some of the main commands include
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.