Fundamentals 7 min read

Master the Linux tee Command: Write, Append, and Redirect Output Efficiently

This guide explains the Linux tee utility, covering its purpose, installation verification, basic syntax, useful options, and seven practical examples that demonstrate writing to files, appending data, handling multiple outputs, ignoring interrupts, and combining tee with sudo for privileged writes.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master the Linux tee Command: Write, Append, and Redirect Output Efficiently

What Is the tee Command?

The tee command reads from standard input and writes the data simultaneously to standard output (your screen) and one or more files, making it handy for chaining commands while still preserving the original output.

Installation Check

tee

is part of GNU Coreutils and is pre‑installed on virtually all Linux distributions. Verify its presence with: which tee Typical output: /usr/bin/tee Check the version with:

tee --version

Basic Syntax

The general form is: tee [options]... [file]... Common options include: -a: Append to the file instead of overwriting. --version: Show the installed version. --help: List all available options.

Practical Examples

1. Write ping output to a file while still displaying it

ping yahoo.com | tee pingLinuxMi.txt

This captures the ping results in pingLinuxMi.txt and also prints them to the terminal.

2. Append data to an existing file

ping 192.168.174.170 | tee -a pingLinuxMi.txt

The -a flag ensures new output is added to pingLinuxMi.txt rather than replacing its contents.

3. Write to multiple files at once

[your_command] | tee file1 file2 file3 file4

All specified files receive the same output.

4. Pipe tee output to another command

ls ~ | tee linuxmi.txt | wc -l

This lists files in the home directory, saves the list to linuxmi.txt, and counts the number of entries.

5. Ignore interrupt signals while tee is running

[command] | tee -i output.txt

The -i option prevents Ctrl+C (SIGINT) from terminating the tee process.

6. Suppress tee output by redirecting to /dev/null

[command] | tee output_file_name >/dev/null

The command’s output is saved to output_file_name but not shown on the screen.

7. Use tee with sudo to write to privileged files

echo "300" | sudo tee /etc/linuxmi.conf

Running tee under sudo allows writing to files owned by the root user.

Conclusion

By mastering tee, you can efficiently capture, append, and redirect command output in a variety of scenarios, making your shell workflows more flexible and powerful.

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.

command-lineUnixTEEShell scriptingoutput 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.