linux tee命令_Linux tee命令示例

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
Tee Command Options
Tee命令选项


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.txtout2.txt的字符串都写入控制台。

Output

输出量

Linux Tee Command Basic Example
Linux Tee Command Basic Example
Linux Tee命令基本示例

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
Linux Tee Command Append
Linux Tee命令附件

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
Linux Tee Command Hide Console Output
Linux Tee命令隐藏控制台输出


结论 (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)



翻译自: https://www.journaldev.com/33765/linux-tee-command

linux tee命令

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值