Operations 8 min read

Master the Linux tee Command: Real‑World Examples and Tips

This guide explains what the Linux tee command does, how it’s installed, its basic syntax and options, and provides practical examples—including writing output to files, appending data, handling multiple files, piping to other commands, ignoring interrupts, hiding output, and using sudo—so readers can efficiently capture and process command‑line output.

Open Source Linux
Open Source Linux
Open Source Linux
Master the Linux tee Command: Real‑World Examples and Tips

When working in a Linux terminal you may need to write data to a file while still processing its output; the tee utility makes this possible.

What is the tee command?

The tee command reads from standard input and writes to both standard output (your screen) and one or more files, similar to a T‑shaped pipe used by plumbers.

Installation

tee

is included in GNU Coreutils, so it is pre‑installed on most Linux distributions. Verify its presence with: which tee Check the version with:

tee --version

Basic syntax

The general form is: tee [options]... [file]... Common options: -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 displaying it

ping yahoo.com | tee pingLinuxMi.txt

This captures the ping results in pingLinuxMi.txt while still showing them on the terminal.

2. Append data to a file

ping 192.168.174.170 | tee -a pingLinuxMi.txt

3. Write to multiple files simultaneously

[your command] | tee file1 file2 file3

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

[command] | tee -i output_file

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

6. Hide tee output

[command] | tee output_file > /dev/null

This writes the output to output_file while discarding it from the screen.

7. Use tee with sudo for privileged files

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

Redirects data to a file owned by the root user without permission errors.

Conclusion

With these examples you should now understand how to use tee for everyday tasks, from simple logging to complex pipelines.

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.

LinuxShell scriptingoutput redirectiontee command
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.