How to use tee command in Linux

How to use tee command in Linux
Sometimes we need to store the command output into a file to use the output later for other purposes. `tee` command is used in Linux for writing any command output into one or more files. This command works like the shape of the capital alphabet ‘T’ that takes input from one source and can store the output in the multiple locations. How this command can be used in Linux is shown in this tutorial by using multiple examples.

Syntax:

tee [OPTIONS] [FILE]

This command can use four types of options and store the result in single or multiple files. The options of this command are described below.

Options:

Name Description
-a or –append It is used to write the output at the end of the existing file.
-i or –ignore-interrupts It is used to ignore interrupt signals.
–help It is used to display all available options of this command.
–version It is used to display the current version of the command.

Files:

One or more file names can use to store the output of the command.

Example-1: Using simple `tee` command

`ls -la` command is used in Linux to display the details of the current directory list with permission information. `tee` command is used here to store the output of `ls -la` command into the file, output.txt. Run the following commands to check the function of simple `tee` command.

$ ls -la | tee output.txt
$ cat output.txt

Output:

Here, the first command displayed the output of `ls –la` into the terminal and wrote the output in the file, output.txt. The second command showed the content of output.txt file.

Example-2: Appending the output into an existing file

If the output of any command is written into an existing file by using `tee` command with ‘-a’ then the content of the file will not be overwritten. Here, the output of `pwd` command will be added at the end of the file, output.txt. Run the following commands from the terminal.

$ pwd | tee -a output.txt
$ cat output.txt

Output:

Here, the first command displays the output of `pwd` into the terminal and write the output at the end of output.txt file. The second command is used to check the output of the file. It is shown that the output.txt file contains both the output of the previous example and the current example.

Example-3: Writing the output into multiple files

`tee` command can be used to store the output of any command into more than one files. You have to write the file names with space to do this task. Run the following commands to store the output of `date` command into two files, output1.txt, and output2.txt.

$ date | tee output1.txt output2.txt
$ cat output1.txt output2.txt

Output:

Here, the first command displayed the current system date in the terminal and stored the value into two files, output1.txt and output2.txt. The second command showed the content of these two files which are identical.

Example-4: Ignoring interrupt signal

`tee` command with ‘-i’ option is used in this example to ignore any interrupt at the time of command execution. So, the command will execute properly even the user presses CTRL+C. Run the following commands from the terminal and check the output.

$ wc -l output.txt | tee -i output3.txt
$ cat output.txt
$ cat output3.txt

Output:

Here, the first command counted the total lines of output.txt file and stored the output into the file, output3.txt. The second command showed the content of output.txt file that contains 9 lines. The third command showed the content of output3.txt that is same as the first command output.

Example-5: Passing `tee` command output into another command

The output of the `tee` command can be passed to another command by using the pipe. In this example, the first command output is passed to `tee` command and the output of `tee` command is passed to another command. Run the following commands from the terminal.

$ ls | tee output4.txt | wc -lcw
$ ls
$ cat output4.txt

Output:

Here, the first command is used to write the output of `ls` command into the file, output4.txt and count the total number of lines, words, and characters of output4.txt. The second command is used to display the output of `ls` command and the third command is used to check the content of the output4.txt file.

Example- 6: `tee` command with the bash script

`tee` command can also be used to write the bash script output into a file. Create a bash file named add.sh with the following code that will take two input numbers from command line arguments and prints the sum of those numbers. `tee` command is used in this example will write the output of add.sh into the file result.txt.

add.sh

#!/bin/bash
a=$1
b=$2
((result=$a+$b))
echo "The addition of $a+$b=$result"

Run the following commands from the terminal to write the file and check the content of the file.

$ bash add.sh 50 90 | tee result.txt
$ cat result.txt

Output:

Here, 50 and 90 are passed as command line arguments into the script, add.sh and the output is written into the file results.txt. `cat` command is used to match the output with the content of result.txt.

Example-7: Hiding `tee` command output

If you want to write the output directly into the file without displaying in the terminal, then you have to use /dev/null with `tee` command. Run the following command do this task.

$ df | tee output5.txt > /dev/null
$ cat output5.txt

Output:

Here, the first command is used to write the output of `df` command into the file, output5.txt without showing in the terminal. The second command is used to check the output.

Conclusion:

The output of any command can be used for multiple purposes. The output of the command can be stored into multiple files by using `tee` command with different options. The most common uses of `tee` command are shown in this tutorial with the explanation. Hope the reader will be benefited after exercising the examples if this tutorial.

Related Posts
Leave a Reply

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