通过9个案例学会使用 Linux Tee 命令

鸠摩智首席音效师 -
通过9个案例学会使用 Linux Tee 命令

Linux Tee 命令是一个命令行工具,它从标准输入中读取并同时将结果写入标准输出和文件。换句话说,Linux 中的 tee 命令可以称的上一石二鸟:从标准输入读取并将结果打印到文件中,同时输出到标准输出。

在本指南中,我们将进一步介绍 Linux tee 命令,并使用一些示例演示它的用法。

基本语法

$ tee OPTIONS filename

下面是 tee 命令可以使用的一些选项

linux-tee-command-options

在 tee 命令的语法中,filename 指的是一个或多个文件。

(1) 基本用法

在下面的例子中,我们正在检查系统中的块设备,并将结果传输到 tee 命令,该命令将输出显示到终端,同时将其保存到名为 block devices.txt 的文件中

$ lsblk | tee block_devices.txt

lsblk-tee-command-output-linux

使用 cat 命令检查 block devices.txt 文件的内容

$ cat block_devices.txt
(2) 保存输出到多个文件

可以将命令的输出写入多个空格分隔的文件,如下面的语法所示。

$ command | tee file1 file2 file3 . . .

In the following example, we have invoked the command to print the hostname of our system among other details and save the standard output to two files file1.txt, and file2.txt

在下面的示例中,我们调用了 hostnamectl 命令来打印系统的主机名和其他详细信息,并将标准输出保存到两个文件 file1.txt 和 file2.txt 中

$ hostnamectl | tee file1.txt file2.txt

tee-command-output-files-linux

同样,您可以使用 cat 命令确认两个文件中的内容,如下所示

$ cat file1.txt
$ cat file2.txt
(3) 抑制输出

If you want to hide or suppress tee command from printing the output on the screen then redirect the output to /dev/null as shown:

如果要隐藏或抑制在屏幕上打印输出,则将输出重定向到 /dev /null

$ command | tee file > /dev/null

例如:

$ df -Th | tee file4.txt > /dev/null

tee-command-suppress-output

(4) 将输出附加到文件中

By default, tee command overwrites the contents of a file. To append the output and prevent the erasure of the current content, use the -a or –append options.

默认情况下,tee 命令覆盖文件内容。要追加输出并防止当前内容被覆盖,请使用 -a-append 选项。

$ command | tee -a file

如图所示,我们将 date 命令的输出附加到 file1.txt,该文件中已经包含了系统上 USB 设备的信息。

$ date | tee -a file1.txt

Append-output-tee-command-linux

(5) 配合 sudo 使用

假设作为 sudo 用户,您希望在 root 用户拥有的文件上写入。

$ echo "10.200.50.20 db-01" | sudo tee -a /etc/hosts/

tee-with-sudo-command-linux

(6) 输出重定向

使用 tee 命令,我们可以轻松地将一个命令的输出重定向到另一个命令。第一个命令的输出将作为第二个命令的输入。如下所示:

$ grep 'root' /etc/passwd | tee /tmp/passwd.tmp | wc -l
2
$ cat /tmp/passwd.tmp
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
$
(7) 在 vi 编辑器中将更改保存到文件中

Let’s assume you are working as non-root user and you are making changes to root owned file and you forget to put sudo In front of command and now you want to save changes, example is demonstrated below:

假设您以非 root 用户的身份工作,正在对 root 用户拥有的文件进行更改,而您忘记在命令前加上 sudo,现在您想要保存更改,示例如下

$ vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.1.60   mail.linuxtechi.com
192.168.1.50   ns.linuxtechi.com
192.168.1.40    pxe.linuxtechi.com

在保存对文件的更改时,您将得到只读提示

save-changes-error-read-only-file-linux

Now to save the changes to /etc/hosts file within the vi editor, run

如果要将更改保存到 /etc /hosts 文件,请运行

:w !sudo tee %

它会提示您输入用户的密码,如果用户有 sudo 权限,那么更改将被保存。

Save-Changes-to-file-tee-commmand-vi-editor

Prompt-sudo-user-password-tee-vi-editor

(8) 忽略中断信号

Using ‘-i’ option in tee command can ignore interrupt signal (CTRL-C), example is shown below:

使用 -i 选项可以忽略中断信号 (ctrl+c),示例如下

$ ping -c 5 linuxtechi.com  | tee -i /tmp/pingtest.tmp

tee-command-ignore-interrupt-signal-linux

(9) 在脚本中的应用

tee 命令在 shell 脚本中也经常使用,下面是一个常见的例子

$ vi basic-script.sh
#!/bin/bash
LOGFILE=/tmp/basic-logs-$(date +%d%m%Y)
FLAVOR=$(cat /etc/*-release  | grep -w 'NAME=' | cut -d"=" -f2 | awk '{print $1}'| sed 's/"//g')
if [ $FLAVOR == CentOS ];
then
   dmesg | grep -i 'error' | tee -a $LOGFILE
   grep -i 'installed' /var/log/dnf.log | tee -a $LOGFILE
else
   echo 'do nothing'
fi

tee-command-shell-script-linux

我的开源项目course-tencent-cloud(酷瓜云课堂 - gitee仓库)course-tencent-cloud(酷瓜云课堂 - github仓库)
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

linux

扩展阅读

加个好友,技术交流

1628738909466805.jpg