This tutorial explains Linux “tee” command, options and its usage with examples.
DESCRIPTION
The tee command reads standard input, then writes the output of a program to standard output and simultaneously copies it into the specified file or files.
SYNOPSIS
tee [ -a ] [ File … ]
OPTIONS
-a
Adds the output to the end of File instead of writing over it.
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
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Apply for Programming Internship
- Practice Programming MCQs
- Check Information Technology Books
- Check Linux Books