Unix/Linux borrows an idea here from the plumbing trade. This is a redirection operator, but with a difference. Like the plumber's 'tee', it permits 'siphoning off' to a file the output of a command or commands within a pipe, but without affecting the result. This is useful for printing an ongoing process to a file or paper, perhaps to keep track of it for debugging purposes.
The `tee' command copies standard input to standard output and also to any files given as arguments. This is useful when you want not only to send some data down a pipe, but also to save a copy.
Synopsis: tee [OPTION]... [FILE]...
Examples:
Code: Select all
cat listfile* | sort | tee check.file | uniq > result.file
Here, the file 'check.file' contains the concatenated sorted "listfiles", before the duplicate lines are removed by uniq, and the result of that operation is sent to 'result.file'.
Code: Select all
ls /root | tee my_directories.txt
You can also write the output to multiple files as shown below.
Code: Select all
ls | tee file1 file2 file3
Note:
- If a file being written to does not already exist, it is created.
- By default tee command overwrites the file.You can instruct the 'tee' command to append to the file using the option –a as shown below.
`-a' `--append' Append standard input to the given files rather than overwriting them.Code: Select all
]ls | tee –a file