linux tee命令
In this article, we shall go through the tee command in Linux. This is commonly used to read the input and write to both the Standard Output (stdout) and to one or more files.
在本文中,我们将介绍Linux中的tee命令。 这通常用于读取输入并写入到标准输出(stdout)和一个或多个文件。
The tee command is commonly used along with other commands through the pipe (|
) operator.
tee命令通常与其他通过管道( |
)运算符使用的命令一起使用。
Let us understand how this command works through a few examples.
让我们通过几个示例来了解此命令的工作方式。
Linux tee命令语法 ( Linux tee Command Syntax)
The tee command is used with the following syntax:
tee命令使用以下语法:
tee [-OPTIONS] [FILES]
-OPTIONS
represent the various possible options along with the tee command.-OPTIONS
代表各种可能的选项以及tee命令。FILES
represent all the files that tee writes the output data to, along with stdout.FILES
代表所有将输出数据写入tee的文件,以及stdout 。
tee命令选项 (tee Command Options)
There are different options for the tee command that changes the behavior of the command. The below table summarizes this for us.
tee命令有不同的选项可以更改命令的行为。 下表为我们总结了这一点。
![Tee Command Options](https://i-blog.csdnimg.cn/blog_migrate/515c8a0829e9ed113a46e35f18f306c2.png)
tee
命令在Linux中的用法 (Usage of tee
Command in Linux)
1.写入多个文件 (1. Write to multiple files)
The most simple usage of this command would be to write to both the stdout
and to all the given files. But since we need to give input to stdin
of tee, it is commonly used in a pipeline sequence.
此命令最简单的用法是同时向stdout
和所有给定文件写入数据。 但是由于我们需要将输入提供给tee的 stdin
,因此它通常在管道序列中使用。
The below example shows this:
以下示例显示了这一点:
echo 'HELLO WORLD' | tee out1.txt out2.txt
This echoes HELLO WORLD and redirects the stdout
using the pipe operator and passes it into tee
. We will get the string HELLO WORLD written to both the Console, as well as to out1.txt
and out2.txt
.
这回显HELLO WORLD,并使用管道运算符重定向stdout
并将其传递到tee
。 我们将把字符串HELLO WORLD以及同时写入out1.txt
和out2.txt
的字符串都写入控制台。
Output
输出量
![Linux Tee Command Basic Example](https://cdn.journaldev.com/wp-content/uploads/2020/01/linux-tee-command-basic-example.jpg)
2.附加到多个文件 (2. Append to multiple files)
By default, the tee command overwrites the files specified as arguments. To avoid that, we can use the -a
(--append
) option to append to those files instead.
缺省情况下,tee命令将覆盖指定为参数的文件。 为避免这种情况,我们可以使用-a
(-- --append
)选项来附加到这些文件。
echo 'HELLO WORLD PART 2' | tee -a out1.txt out2.txt
Output
输出量
![Linux Tee Command Append](https://i-blog.csdnimg.cn/blog_migrate/8e097abe504fe67501b4a33a6bb795ca.png)
3.忽略中断 (3. Ignore Interrupts)
We can use the -i
option to ignore any interrupt signals (such as Ctrl+C) during the execution of tee
.
我们可以使用-i
选项在执行tee
忽略任何中断信号(例如Ctrl + C )。
command | tee -i out.txt
4.隐藏控制台输出 (4. Hide the Console Output)
We can hide the output to the Console by redirecting the stdout
of tee to /dev/null
.
通过将tee的stdout
重定向到/dev/null
我们可以将输出隐藏到控制台。
ls -l | tee out3.txt > /dev/null
This sequence writes the output of ls -l
to out3.txt
, without printing to the Console.
此序列将ls -l
的输出写入out3.txt
,而不打印到控制台。
Output
输出量
![Linux Tee Command Hide Console Output](https://i-blog.csdnimg.cn/blog_migrate/04b03f13bd9adf0b2d86fde88b3bebb3.png)
结论 (Conclusion)
In this article, we learned about the usage of the tee command in Linux, which is very useful for writing to multiple files. It is commonly used in a pipeline sequence, where you want to look at the intermediate output by printing to stdout
.
在本文中,我们了解了tee命令在Linux中的用法,这对于写入多个文件非常有用。 它通常用于管道序列中,在该序列中,您希望通过打印到stdout
来查看中间输出。
We learned about how we can use the tee
command with various options. I hope this helped you understand more about the tee command, which can be a very nifty tool for a programmer!
我们了解了如何在各种选项中使用tee
命令。 我希望这可以帮助您了解有关tee命令的更多信息,对于程序员来说,这可能是一个非常漂亮的工具!
参考资料 (References)
linux tee命令