The β€œtee” Command in Linux [4 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The tee command in Linux is used to read standard input and write to another file apart from standard output. This command is very useful to store the intermediate output of multiple commands. In this article, you will find the frequent uses of the tee command in Linux with practical examples.

A. Description

The tee command in Linux writes standard input to standard output as well as to specified file/s through piping.

B. Syntax

The syntax of the tee command in Linux simply contains single or multiple options and then the desired Filenames.

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

Note: In the above syntax OPTION & Filenames enclosed by square brackets and followed by 3 dots represents that multiple OPTIONs & Filenames can be utilized simultaneously

C. Options

The tee command in Linux offers multiple options. You will find the most useful one’s here for your convenience. For any further features, you can always look at the man page.

man tee

Useful Options

  • -a: Appends output to the given files.
  • -i: Ignores Interruptions.
  • -p: Diagnoses errors in writing to non-pipe output.
  • –help: Displays help page.
  • –version: Prints version information.

Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them

Practical Examples of the β€œtee” Command in Linux

The tee command in Linux is a very useful command to store the output of commands between defined tasks. In this section, I will explain the most helpful applications of the tee command with practical examples.

Example 1: Writing Command Output to Multiple Files Using the β€œtee” Command in Linux

You can display a command output in the terminal and also write this output to multiple files using tee command in Linux. You will need to redirect the command using pipe(|) to the tee command and then mention desired filenames. The command will create the files if they do not exist in the system. In this example, I will write the output of the ls command into the files β€œlist1.txt”, β€œlist2.txt”. Follow the instructions given below to do the same.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

βž‹ After that, type the following command in the command prompt:

ls |tee list1.txt list2.txt

➌ Now, press the ENTER button.

Output >

In this first image, you can see that I have run the ls command and displayed a list of files and directories under the current path.Writing Command Output to Multiple Files Using the tee Command in Linux.In this second image, you can see that the same list shown in the terminal is stored in the files β€œlist1.txt”, β€œlist2.txt”. You can use the cat command to view the file contents.Viewing file contents using cat command in linux.


Similar Readings


Example 2: Appending Command Output to Existing File Using the β€œtee” Command in Linux

You can append a command output in an existing file as well as show it in the terminal using tee command in Linux. However, you will need to use the option -a along with the tee command. In this example, I will write the output of the ls -l command into the existing file β€œlist2.txt”. You can do the same by following the steps below.

Steps to Follow >

➊ At first, go to the Ubuntu Terminal.

βž‹ Then, type the following command in the command prompt:

ls |tee -a list2.txt

➌ Now, press the ENTER button.

Output >

In this first image, you can see that I have run the ls -l command and displayed a long list of files and directories under the current path.Appending Command Output to Existing File Using the tee Command in Linux.

In the second image, you can see that I have appended the long list after the existing contents of the β€œlist2.txt” file. You can use the cat command to view the file contents in the terminal.Viewing file contents using cat command in linux.

Example 3: Ignoring Interrupts Using the β€œtee” Command in Linux

You can ignore interruptions while running a command using the tee command with the option -i. In this example, I will interrupt the output of the ping command while checking the server https://linuxsimply.com/. To do the same follow the given steps.

Steps to Follow >

➊ Open the Ubuntu Terminal.

βž‹ Type the following command in the command prompt:

ping linuxsimply.com | tee -i ping.txt

➌ Hit ENTER.

Output >

In the image below, you can see that I have interrupted the output of the ping command using CTRL+C. The command in this case is not interrupted and completes checking the https://linuxsimply.com/ server.Ignoring Interrupts Using the tee Command in Linux.


Similar Readings


Example 4: Redirecting the β€œtee” Command Output to Another Command

You can use the output of the tee command as the input of other commands using piping(|). In this case, the file generated with the tee command in Linux is sent to the next command. In this example, at first, I will store the list of the files and directories under the current path to the β€œlist1.txt” file. Then, I will use the grep command on the β€œlist1.txt” file to find the file names with the .txt extension. You can do the same by following the process below.

Steps to Follow >

➊ At first, launch the Ubuntu Terminal.

βž‹ Type the following command in the command prompt:

ls | tee list1.txt| grep ".txt"

➌ Now, hit the ENTER button.

Output >

In the following image, you can see that I have applied the grep command on the output file of the tee command and extracted the desired .txt files.Sending the tee Command Output to Another Command.

Conclusion

This article presents the basic uses of the tee command with hands-on examples. Using this command you will be able to store the output of complicated tasks in the middle of running programs. Furthermore, I hope these practical examples will help you in the journey with the command line and aid your experience in Linux.


Similar Readings

Rate this post
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment