为初学者介绍的 Linux tee 命令（6 个例子）
tee 输出
![](/data/attachment/album/201803/12/112453jooo6kdo0yohot7o.jpg)


有时候，你会想手动跟踪命令的输出内容，同时又想将输出的内容写入文件，确保之后可以用来参考。如果你想寻找这相关的工具，那么恭喜你，Linux 已经有了一个叫做 `tee` 的命令可以帮助你。


本教程中，我们将基于 `tee` 命令，用一些简单的例子开始讨论。但是在此之前，值得一提的是，本文我们所有的测试实例都基于 Ubuntu 16.04 LTS。


### Linux tee 命令


`tee` 命令基于标准输入读取数据，标准输出或文件写入数据。感受下这个命令的语法：



```
tee [OPTION]... [FILE]...

```

这里是帮助文档的说明：



> 
> 从标准输入中复制到每一个文件，并输出到标准输出。
> 
> 
> 


让 Q&A（问&答）风格的实例给我们带来更多灵感，深入了解这个命令。


### Q1、 如何在 Linux 上使用这个命令？


假设因为某些原因，你正在使用 `ping` 命令。



```
ping google.com

```

[![如何在 Linux 上使用 tee 命令](/data/attachment/album/201803/12/112504ouk3wkdnnt5bww3d.png)](https://www.howtoforge.com/images/command-tutorial/big/ping-example.png)


然后同时，你想要输出的信息也同时能写入文件。这个时候，`tee` 命令就有其用武之地了。



```
ping google.com | tee output.txt

```

下面的截图展示了这个输出内容不仅被写入 `output.txt` 文件，也被显示在标准输出中。


[![tee command 输出](/data/attachment/album/201803/12/112504j5kwkwn4k5wldzzj.png)](https://www.howtoforge.com/images/command-tutorial/big/ping-with-tee.png)


如此应当明白了 `tee` 的基础用法。


### Q2、 如何确保 tee 命令追加信息到文件中？


默认情况下，在同一个文件下再次使用 `tee` 命令会覆盖之前的信息。如果你想的话，可以通过 `-a` 命令选项改变默认设置。



```
[command] | tee -a [file]

```

基本上，`-a` 选项强制 `tee` 命令追加信息到文件。


### Q3、 如何让 tee 写入多个文件？


这非常之简单。你仅仅只需要写明文件名即可。



```
[command] | tee [file1] [file2] [file3]

```

比如：



```
ping google.com | tee output1.txt output2.txt output3.txt

```

[![如何让 tee 写入多个文件](/data/attachment/album/201803/12/112504migehhaaezheiaus.png)](https://www.howtoforge.com/images/command-tutorial/big/tee-mult-files1.png)


### Q4. 如何让 tee 命令的输出内容直接作为另一个命令的输入内容？


使用 `tee` 命令，你不仅可以将输出内容写入文件，还可以把输出内容作为另一个命令的输入内容。比如说，下面的命令不仅会将文件名存入 `output.txt` 文件中，还会通过 `wc` 命令让你知道输入到 `output.txt` 中的文件数目。



```
ls file* | tee output.txt | wc -l

```

[![如何让 tee 命令的输出内容直接作为另一个命令的输入内容](/data/attachment/album/201803/12/112505ga9yz979wkze4n4l.png)](https://www.howtoforge.com/images/command-tutorial/big/tee-redirect-output.png)


### Q5. 如何使用 tee 命令提升文件写入权限？


假如你使用 [Vim 编辑器](https://www.howtoforge.com/vim-basics) 打开文件，并且做了很多更改，然后当你尝试保存修改时，你得到一个报错，让你意识到那是一个 root 所拥有的文件，这意味着你需要使用 `sudo` 权限保存修改。


[![如何使用 tee 命令提升文件写入权限](/data/attachment/album/201803/12/112505oq4qddmqgdqcrc1j.png)](https://www.howtoforge.com/images/command-tutorial/big/vim-write-error.png)


如此情况下，你可以（在 Vim 内）使用 `tee` 命令来提高权限。



```
:w !sudo tee %

```

上述命令会向你索要 root 密码，然后就能让你保存修改了。


### Q6. 如何让 tee 命令忽视中断？


`-i` 命令行选项使 `tee` 命令忽视通常由 `ctrl+c` 组合键发起的中断信号（`SIGINT`）。



```
[command] | tee -i [file]

```

当你想要使用 `ctrl+c` 中断该命令，同时让 `tee` 命令优雅的退出，这个选项尤为实用。


### 总结


现在你可能已经认同 `tee` 是一个非常实用的命令。基于 `tee` 命令的用法，我们已经介绍了其绝大多数的命令行选项。这个工具并没有什么陡峭的学习曲线，所以，只需跟随这几个例子练习，你就可以运用自如了。更多信息，请查看 [帮助文档](https://linux.die.net/man/1/tee).




---


via: <https://www.howtoforge.com/linux-tee-command/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[CYLeft](https://github.com/CYLeft) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux tee Command Explained for Beginners (6 Examples)

### On this page

[Linux tee command](#linux-tee-command)[Q1. How to use tee command in Linux?](#q-how-to-use-tee-command-in-linux)[Q2. How to make sure tee appends information in files?](#q-how-to-make-sure-tee-appends-information-in-files)[Q3. How to make tee write to multiple files?](#q-how-to-make-tee-write-to-multiple-files)[Q4. How to make tee redirect output of one command to another?](#q-how-to-make-tee-redirect-output-of-one-command-to-another)[Q5. How to write to a file with elevated privileges using tee?](#q-how-to-write-to-a-file-with-elevated-privileges-using-tee)[Q6. How to make tee ignore interrupt?](#q-how-to-make-tee-ignore-interrupt)[Conclusion](#conclusion)

There are times when you want to manually track output of a command and also simultaneously make sure the output is being written to a file so that you can refer to it later. If you are looking for a Linux tool which can do this for you, you'll be glad to know there exists a command * tee* that's built for this purpose.

In this tutorial, we will discuss the basics of the tee command using some easy to understand examples. But before we do that, it's worth mentioning that all examples used in this article have been tested on Ubuntu 16.04 LTS.

## Linux tee command

The tee command basically reads from the standard input and writes to standard output and files. Following is the syntax of the command:

tee [OPTION]... [FILE]...

And here's how the man page explains it:

Copy standard input to each FILE, and also to standard output.

The following Q&A-styled examples should give you a better idea on how the command works.

## Q1. How to use tee command in Linux?

Suppose you are using the ping command for some reason.

ping google.com

And what you want, is that the output should also get written to a file in parallel. Then here's where you can use the tee command.

ping google.com | tee output.txt

The following screenshot shows the output was written to the 'output.txt' file along with being written on stdout.

So that should clear the basic usage of tee.

## Q2. How to make sure tee appends information in files?

By default, the tee command overwrites information in a file when used again. However, if you want, you can change this behavior by using the -a command line option.

[command] | tee -a [file]

So basically, the -a option forces tee to append information to the file.

## Q3. How to make tee write to multiple files?

That's pretty easy. You just have to mention their names.

[command] | tee [file1] [file2] [file3]

For example:

ping google.com | tee output1.txt output2.txt output3.txt

## Q4. How to make tee redirect output of one command to another?

You can not only use tee to simultaneously write output to files, but also to pass on the output as input to other commands. For example, the following command will not only store the filenames in 'output.txt' but also let you know - through wc - the number of entries in the output.txt file.

ls file* | tee output.txt | wc -l

## Q5. How to write to a file with elevated privileges using tee?

Suppose you opened a file in the [Vim editor](https://www.howtoforge.com/vim-basics), made a lot of changes, and then when you tried saving those changes, you got an error that made you realize that it's a root-owned file, meaning you need to have sudo privileges to save these changes.

In scenarios like these, you can use tee to elevate privileges on the go.

:w !sudo tee %

The aforementioned command will ask you for root password, and then let you save the changes.

## Q6. How to make tee ignore interrupt?

The -i command line option enables tee to ignore the interrupt signal (`SIGINT`

), which is usually issued when you press the crl+c key combination.

[command] | tee -i [file]

This is useful when you want to kill the command with ctrl+c but want tee to exit gracefully.

## Conclusion

You'll likely agree now that tee is an extremely useful command. We've discussed it's basic usage as well as majority of its command line options here. The tool doesn't have a steep learning curve, so just practice all these examples, and you should be good to go. For more information, head to the tool's [man page](https://linux.die.net/man/1/tee).