关于linux:通过9个案例学会使用-Linux-Tee-命令

31次阅读

共计 2655 个字符,预计需要花费 7 分钟才能阅读完成。

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

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

根本语法

$ tee OPTIONS filename

上面是 tee 命令能够应用的一些选项

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

(1) 根本用法

在上面的例子中,咱们正在查看零碎中的块设施,并将后果传输到 tee 命令,该命令将输入显示到终端,同时将其保留到名为 block devices.txt 的文件中

$ lsblk | tee block_devices.txt

应用 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

同样,您能够应用 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

(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

(5) 配合 sudo 应用

假如作为 sudo 用户,您心愿在 root 用户领有的文件上写入。

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

(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

在保留对文件的更改时,您将失去只读提醒

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

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

:w !sudo tee %

它会提醒您输出用户的明码,如果用户有 sudo 权限,那么更改将被保留。

(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

(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

我的开源我的项目

  • course-tencent-cloud(酷瓜云课堂 – gitee 仓库)
  • course-tencent-cloud(酷瓜云课堂 – github 仓库)

正文完
 0