
{"id":30209,"date":"2026-04-14T13:47:33","date_gmt":"2026-04-14T11:47:33","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=30209"},"modified":"2026-04-24T13:53:30","modified_gmt":"2026-04-24T11:53:30","slug":"linux-tee-command","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/linux-tee-command\/","title":{"rendered":"Linux tee Command: Syntax, Options, Examples"},"content":{"rendered":"\n<p>The linux tee command solves a specific problem: you want to see command output in your terminal AND save it to a file at the same time. Without tee, you&#8217;re choosing one or the other.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-linux-tee-command\">What Is the Linux tee Command?<\/h2>\n\n\n\n<p>tee reads from standard input and writes to both standard output and one or more files simultaneously. The name comes from T-shaped pipe fittings in plumbing, which split flow in two directions. Same idea here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-how-tee-fits-into-the-unix-pipeline\">How tee Fits Into the Unix Pipeline<\/h3>\n\n\n\n<p>In a standard Unix pipeline, data flows left to right through pipes. tee inserts itself into that flow and forks the output: one copy continues down the pipeline, one copy goes to a file. Without it, you&#8217;d have to run the command twice or sacrifice terminal visibility.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tee-command-syntax\">tee Command Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>command | tee &#91;OPTIONS] &#91;FILE...]<\/code><\/pre>\n\n\n\n<p>The pipe character feeds the preceding command&#8217;s output into tee. tee then writes that output to the terminal and to each specified file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-use-the-tee-command\">How to Use the tee Command<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-write-output-to-a-file-and-terminal\">Write Output to a File and Terminal<\/h3>\n\n\n\n<p>Basic usage: run a command and write its output to a file while still seeing it on screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -la | tee directory_listing.txt<\/code><\/pre>\n\n\n\n<p>The terminal shows the ls output. directory_listing.txt gets the same content. Without tee, ls &gt; directory_listing.txt would redirect to the file but show nothing on screen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-append-to-a-file-with-tee-a\">Append to a File with tee -a<\/h3>\n\n\n\n<p>By default, tee overwrites the file each time. The -a flag appends instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"New entry\" | tee -a logfile.txt<\/code><\/pre>\n\n\n\n<p>This is the pattern for building up log files incrementally in scripts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-write-to-multiple-files-at-once\">Write to Multiple Files at Once<\/h3>\n\n\n\n<p>List multiple filenames and tee writes to all of them simultaneously:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uptime | tee file1.txt file2.txt file3.txt<\/code><\/pre>\n\n\n\n<p>All three files receive identical content. Useful for copying configuration snapshots to multiple locations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-sudo-with-tee-for-root-owned-files\">Using sudo with tee for Root-Owned Files<\/h3>\n\n\n\n<p>This is where tee earns its keep on production systems. You can&#8217;t use output redirection with sudo because the shell handles redirection before sudo runs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"net.ipv4.ip_forward=1\" | sudo tee \/etc\/sysctl.conf<\/code><\/pre>\n\n\n\n<p>The pipe feeds through tee, which runs as root via sudo and writes to the protected file. To append instead of overwrite, add -a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"new config line\" | sudo tee -a \/etc\/nginx\/nginx.conf<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-advanced-tee-usage-patterns\">Advanced tee Usage Patterns<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-suppressing-terminal-output-with-tee\">Suppressing Terminal Output with tee<\/h3>\n\n\n\n<p>If you want to save output to a file but not see it on screen, redirect tee&#8217;s stdout to \/dev\/null:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>command | tee output.txt > \/dev\/null<\/code><\/pre>\n\n\n\n<p>The file still gets written. The terminal sees nothing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-combining-tee-with-other-commands\">Combining tee with Other Commands<\/h3>\n\n\n\n<p>tee can sit in the middle of a pipeline, letting you inspect data at intermediate stages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat large_file.txt | tee snapshot.txt | grep \"ERROR\" | wc -l<\/code><\/pre>\n\n\n\n<p>snapshot.txt captures the full file. The terminal shows only the count of ERROR lines. Two results from one pass.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-tee-vs-output-redirection-when-to-use-each\">tee vs Output Redirection: When to Use Each<\/h2>\n\n\n\n<p>Output redirection (&gt;) is simpler when you only need to save to a file and don&#8217;t care about terminal output. tee is the right choice when you need both, when you need to write to multiple files simultaneously, or when you need sudo to write to protected paths. If your script runs unattended, redirection is usually sufficient. If someone is monitoring it live, tee keeps the output visible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faq-linux-tee-command\">FAQ: Linux tee Command<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777031372662\"><strong class=\"schema-faq-question\">What does the tee command do in Linux?<\/strong> <p class=\"schema-faq-answer\">It reads from standard input and writes to both the terminal and one or more files at the same time. Think of it as a pipe splitter.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777031385033\"><strong class=\"schema-faq-question\">How do I append output using tee?<\/strong> <p class=\"schema-faq-answer\">Add the -a flag: command | tee -a filename.txt. Without -a, tee overwrites the file each run.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777031396082\"><strong class=\"schema-faq-question\">How do I use sudo with tee?<\/strong> <p class=\"schema-faq-answer\">Pipe to sudo tee: echo &#8220;content&#8221; | sudo tee \/path\/to\/protected\/file. This works because tee, not the shell redirection, does the writing, and tee runs with elevated privileges.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777031406070\"><strong class=\"schema-faq-question\">What is the difference between tee and output redirection?<\/strong> <p class=\"schema-faq-answer\">Output redirection (> or >>) only writes to a file. You lose terminal visibility. tee writes to both. Use redirection for background scripts where terminal output doesn&#8217;t matter. Use tee when you need to see output and save it.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>The linux tee command solves a specific problem: you want to see command output in your terminal AND save it to a file at the same time. Without tee, you&#8217;re choosing one or the other. What Is the Linux tee Command? tee reads from standard input and writes to both standard output and one or [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[3402],"class_list":["post-30209","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Milan Ivanovic","author_link":"https:\/\/contabo.com\/blog\/author\/milan\/"},"uagb_comment_info":0,"uagb_excerpt":"The linux tee command solves a specific problem: you want to see command output in your terminal AND save it to a file at the same time. Without tee, you&#8217;re choosing one or the other. What Is the Linux tee Command? tee reads from standard input and writes to both standard output and one or&hellip;","authors":[{"term_id":3402,"user_id":0,"is_guest":1,"slug":"contabro","display_name":"ContaBro","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=30209"}],"version-history":[{"count":1,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30209\/revisions"}],"predecessor-version":[{"id":30210,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30209\/revisions\/30210"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=30209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=30209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=30209"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=30209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}