While using a Linux terminal, there exist many situations where you want to redirect the output of a command to a file and screen simultaneously, ensuring that the output is written to a file so you can refer to it later. You will be pleased to know that a Linux tool can perform this function effortlessly. “tee command” is added in the Linux-based systems specifically for this purpose. So, let’s go ahead and learn about this fantastic tool.
tee command can read the standard input and redirect it to one or more files. It breaks the output of any command or program shown on the screen and saves it to a file. It is utilized with other commands using pipes.
tee command basic syntax:
Here we have shown you the basic syntax of the tee command.
- -i or –ignore-interrupts is for ignoring interrupt signals.
- Finally, -a or –append allows tee command to append files rather than overwriting the content.
- You can also use tee -help to view the list of available options for the tee command.
- File names of one or more files. Each of these files will contain the redirected output.
Using tee command for redirecting output to file and screen:
This section contains some excellent examples for demonstrating the output redirection of any command to a screen or any file.
Syntax of tee command for redirecting output:
Redirecting output to a single file and screen:
In the terminal, executing the “ls” command will list the files and directories present in your current working directory.
For redirecting the output to a file and screen simultaneously, write out the tee command in the following way:
Before the “|” pipe symbol, you can type the command you want to execute and then combine the “|” with the tee command while specifying the file path. In our case, we will redirect the output to “samplefile” present in our home directory.
You can also append the redirected output by utilizing the “-a” or “–append” option with the tee command. -a or –append option allows tee command to append files rather than overwriting the file’s content.
Syntax for appending redirected output:
This command will append the redirected output to the end of the “samplefile.”
Redirecting output to Multiple files and screen:
If you want to redirect the screen output to multiple files, the only thing you have to do is add the file names at the end of the tee command. We have provided you the syntax for this multiple file redirection.
The below-given command will redirect the output of “ls” to screen and “samplefile.txt” and “samplefile2.txt” files.
While working in the Linux terminal, the concept of output redirection is precious. “tee” command is one of the most valuable tools that helps a Linux user redirect the output of a command to a file and screen. This article discussed the primary usage of “tee” for redirecting output to screen, single, or multiple files.