Example - How To Use Tee Command In Linux

Posted on : August 22, 2011 , Last Updated on : October 14, 2016 By
| Reply More

In Unix and Linux world, a large number of small and single purpose programs are available. These programs can be chained together to perform complex tasks. Command chaining and redirection are the features that are used for these complex tasks. You can redirect the output and/or errors to a file and it will not be shown on the terminal. Similarly, using chaining, output of one command can be given as input to second command and further to third command and so on. But once you redirect an output to a file, you cannot chain it with other command. Tee command can be used to achieve both of these results together, i.e. store the result to a file while chaining the same output to another command.

Tee command.

By default, tee command reads from standard input, and writes to standard output and files. Let us see an example of tee command:

$ ls / | tee rootlisting.txt
bin
boot
cdrom
dev
etc
home
initrd.img
initrd.img.old
lib
lost+found
media
mnt
opt
proc
root
sbin
selinux
srv
sys
tmp
usr
var
vmlinuz
vmlinuz.old

In this example, the tee command stores the output of "ls /" command in a file named rootlisting.txt and to standard output as well. With tee command, you can store intermediate results of the commands in different stages of chaining as:

$ ls /etc | tee stage1.txt | grep ^s | tee stage2.txt | sort -r
sysctl.d
sysctl.conf
sudoers.d
sudoers
ssl
ssh
speech-dispatcher
sound
snmp
smi.conf
skel
shells
shadow-
shadow
sgml
services
sensors.d
sensors3.conf
security
securetty
sane.d
samba

Here, first /etc directory is listed and then output is stored in the file named stage1.txt. This output is filtered through grep for the lines starting with letter s. This filtered output is stored in the file stage2.txt. Finally, the filtered output is reverse sorted using sort -r command. This final output is displayed on the terminal. Thus tee command is used to store intermediate results to files. The tee command can store the output to more than one file:

$ ls /etc | grep ^s | tee file1.txt file2.txt | sort -r
sysctl.d
sysctl.conf
sudoers.d
sudoers
ssl
ssh
speech-dispatcher
sound
snmp
smi.conf
skel
shells
shadow-
shadow
sgml
services
sensors.d
sensors3.conf
security
securetty
sane.d
samba

$ ls -l file*
-rw-r--r-- 1 raghu raghu 182 2012-08-19 13:07 file1.txt
-rw-r--r-- 1 raghu raghu 182 2012-08-19 13:07 file2.txt

In this example, the output if grep filter is stored in two files: file1.txt and file2.txt. Tee command by default overwrites any content in the file. The output can be appended to the file with -a option:

$ ls /boot/ | tee -a file1.txt
abi-2.6.38-12-generic
abi-2.6.38-13-generic
abi-2.6.38-8-generic
config-2.6.38-12-generic
config-2.6.38-13-generic
config-2.6.38-8-generic
grub
initrd.img-2.6.38-12-generic
initrd.img-2.6.38-13-generic
initrd.img-2.6.38-8-generic
memtest86+.bin
memtest86+_multiboot.bin
System.map-2.6.38-12-generic
System.map-2.6.38-13-generic
System.map-2.6.38-8-generic
vmcoreinfo-2.6.38-12-generic
vmcoreinfo-2.6.38-13-generic
vmcoreinfo-2.6.38-8-generic
vmlinuz-2.6.38-12-generic
vmlinuz-2.6.38-13-generic
vmlinuz-2.6.38-8-generic

You can check the contents of the file named file1.txt to confirm your result:

$ cat file1.txt
samba
sane.d
securetty
security
sensors3.conf
sensors.d
services
sgml
shadow
shadow-
shells
skel
smi.conf
snmp
sound
speech-dispatcher
ssh
ssl
sudoers
sudoers.d
sysctl.conf
sysctl.d
abi-2.6.38-12-generic
abi-2.6.38-13-generic
abi-2.6.38-8-generic
config-2.6.38-12-generic
config-2.6.38-13-generic
config-2.6.38-8-generic
grub
initrd.img-2.6.38-12-generic
initrd.img-2.6.38-13-generic
initrd.img-2.6.38-8-generic
memtest86+.bin
memtest86+_multiboot.bin
System.map-2.6.38-12-generic
System.map-2.6.38-13-generic
System.map-2.6.38-8-generic
vmcoreinfo-2.6.38-12-generic
vmcoreinfo-2.6.38-13-generic
vmcoreinfo-2.6.38-8-generic
vmlinuz-2.6.38-12-generic
vmlinuz-2.6.38-13-generic
vmlinuz-2.6.38-8-generic

Filed Under : HOWTOS, LINUX COMMANDS, LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.