{"id":105,"date":"2023-12-27T09:37:07","date_gmt":"2023-12-27T09:37:07","guid":{"rendered":"https:\/\/resellerjungle.com\/articles\/?p=105"},"modified":"2023-12-27T12:33:10","modified_gmt":"2023-12-27T12:33:10","slug":"8-useful-tee-command-in-linux","status":"publish","type":"post","link":"https:\/\/resellerjungle.com\/articles\/8-useful-tee-command-in-linux\/","title":{"rendered":"8 Useful \u2018tee\u2019 Command in Linux"},"content":{"rendered":"\n<p><br>The <code>tee<\/code> command is a command-line utility available in Unix-like operating systems. Its primary function is to read from standard input and write to both standard output and files simultaneously. It allows you to capture and display the output of a command in the terminal while also saving it to one or more files.<\/p>\n\n\n\n<p>Almost all power users prefer to use the command line interface while interacting with Linux systems. By default,\u00a0all Linux commands\u00a0display their output on the standard output stream. However, sometimes we need to store this output in the files for debugging purposes.<\/p>\n\n\n\n<p>Certainly, we can use the redirection operator to achieve this. However, one of the minor limitations of this method is that it stores the output in the file only.<\/p>\n\n\n\n<p>To overcome this limitation, we can the\u00a0<strong><a href=\"https:\/\/resellerjungle.com\/articles\/create-file-using-echo-touch-tee-cat\/\" data-type=\"link\" data-id=\"https:\/\/resellerjungle.com\/articles\/create-file-using-echo-touch-tee-cat\/\">tee command<\/a><\/strong>\u00a0that reads from the standard input stream and write to the standard output stream and files.<\/p>\n\n\n\n<p>In this practical guide, we will see various examples of the tee command. By the end of this guide, Linux users will be able to use the tee command to fulfill their programming needs.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/Linux-Tee-Command-1024x576.png\" alt=\"\" class=\"wp-image-109\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/Linux-Tee-Command-1024x576.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/Linux-Tee-Command-300x169.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/Linux-Tee-Command-768x432.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/Linux-Tee-Command-1536x864.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/Linux-Tee-Command-2048x1152.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#tee-command-syntax\">tee Command Syntax<\/a><\/li><li><a href=\"#1-save-output-to-a-file-in-linux\">1. Save Output to a File in Linux<\/a><\/li><li><a href=\"#2-append-output-to-file-in-linux\">2. Append Output to File in Linux<\/a><\/li><li><a href=\"#3-write-output-to-multiple-files-in-linux\">3. Write Output to Multiple Files in Linux<\/a><\/li><li><a href=\"#4-send-output-of-one-command-to-another\">4. Send Output of One Command to Another<\/a><\/li><li><a href=\"#5-hide-output-of-file-in-linux\">5. Hide Output of File in Linux<\/a><\/li><li><a href=\"#6-write-output-to-privileged-file\">6. Write Output to Privileged File<\/a><\/li><li><a href=\"#7-edit-privileged-file-in-linux\">7. Edit Privileged File in Linux<\/a><\/li><li><a href=\"#8-ignore-interrupts-signal-sigint\">8. Ignore Interrupts Signal (SIGINT)<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"tee-command-syntax\">tee Command Syntax<\/h3>\n\n\n\n<p>The syntax of the&nbsp;<strong>tee<\/strong>&nbsp;command is similar to other Linux commands. At a high level, it is divided into two groups \u2013&nbsp;<code>OPTIONS<\/code>&nbsp;and&nbsp;<code>FILES<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tee &#91;OPTIONS] &#91;FILE1] &#91;FILE2] &#91;FILE3] ...<\/code><\/pre>\n\n\n\n<p>In the above syntax, both&nbsp;<code>OPTIONS<\/code>&nbsp;and&nbsp;<code>FILES<\/code>&nbsp;are optional parameters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-save-output-to-a-file-in-linux\">1. Save Output to a File in Linux<\/h3>\n\n\n\n<p>As discussed previously, the\u00a0<strong>tee<\/strong>\u00a0command sends output to the standard output as well as the file. To understand this, first, let\u2019s use the\u00a0<a href=\"https:\/\/resellerjungle.com\/articles\/create-file-using-echo-touch-tee-cat\/\" target=\"_blank\" rel=\"noreferrer noopener\">echo command<\/a>\u00a0to display the text on the standard output stream:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com\"<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"42\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-2-1024x42.png\" alt=\"\" class=\"wp-image-115\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-2-1024x42.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-2-300x12.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-2-768x31.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-2.png 1417w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, let\u2019s use the\u00a0<strong>tee command<\/strong>\u00a0to write the output to the\u00a0<strong>output.txt<\/strong>\u00a0file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com\" | tee output.txt<\/code><\/pre>\n\n\n\n<p>Finally, view the contents of the\u00a0<strong>output.txt<\/strong>\u00a0file using the\u00a0cat command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat output.txt<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-append-output-to-file-in-linux\">2. Append Output to File in Linux<\/h3>\n\n\n\n<p>By default, the&nbsp;<strong>tee command<\/strong>&nbsp;overwrites the output files. However, we can avoid this by enabling the append mode, which appends the output at the end of the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com\" | tee -a output.txt<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cat output.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"68\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-2-1024x68.png\" alt=\"\" class=\"wp-image-117\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-2-1024x68.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-2-300x20.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-2-768x51.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-2.png 1411w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this example, we have used the\u00a0<code>-a<\/code>\u00a0option to enable the append mode.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-write-output-to-multiple-files-in-linux\">3. Write Output to Multiple Files in Linux<\/h3>\n\n\n\n<p>Similarly, we can use the&nbsp;<strong>tee<\/strong>&nbsp;command to write output to multiple files as shown.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com\" | tee file-1.txt file-2.txt file-3.txt<\/code><\/pre>\n\n\n\n<p>n this example, we have provided multiple files as command-line arguments. Now, let\u2019s use the&nbsp;head command&nbsp;to display the contents of the files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>head -v file-1.txt file-2.txt file-3.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"212\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-2-1024x212.png\" alt=\"\" class=\"wp-image-119\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-2-1024x212.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-2-300x62.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-2-768x159.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-2.png 1480w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-send-output-of-one-command-to-another\">4. Send Output of One Command to Another<\/h3>\n\n\n\n<p>Additionally, we can also use the&nbsp;<strong>tee<\/strong>&nbsp;command with the pipe operator&nbsp;<code>(|)<\/code>. This method comes in handy when we want to store the output as well as forward it to another command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com\" | tee example.txt | wc -c<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cat example.txt<\/code><\/pre>\n\n\n\n<p>In this example, first, we are using the&nbsp;<strong>tee<\/strong>&nbsp;command to write the output to the file. Next, we are using the&nbsp;wc command&nbsp;to count the number of characters.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"102\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-1-1024x102.png\" alt=\"\" class=\"wp-image-121\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-1-1024x102.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-1-300x30.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-1-768x76.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-1.png 1471w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-hide-output-of-file-in-linux\">5. Hide Output of File in Linux<\/h3>\n\n\n\n<p>In the previous examples, we used the&nbsp;<strong>tee<\/strong>&nbsp;command to send the output to the standard output. However, in some rare scenarios, we want to suppress the output. In such cases, we can redirect the output to the&nbsp;<strong>\/dev\/null<\/strong>&nbsp;device:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com\" | tee output.txt &gt; \/dev\/null<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cat output.txt<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-write-output-to-privileged-file\">6. Write Output to Privileged File<\/h3>\n\n\n\n<p>Sometimes, we need to write the output to the privileged file. However, we cannot achieve this using just the&nbsp;<strong>tee<\/strong>&nbsp;command. In such scenarios, we can use the combination of the&nbsp;<strong>sudo<\/strong>&nbsp;and&nbsp;<strong>tee<\/strong>&nbsp;commands.<\/p>\n\n\n\n<p>First, let\u2019s change the ownership of the file using the&nbsp;chown command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chown root:root output.txt<\/code><\/pre>\n\n\n\n<p>Next, let\u2019s append some text to the&nbsp;<strong>output.txt<\/strong>&nbsp;file using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com\" | sudo tee -a output.txt<\/code><\/pre>\n\n\n\n<p>Finally, let\u2019s verify that the file has been updated successfully.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-edit-privileged-file-in-linux\">7. Edit Privileged File in Linux<\/h3>\n\n\n\n<p>In a similar way, we can use the combination of the&nbsp;<strong>sudo<\/strong>&nbsp;and&nbsp;<strong>tee<\/strong>&nbsp;commands to edit the privileged file. To understand this, first, let\u2019s open a privileged file in a&nbsp;Vi editor&nbsp;and add some text to it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi output.txt<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-ignore-interrupts-signal-sigint\">8. Ignore Interrupts Signal (SIGINT)<\/h3>\n\n\n\n<p>In Linux, we use the&nbsp;<code>Ctrl+c<\/code>&nbsp;key combination to send the&nbsp;<strong>SIGINT<\/strong>&nbsp;signal. The default behavior of this signal is to terminate the process. However, we can use the&nbsp;<code>-i<\/code>&nbsp;option to ignore the interrupt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com\" | tee -i example.txt<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cat example.txt<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we learned about the&nbsp;<strong>tee<\/strong>&nbsp;command using some practical examples. Users can refer to this guide while working with Linux systems from the command line interface.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We hope you&#8217;ve found this useful!!<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tee command is a command-line utility available in Unix-like operating systems. Its primary function is to read from standard&hellip;<\/p>\n","protected":false},"author":4,"featured_media":111,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-105","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/comments?post=105"}],"version-history":[{"count":9,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/105\/revisions\/123"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/media\/111"}],"wp:attachment":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/media?parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/categories?post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/tags?post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}