loader

Learn about the Linux TEE command

 

 


TEE primarily reads from the standard input and then writes to output and a file simultaneously. 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 the tasks simultaneously, copies the result to the specified variable or file, and also 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)  – It means, 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. By using it, you can 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 output 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





That’s it!

 

 

 





 

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

 

 

 

 

 




 

Get the most out of learning with VPSie.com