Redirection with 'tee' - having your cake and eating it too.

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
tallboy
Posts: 1760
Joined: Tue 21 Sep 2010, 21:56
Location: Drøbak, Norway

Redirection with 'tee' - having your cake and eating it too.

#1 Post by tallboy »

There is a very useful command named 'tee', that I don't see mentioned very often. I have gathered some info here.
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
Lists the files (displays the output on the screen) and also sends the output to a file: 'my_directories.txt'. (puppy specific - the original would be /home/username instead of /root)

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
tallboy
True freedom is a live Puppy on a multisession CD/DVD.

Post Reply