How to Use the tee Command to Split Terminal Output on LinuxReading Time: 3 minutes
Using the tee command, you can split the output of a command between the terminal and files on Linux.
Linux lets you store the command output in files as a means of output redirection. When you save the output to a file using the > or >> operators, the output gets redirected with no information displayed on the terminal.
But what if you want to print the output on the screen and store it in a file simultaneously? Although you can’t do it with the output redirection operators, it is possible to do so using the tee command on Linux.
Basic Command Syntax
tee is a standard Linux utility used to split the output of a command between the standard output and files.
Unlike most Linux commands, tee is used with other programs using a pipe (|). The pipe operator—one of the many command-line operators on Linux—lets a program use another command’s output as its input. You might have used pipe while using grep alongside the ls command or cat.
The basic syntax of the tee command is:
command | tee options filepath
…where options and filepath are the command-line arguments and the path to the file you want to store the output in.
Split Terminal Output on Linux With tee
The simplest use of tee is to split the output to display it in the terminal as well as store it inside a file. For instance, to store the output of the ls command to a text file named ‘output.txt,’ run:
ls | tee ./output.txt
The output will display the contents of the present working directory as usual. On checking the output.txt file, you’ll find that tee saved the output to the file as well.
When you specify a file path, tee checks if the file exists. If it doesn’t find one, it automatically creates the file for you, making it convenient for use in shell scripts. It is useful when you want to log the output of a program or script for later reference.
If the specified file has data stored inside that you don’t want to overwrite, use the -a flag to append the output to the file, instead of clearing the entire file and then saving the data:
ls | tee -a ./output.txt
Similarly, you can redirect the output to multiple files by specifying the paths, separated by single spaces:
ls | tee ./output1.txt ./output2.txt
Sometimes, unexpected errors or manual interruptions (using Ctrl + C or Ctrl + Z) in the former command can cause tee to quit. To ignore such interruptions, use the -i flag:
ls | tee -i output.txt
To get command-line help regarding tee or find the version details, use the –help and –version flags as follows:
Redirecting the Output to Another Command
You can create an output chain by piping tee with other Linux commands. Doing so will save the output to the specified file and then pass it on to the next command for processing.
Use the following format to redirect tee’s output to another command:
command | tee filepath | othercommand
For example, the following command will save the ls command output to output.txt before finally redirecting it to grep for a quick search:
ls | tee output.txt | grep 'Documents'
Overall, you can use the tee command to play around with the standard input and output on Linux.
Manipulating Output and Text With Linux Commands
The tee command adds much-needed functionality to the standard output redirection operators on Linux. You can even use it to manipulate the output of a command before passing it on to another program.
Similarly, you can modify text using the terminal with the help of some standard Linux commands. They might take some time to get used to, but in the end, text manipulation via the command line is worth learning for any system administrator.
MediaDownloader.net -> Free Online Video Downloader, Download Any Video From YouTube, VK, Vimeo, Twitter, Twitch, Tumblr, Tiktok, Telegram, TED, Streamable, Soundcloud, Snapchat, Share, Rumble, Reddit, PuhuTV, Pinterest, Periscope, Ok.ru, MxTakatak, Mixcloud, Mashable, LinkedIn, Likee, Kwai, Izlesene, Instagram, Imgur, IMDB, Ifunny, Gaana, Flickr, Febspot, Facebook, ESPN, Douyin, Dailymotion, Buzzfeed, BluTV, Blogger, Bitchute, Bilibili, Bandcamp, Akıllı, 9GAG