FUNDAMENTALS A Complete Guide for Beginners
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]...
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.
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.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.
Similar Readings
- The βmanβ Command in Linux [6 Practical Examples]
- The “history” Command in Linux [6 Practical Examples]
- The βcalβ Command in Linux [7 Practical Examples]
- The βbcβ Command in Linux [10+ Practical Examples]
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.
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.
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.
Similar Readings
- The βcronβ Command in Linux [3 Practical Examples]
- The βcrontabβ Command in Linux [10 Practical Examples]
- The βdateβ Command in Linux [8+ Practical Examples]
- The βncalβ Command in Linux [8 Practical Examples]
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.
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
- The βneofetchβ Command in Linux [12+ Practical Examples]
- The βtreeβ Command in Linux [14 Practical Examples]
- The βwhatisβ Command in Linux [12 Practical Examples]
- The βwhereisβ Command in Linux [10 Practical Examples]
- The βwhichβ Command in Linux [3 Practical Examples]
- The βatβ Command in Linux [7 Practical Examples]