Detailed explanation of Linux tee command usage

Detailed explanation of Linux tee command usage

The tee command is mainly used to output to standout (standard output stream, usually the command execution window) while also outputting the content to a file. The following is the man page information of tee

read from standard input and write to standard output and files

Next, we will familiarize ourselves with the tee command through several application scenarios.

Scenario 1: How to use the tee command

The tee command format is:

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

From the definition in the man file, we know that tee reads data from the standard input stream, so here we use a simple command to generate an output stream as the input stream of tee . Here we use the ping command.

[mysql@localhost ~]$ ping baidu.com
PING baidu.com (220.181.57.216) 56(84) bytes of data.
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=1 ttl=128 time=30.1 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=128 time=33.1 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=128 time=31.9 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=4 ttl=128 time=30.9 ms
...

Now we want to output to the console and save the content output to another file for other purposes. Then tee command can come into play.

[mysql@localhost ~]$ ping baidu.com | tee ping-baidu.log #Save the content to the ping-baidu.log file while outputting to the console. PING baidu.com (220.181.57.216) 56(84) bytes of data.
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=1 ttl=128 time=30.6 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=128 time=30.5 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=128 time=30.2 ms
^C[mysql@localhost ~]$ cat ping-baidu.log #Check if the file content is consistent with the output PING baidu.com (220.181.57.216) 56(84) bytes of data.
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=1 ttl=128 time=30.6 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=128 time=30.5 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=128 time=30.2 ms

Scenario 2: How to append content to a file?

In the above operation, the output is to ping-baidu.log. If this file exists, first clear all the contents in the file, and then input the content. This method is not suitable for some scenarios. We hope that each output is appended to the file. In this case, we can specify it through the -a parameter.

ping baidu.com | tee -a ping-baidu.log 

insert image description here

Scenario 3: How to output to multiple files at the same time?

Simply append the file name to be input after the tee command. The specific format is:

ping baidu.com | tee ping.log ping-baidu.log 

insert image description here

Scenario 4: How to redirect the output of tee command as the input stream of another command

Simply add | and the next command after tee command to follow the pipeline method.

insert image description here

Scenario 5: Raising the permission level to write to a file by using the tee command

When we are actually operating the liunx server, when we are configuring certain files, after the configuration is completed and when we save them, we find that our current user does not have the file to save the file. At this time, we feel very frustrated. Is there any solution? At this time, the tee command comes to save our troubles.

The specific operation is to enter :w !sudo tee % when saving the file. At this time, vi will remind you to enter the password for the sudo operation. After entering it correctly, the file will be saved. At this time, you can safely exit the file :q!

Scenario 6: How to make tee ignore interrupt events

The -i parameter allows tee to ignore interrupt events (SIGINT)

ping baidu.com | tee -i ping.log

refer to:

https://www.howtoforge.com/linux-tee-command/

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Using puppeteer to implement webpage screenshot function on linux (centos)
  • Linux outputs information and records it to a file (tee command)

<<:  A brief discussion on several ways to implement front-end JS sandbox

>>:  MySQL 8.0.16 compressed package installation and configuration method graphic tutorial

Recommend

Shtml Concise Tutorial

Shtml and asp are similar. In files named shtml, s...

How to update the view synchronously after data changes in Vue

Preface Not long ago, I saw an interesting proble...

Vue-cli creates a project and analyzes the project structure

Table of contents 1. Enter a directory and create...

Solution to the Docker container cannot be stopped and deleted

Find the running container id docker ps Find the ...

The whole process of realizing website internationalization using Vite2 and Vue3

Table of contents Preface Install vue-i18n Config...

Detailed explanation of system input and output management in Linux

Management of input and output in the system 1. U...

JavaScript web page entry-level development detailed explanation

Part 3: ❤Three ways to overlook backend data rece...

Using an image as a label, the for attribute does not work in IE

For example: Copy code The code is as follows: <...

Docker image management common operation code examples

Mirroring is also one of the core components of D...

How to add double quotes in HTML title

<a href="https://www.jb51.net/" titl...

Discussion on horizontal and vertical centering of elements in HTML

When we design a page, we often need to center th...

How to handle concurrent updates of MySQL data

Will UPDATE lock? Will the SQL statement be locke...

Vue uses the Element el-upload component to step on the pit

Table of contents 1. Basic Use 2. Image quantity ...

Detailed steps for installing, configuring and uninstalling QT5 in Ubuntu 14.04

1. I downloaded QT5.13 version before, but after ...