Tee Command Usage Examples

Tee command is used to store and view (both at the same time) the output of any other command.

Tee command writes to the STDOUT, and to a file at a time as shown in the examples below.

Example 1: Write output to stdout, and also to a file

The following command displays output only on the screen (stdout).

$ ls 

The following command writes the output only to the file and not to the screen.

$ ls > file

The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file.

$ ls | tee file

Example 2: Write the output to two commands

You can also use tee command to store the output of a command to a file and redirect the same output as an input to another command.

The following command will take a backup of the crontab entries, and pass the crontab entries as an input to sed command which will do the substituion. After the substitution, it will be added as a new cron job.

$ crontab -l | tee crontab-backup.txt | sed 's/old/new/' | crontab –

Misc Tee Command Operations

By default tee command overwrites the file. You can instruct tee command to append to the file using the option –a as shown below.

$ ls | tee –a file

You can also write the output to multiple files as shown below.

$ ls | tee file1 file2 file3

Example : How To Use Tee Command In Linux

In Unix and Linux world, a large number of small, 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

EXAMPLES

1. Write output to stdout, and also to a file

The following command displays output only on the screen (stdout).

$ ls

The following command writes the output only to the file and not to the screen.

$ ls > file

The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file.

$ ls | tee file1.txt

2. You can instruct tee command to append to the file using the option –a as shown below

$ ls | tee –a fileq.txt

3. You can also write the output to multiple files as shown below.

$ ls | tee file1 file2 file3

4. Write the output to two commands

You can also use tee command to store the output of a command to a file and redirect the same output as an input to another command.

The following command will long list directory contents, store it in abc.txt and then count the no. of lines and echo it

$ ls -l | tee abc.txt | wc -l

Linux Redirect Error Output To File

Iam a new Ubuntu Linux and bash shell user. I also know how to redirect output from display/screen to a file using the following syntax:

cmd > file
ls > file

However, some time errors are displayed on screen. How do I store and redirect output from the computer screen to a file on a Linux or Unix-like systems?

Bash / ksh and other modern shell on Linux has three file descriptors:

  1. stdin (0)
  2. stdout (1)
  3. stderr (2)

Syntax To redirect all output to file

The syntax is as follows to redirect output (stdout) as follows:

 
command-name >  output.txt
command-name >  stdout.txt

Syntax To redirect all error to file

The syntax is as follows to redirect errors (stderr) as follows:

 
command-name 2> errors.txt
command-name 2> stderr.txt

Syntax to redirect both output (stdout) and errors (stderr) to different files

The syntax:

 
command1 > out.txt 2> err.txt
command2 -f -z -y > out.txt 2> err.txt

Syntax to redirect both output (stdout) and errors (stderr) to same file

The syntax is:

 
command1 > everything.txt 2>&1
command1 -arg > everything.txt 2>&1

Syntax to redirect errors (stderr) to null or zero devices

Data written to a null or zero special file is discarded by your system. This is useful to silence out errors (also know as ‘error spam’):

 
command1 2> /dev/null
command1 2> /dev/zero
command2 -arg 2> /dev/null
command2 -arg 2> /dev/zero

Tip: Use tee command to redirect to both a file and the screen same time

The syntax is:

 
command1 |& tee log.txt
## or ##
command1 -arg |& tee log.txt
## or ##
command1 2>&1 | tee log.txt

Another usage:

#!/bin/bash
# My script to do blah ...
foo(){
 :
} 2>&1 | tee foo.log

OR

#!/bin/bash
# My script to do blah ...
{
   command1
   command2
} 2>&1 | tee script.log

Advertisements

Posted on June 17, 2015, in Linux (Ubuntu/CentOS). Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: