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.
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
-aor --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.txt2. 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.txt3. 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.txt4. 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.txt5. 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.txt6. 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.txt7. 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.txtWith these examples you can reliably capture, store, and further process command output using tee in a variety of Linux workflows.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
