Search
Close this search box.

Learn about the Linux Tee Command

Table of Contents

Learn about the Linux TEE command

Linux Tee command is a popular utility that allows users to redirect the output of a command to multiple destinations simultaneously. This article will delve into the features, advantages, disadvantages, and use cases of the Linux Tee command.

 

TEE primarily reads from the standard input and then writes simultaneously to the output and a file. Plumbing T-splitters inspired the name of the command. The TEE is used in conjunction with other commands via piping.

 

This method allows you to display and save the output of a program at the same time. It does both tasks simultaneously, copies the result to the specified variable or file, and displays the result.

 

In this tutorial we will walk you through the Linux TEE Command,

 

 

1. First, Let’s start by looking at the syntax,

 

tee [OPTIONS] [FILE]

 

Options:

 

 

  • -a (-append)  – Instead of overwriting the files, append to the given files.

 

 

  • -i (-ignore-interrupts) – Ignoring interrupt signals.

 

 

  • -help –  By doing so, you can see all your options. For example,
tee --help

 

 

OUTPUT

 

 

Usage: tee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.
  -a, --append              append to the given FILEs, do not overwrite
  -i, --ignore-interrupts   ignore interrupt signals
  -p                        diagnose errors writing to non pipes
      --output-error[=MODE]   set behavior on write error.  See MODE below
      --help     display this help and exit
      --version  output version information and exit
MODE determines behavior with write errors on the outputs:
  'warn'         diagnose errors writing to any output
  'warn-nopipe'  diagnose errors writing to any output not a pipe
  'exit'         exit on error writing to any output
  'exit-nopipe'  exit on error writing to any output not a pipe
The default MODE for the -p option is 'warn-nopipe'.
The default operation when --output-error is not specified, is to
exit immediately on error writing to a pipe, and diagnose errors
writing to non pipe outputs.
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/tee>
or available locally via: info '(coreutils) tee invocation'
  • FILE_NAMES – A file or files to which the output data will be written.
  • –VERSION –  It gives the version information; for example,
tee --version

 

 

OUTPUT

 

tee (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Parker, Richard M. Stallman, and David MacKenzie.

 

 

2. tee Command with other commands

 

As you can see, use a  df command. Using it lets you discover how much available disk space there is on the file system. Furthermore, the output is piped to the TEE command. Upon running the script, the terminal will display the production, and the same information will be written to the file disk.txt.

 

 df -h | tee disk.txt

 

 

OUTPUT

 

 Filesystem              Size  Used Avail Use% Mounted on
udev                    444M     0  444M   0% /dev
tmpfs                    92M  9.7M   83M  11% /run
/dev/mapper/vpsie-root   39G  2.5G   35G   7% /
tmpfs                   460M     0  460M   0% /dev/shm
tmpfs                   5.0M     0  5.0M   0% /run/lock
tmpfs                   460M     0  460M   0% /sys/fs/cgroup
/dev/vda1               453M   84M  342M  20% /boot
tmpfs                   135M     0  135M   0% /run/user/0
 

3. Writing to Multiple Files

 

Multiple files will also be written with the TEE command. So, you should specify a list of files separated by space as arguments.

 

For example :

 

command | tee filename01.out filename02.out filename03.out filename04.out

 

An example of using these functions appeding(-append), hiding, and ignoring(–ignore-interrupt) will be given here for better understanding.

 

1. appeding(-append)

 

 command | tee -a filename.out

 

2. Hiding the output

Here we redirect the output it to /dev/null:

 

command | tee filename.out >/dev/null

 

3. Ignoring the Interrupt

 

command | tee -i filename.out

 

Features of Linux Tee Command

 

Simultaneous Output: One of the primary features of the Tee command is its ability to split the output of order and redirect it to multiple destinations simultaneously. This means you can save the work to a file and display it simultaneously on the screen.

 

Easy to Use: The Tee command is easy to use and can be executed from the command line interface (CLI) with a single command.

 

Customizable: The Tee command is customizable and allows users to specify the location and name of the output file.

 

Advantages of Linux Tee Command

Multitasking: The Tee command is an excellent tool for multitasking, as it allows you to monitor an order’s output while saving it to a file for later use.

 

Improved Efficiency: With the Tee command, users can avoid running an order twice to display the output on the screen and save it to a file. This can save a significant amount of time and improve efficiency.

 

Debugging: The Tee command is helpful for debugging purposes, as it allows users to capture and analyze the order output in real time.

 

Disadvantages of Linux Tee Command

Storage Overhead: If the output of a command is extensive, it can result in a significant amount of storage overhead when saving to a file.

 

Overwriting: If the Tee command is used to save the output to a file, it will overwrite any existing file with the identical name without warning. This can result in losing important data if precautions are not taken.

 

Limited Functionality: The Tee command is tiny and may need to be more suitable for advanced use cases.

 

In summary, the Linux Tee command is a powerful and multipurpose tool that allows users to redirect the order output to multiple destinations simultaneously. Its ease of use, customizability, and multitasking capabilities make it an essential tool for Linux users. However, its limitations, such as the potential for storage overhead and overwriting files, should also be considered. Overall, the Tee command is valuable to any Linux user’s toolbox.

 

 

We hope that this article has provided you with more information.

 

Secure Your Linux Server

 

The Linux Tee command is a command-line utility that reads standard input and writes it to both standard output and a file.

The syntax of the Tee command is as follows: tee [OPTION]… [FILE]…

Some common options used with the Tee command include:

  • -a : Appends the output to the end of the file instead of overwriting it
  • -i : Ignores interrupt signals
  • -p : Writes to the pipe before writing to the output files
  • -u : Disables output buffering

To use the Tee command, you can pipe the output of a command to tee and specify the name of the file you want to write to. For example, ls | tee file.txt will list the files in the current directory and write the output to both the screen and a file called file.txt.

Some advantages of using the Tee command include the following:

  • It allows you to capture the output of a command and save it to a file for later use.
  • It provides a way to send output to multiple destinations at once.
  • It is a simple and efficient way to save the output to a file without needing to redirect the output to a file using the ‘>’ or ‘>>’ operators.
Make a Comment
Share on
Facebook
Twitter
LinkedIn
Print
VPSie Cloud service

Fast and Secure Cloud VPS Service

Try FREE
For a month

The First 1 orders gets free discount today! Try Sign up on VPSie to get a chance to get the discount.