Quick Links
Summary
Theteecommand can save the output of your commands for later review. This command not only displays the output on screen but also records it in aseparate log file. Let’s say you want to save the output of thedfcommandto 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 Linuxteecommand is a useful tool for saving time and improving efficiency on Linux. It can read thestandard input (stdin)and write it to both thestandard output (stdout)and the file or files you specify. Theteecommand 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.
Theteecommand 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 inshell scriptsand terminal commands to send output to different locations. You can use theteecommand tomake backups, find errors in scripts, and keep track of system logs.
Theteecommand also lets youpipe 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 theteecommand pre-installed, which is part of the Coreutils package.
Theteecommand follows a similar syntax as other Linux commands. It has two arguments, –OPTIONS and FILES:
To find out which version of theteecommand you are using, simply run the following command:
If you need help with the syntax and available arguments for theteecommand, type this:
tee Command Options
Theteecommand has several options to modify its functionality. The below table shows a few options that will help you to use theteecommand efficiently:
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
–version
Display theteecommand version.
Save Output to a File in Linux Using tee
Theteecommand saves the output of a command to a file while also displaying it on the terminal. For example, theteecommand will let you see thefiles and directories in your home directoryand also save them in a separate file. To do this, pipe theteecommand withlscommandas follows:
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 likecat,less, ormorecommand.
Let’s perform another example with aechocommandto save and view the output. First, use theechocommand to print text on the terminal. After that, piped theteecommand with theechocommand to write the same text to a file called “output.txt”.
Write the Output to Multiple Files in Linux Using tee
Theteecommand can also write output to multiple files simultaneously. You just have to define the file names after theteecommand that you want to write to. Simply separate them with spaces.
For example, to save the output of theechocommand to three different files, use the following syntax:
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 thecatorheadcommand:
Similarly, thecatcommand will also display the identical output:
You can write the output to any number of files with theteecommand. Just type the file names after theteecommand with spaces between them.
Append Output to a File Using tee
Theteecommand on Linux overwrites the file content by default. The-aor–appendoption with theteecommand 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:
This will append the output of a command to the end of output.txt, without deleting any previous content in it. To verify, run thecatcommand:
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/nulldevice. The/dev/nullis 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 theechocommand:
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, thecatcommand will let you verify the output by viewing the file content:
Redirect Output of One Command to Another Using tee
Theteecommand can also redirect the output of one command to a file or any other command. Theteecommand 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:
Theechocommand output “Welcome to Ubuntu” is written to the output.txt file. After that, the pipeline operator is used with theteecommand. This will pass the file content to thewccommand. Thewccommand will output the total characters counted and display an integer value.
To verify if theteecommand has also written output to a file, use thecatcommand to show the file content:
Using tee Command with sudo
When you use theteecommand, 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, useteein conjunction withsudo.
In the example below, when you try to write a root-owned “file.conf” file without usingsudo, it will give you a permission denied error.
However, when you use thesudowith theteecommand, this will run without any error. You can use thesudocommand to run theteecommand as the root user or the owner of the file. Simply prependsudowith theteecommand:
First, theteecommand will take theechocommand output. After that, it elevates tosudopermissions and writes the text to the file.
Examples of Using tee in a Bash Script
Theteecommand can be useful in various scripting scenarios. It helps you to log or capture the output of a command for further processing or debugging. Theteecommand 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 followingbash script:
In this case, the standard input is the output of the date command, which shows the current date and time. Theteecommand 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-aoption to append to the file.
you’re able to also use theteecommand to write to multiple files by specifying more file names as arguments.
This script prints the date and time to the terminal and to two files named log1.txt and log2.txt. Read both files content usingcatcommand.
Let’s consider another simple bash script that takes an input and stores it inside a log file—using theteecommand.
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 theechocommand and thereadcommand to prompt you to enter some text and store it in a variable. Next, use theteecommand with the-aoption to display the enter input on the terminal and append it to the log file.
Finally, use theechocommand 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.
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 theteecommand, Linux hasmultiple other commandsthat help you monitor the processes easily. Some of the main commands includeps,top, andpgrepcommand. Linux’s systems often run multiple processes simultaneously. Using these commands you can prioritize critical tasks, allocate resources appropriately, and prevent resource contention.