tee
is a Linux utility that reads from standard input and writes to standard output and files.
In this article, we will look into some of the basic use of tee and also try to understand how it works so next time we encounter it we will be able to know what it’s doing there.
:w !sudo tee %
I often forget to sudo before editing a file I don’t have write permissions on. This has been a life saver in cases like this,just try this vim command in order to save the file.
echo "tee can split a pipe in two" |tee >(rev) >(tr ' ' '_')
Tee can be used to split a pipe into multiple streams for one or more processes to work it. You can add more ” >()” for even more fun.
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
:w !sudo tee > /dev/null %
Write a file you edited in Vim but that you do not have the permissions to write to (unless you use sudo.) Same as #1204 but without the echo to stdout that I find annoying.
echo "uptime" | tee >(ssh host1) >(ssh host2) >(ssh host3)
tee >(cat – >&2)
the tee command does fine with file names, but not so much with file descriptors, such as &2 (stderr). This uses process redirection to tee to the specified descriptor.
In the sample output, it’s being used to tee to stderr, which is connected with the terminal, and to wc -l, which is also outputting to the terminal. The result is the output of bash –version followed by the line count
echo “foo bar” | sudo tee -a /path/to/some/file
This is the solution to the common mistake made by sudo newbies, since
sudo echo "foo bar" >> /path/to/some/file
does NOT add to the file as root.
Alternatively,
sudo echo "foo bar" > /path/to/some/file
should be replaced by
echo "foo bar" | sudo tee /path/to/some/file
And you can add a >/dev/null
in the end, if you’re not interested in the tee stdout :
echo "foo bar" | sudo tee -a /path/to/some/file >/dev/null
echo “Hello World.” | tee -a hello.txt
When plumbers use pipes, they sometimes need a T-joint. The Unix equivalent to this is ‘tee’. The -a flag tells ‘tee’ to append to the file, rather than clobbering it.
Tested on bash and tcsh.
bash -x test.sh 2>&1 | tee out.test
Sends both stdout and stderr to the pipe which captures the data in the file ‘out.test’ and sends to stdout of tee (likely /dev/tty unless redirected). Works on Bourne, Korn, and Bash shells.
cat /path/to/some/file.txt | tee /dev/pts/0 | wc -l
This is a cool trick to view the contents of the file on /dev/pts/0 (or whatever terminal you’re using), and also send the contents of that file to another program by way of an unnamed pipe. All the while, you’ve not bothered saving any extra data to disk, like you might be tempted to do with sed or grep to filter output.
echo “text” | sudo tee -a /path/file.conf > /dev/null
You can add repositories, options, etc to any .conf in your system!
sudo echo 0 | tee /proc/sys/kernel/randomise_va_space
Will shut-down ASLR until next reboot.
Ummmmmm… Bye. See ya in next one. :)
This article is written by Saket Upadhyay and Peer Reviewed by Editing and Documentation Team @ FrigidSec . To check profile of every writer of FrigidSec-Blog please visit HERE