tee Command in Linux with Examples

Posted in

tee Command in Linux with Examples
sangeeta.gulia

Sangeeta Gulia
Last updated on March 29, 2024

    Reading from standard input and writing to standard output is a common task in Linux, which can be used for various debugging and informative-related things. This operation of input-output is performed by the tee command in Linux. In this article, we will talk about the tee command and its options and examples.

    tee Command in Linux

    The tee command simultaneously reads from standard input and writes to standard output and one or more files. The command is named after the plumbing T-splitter. It simply breaks down a program's output so that it may be shown as well as saved in a file. tee is typically used in conjunction with other commands through piping. It does both jobs at the same time, transfers the output to the given files or variables, and displays the output. The tee command has the following syntax:

    $ tee [OPTIONS] [FILE]

    Options and Examples of tee command

    1.-a

    It basically appends to the specified file rather than overwriting it.

    $ wc -l demo1.txt|tee -a demo2.txt

    2.–help

    Displays a help message before exiting.

    $ tee --help

    3.–version

    This option displays the version number and exits.

    $ tee --version

    4.-i

    Ignore interrupt signals

    5. Writing to several files

    Additionally, the tee command can write to several files. As parameters, give a list of files separated by spaces:

    $ command | tee demo1.text demo2.txt

    6. Routing the output to destination

    You can route also tee's output to given destination, like /dev/null if you don't want it to go to the standard output:

    command | tee demp.txt >/dev/null

    7. Hide the Output

    Use the following syntax to tell tee command to save command output to a file and ignore terminal output:

    [command] | tee [options] [filename] >/dev/null

    8. Using tee with Sudo

    Place the sudo command just before tee to allow tee to write to a root-owned or another user-owned file.

    [command] | sudo tee [options] [filename]

    9. Diagnose Errors Writing to Non-Pipes

    Use the -p parameter to tell tee to display an error message if the process fails:

    [command] | tee -p [filename]

    Conclusion

    In this article, we discussed the tee command in Linux along with its options which perform different tasks, including appending to the specified file rather than overwriting it, displaying a help message before exit, showing the version number, and ignoring interrupt signals.

    People are also reading:

    Leave a Comment on this Post

    0 Comments