The command “tee” it’s one of the basic commands that you should find in any system, yet it’s not so popular or use, this command reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.
In short if you want to redirect the STDOUT of any command as well as printing it to the screen, tee is the right tool to use, let’s see some practical use of this command.
The basic usage of tee it’s:
tee [ -a ] [ -i ] [ File ... ]
The 2 options of tee means:
-a Appends the output to the end of File instead of writing over it.
-i Ignores interrupts.
So a basic usage would be:
./program | tee logfile
The command above will execute the ‘program’. All the output will be redirected to ‘logfile’ while you can simultaneously view them on stdout also.
And now some more complex and maybe useful examples:
1) Use tee to process a pipe with two or more processes
echo “tee can split a pipe in two”|tee >(rev) >(tr ‘ ‘ ‘_’)
The output of this command is:
$ echo "tee can split a pipe in two"|tee >(rev) >(tr ' ' '_') tee can split a pipe in two $ owt ni epip a tilps nac eet tee_can_split_a_pipe_in_two
Tee can be used to split a pipe into multiple streams for one or more process to work it. You can add more ” >()” for even more fun.
2) Save a file as root with your normal user
Sometimes you start to edit a file without noticing that you can’t save it for permissions problems, tee can easily save you in this case, as example:
$ vim /etc/passwd [:w !sudo tee %]
In vim this means:
:w saves the file
!sudo calls the sudo command
tee redirects vim’s output
% the current file
After this command you’ll have saved successfully your file and can exit safely from the editor.
3) Duplicate several drives concurrently
dd if=/dev/sda | tee >(dd of=/dev/sdb) | dd of=/dev/sdc
If you have some drive imaging to do, you can boot into any liveCD and use a commodity machine. The drives will be written in parallel.
To improve efficiency, specify a larger block size in dd:
dd if=/dev/sda bs=64k | tee >(dd of=/dev/sdb bs=64k) | dd of=/dev/sdc bs=64k
To image more drives , insert them as additional arguments to tee:
dd if=/dev/sda | tee >(dd of=/dev/sdb) >(dd of=/dev/sdc) >(dd of=/dev/sdd) | dd of=/dev/sde
5) Run a command on a group of nodes in parallel via ssh
There are a lot of tools and utility to give the same command on multiple nodes, a really simple way is this one:
echo “uptime” | tee >(ssh host1) >(ssh host2) >(ssh host3)
6) Simple terminal redirection with script and tee
Using the script command you can
display the commands and their output to another user who is connected to another terminal, by example pts/3
script /dev/null | tee /dev/pts/3
- Linux game: Garry’s Mod
- Check how much do you type with WhatPulse on Linux
- Zorin OS Contest Results
- Linux Terminal: speedtest_cli checks your real bandwidth speed.
- Counting and listing hard links on Linux
Find me on Google+