Operations 8 min read

Mastering the tee Command: Capture and Redirect Linux Output Efficiently

This guide explains how to use the Linux tee command to capture command output, append to files, write to multiple destinations, ignore interrupts, pipe results to other commands, and integrate tee into shell scripts, providing practical examples and syntax details.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the tee Command: Capture and Redirect Linux Output Efficiently

What is the tee command?

The tee utility reads from standard input and writes the data to both standard output and one or more files, allowing you to preserve command results while still seeing them on the terminal.

Syntax

$ tee [OPTIONS] [FILE]...

Key options

-a

or --append: Append output to the end of existing files instead of overwriting. -i or --ignore-interrupts: Ignore interrupt signals (e.g., Ctrl+C). --help: Show help information. --version: Display the command version.

1. Basic usage – saving command output

To store the output of ls -la in output.txt while still displaying it:

$ ls -la | tee output.txt
$ cat output.txt

2. Appending to an existing file

Using the -a option adds new output to the end of the file without overwriting previous content:

$ pwd | tee -a output.txt
$ cat output.txt

3. Writing to multiple files

The command can duplicate output to several files by listing them separated by spaces:

$ date | tee output1.txt output2.txt
$ cat output1.txt output2.txt

4. Ignoring interrupt signals

When you want the command to continue even after pressing Ctrl + C , add the -i option:

$ wc -l output.txt | tee -i output3.txt
$ cat output.txt
$ cat output3.txt

5. Piping tee output to another command

You can chain tee with other utilities. The example below counts lines, words, and characters of the ls output while also saving it:

$ ls | tee output4.txt | wc -lwc
$ ls
$ cat output4.txt

6. Using tee inside a Bash script

Consider a script add.sh that adds two numbers and prints the result. You can capture its output with tee:

#!/bin/bash
a=$1
b=$2
((result=a+b))
echo "The addition of $a+$b=$result"
$ bash add.sh 50 90 | tee result.txt
$ cat result.txt

7. Hiding tee output from the terminal

Redirect tee’s standard output to /dev/null if you only want the file to receive the data:

$ df | tee output5.txt > /dev/null
$ cat output5.txt

With these examples you can reliably capture, store, and further process command output using tee in a variety of Linux workflows.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TEEShell scriptingcommand-lineoutput redirection
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.