Today’s COTD is the “tee” command.
Have you ever wanted to redirect the output of a script or program to a file, but be able to view it right away? Without putting the file in the background, and then tailing the file?
The tee command allows you to do just that.
Here is how we could use it:
$ nmap 192.168.123.0/24 | tee nmap.out
Starting Nmap 4.62 ( http://nmap.org ) at 2009-03-03 21:51 CST
Interesting ports on firewall.domain.com (192.168.123.1):
Not shown: 1687 closed ports
PORT STATE SERVICE
1/tcp open tcpmux
11/tcp open systat
15/tcp open netstat
22/tcp open ssh
23/tcp open telnet
79/tcp open finger
.
.
.
Nmap done: 256 IP addresses (14 hosts up) scanned in 147.576 seconds$ ls -l
total 6231
-rw-r–r– 1 root root 5756 2009-03-03 21:55 nmap.out
Here we tell the shell to run the nmap command, send the output to the tee command, which then displays it and writes it to a file. Tee also supports the “-a” option which will tell it to append to the filename specified if it already exists, otherwise, it will overwrite the file each time.
It’s important to note that tee only works with “stdin” and “stdout”. It does not process “stderr”. Stderr will still go to your terminal, but will not be captured in the file that tee is writing to.
![[del.icio.us]](http://www.theunixzone.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.theunixzone.com/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://www.theunixzone.com/wp-content/plugins/bookmarkify/facebook.png)
![[Twitter]](http://www.theunixzone.com/wp-content/plugins/bookmarkify/twitter.png)