Home » Linux Tee Command

Linux Tee Command

by Online Tutorials Library

Linux tee Command

Linux tee command is quite similar to the ‘cat’ command, with only one difference. It puts stdin on stdout and also put them into a file. It is one of the most used commands with other commands through piping. It allows us to write whatever is provided from std input to std output. Optionally, it provides writing to one or more files. The command name tee comes from T splitter used in plumbing.

Syntax:

Options:

The following are some useful options that can be used with the tee command to make it more specific:

-a, –append: It is used to append the data to the given files, it does not overwrite data.

-i, –ignore-interrupts: It is used to ignore the interrupt signals.

-p: It is used to diagnose errors writing to non-pipes.

–output-error[=MODE]: It is used to set behavior on write error mode.

–help: It is used to display the help documentation.

–version: it is used to display the version information.

Examples of the tee Command

Let’s see the following examples of the tee command:

How to use the tee command

The tee command is used to write a standard input to standard output and a file. It is used after a pipe. To write to standard output and a file, specify the tee command after a pipe and provide the file(s) name. Consider the below command:

The above command will write the input of the ‘weeks.txt’ to ‘newfile.txt.’ Consider the below output:

Linux tee Filters

From the above output, a file ‘newfile.txt’ is created using the tee command.

Write a file and append output

The ‘-a’ option is used with the tee command to append the output and write it to a file. Consider the below command:

The above command will append the specified input to the ‘newfile.txt’. Consider the below output:

Linux tee Filters

As from the above output, the specified input is appended to ‘newfile.txt’.

Write the State of Data to a File

Writing the state of data is very useful for taking backup or creating a snap of the data for the debugging purpose. It can be easily done by using the tee command.

To write the state of data to a file, execute the below command:

The above command will write the data to the pipe1.txt. Consider the below output:

Linux tee Filters

From the above output, the snap of current working directory data is stored in ‘newfile.txt’.

Write to multiple files

The tee command allows us to write to multiple files. To write to the multiple files, specify the names of the files after the tee command as follows:

The above command will create all the specified files. Consider the below output:

Linux tee Filters

Write to a privileged file

The tee command allows us to write to a file having sudo privilege. If we try to write a file owned by the root user will through the permission error. But, we can elevate the sudo permission by executing the tee command as follows:

Ignoring Interrupts

The ‘-i’ option is used to ignore the interrupts. This is useful if we want to stop and exit the command gracefully during the execution. It is used as follows:

Hide the output

To restrict the tee command not to write to the standard output, redirect it to “/dev/null”. Execute the command as follows:

Consider the below output:

Linux tee Filters


Next TopicLinux tr

You may also like