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

vue-cropper component realizes image cutting and uploading

This article shares the specific code of the vue-...

Reasons why MySQL cancelled Query Cache

MySQL previously had a query cache, Query Cache. ...

Navicat remote connection to MySQL implementation steps analysis

Preface I believe that everyone has been developi...

Using MySQL in Windows: Implementing Automatic Scheduled Backups

1. Write a backup script rem auther:www.yumi-info...

Vue implements multiple ideas for theme switching

Table of contents Dynamically change themes The f...

Detailed explanation on how to get the IP address of a docker container

1. After entering the container cat /etc/hosts It...

How to deploy python crawler scripts on Linux and set up scheduled tasks

Last year, due to project needs, I wrote a crawle...

MySQL 8.0.18 deployment and installation tutorial under Windows 7

1. Preliminary preparation (windows7+mysql-8.0.18...

Zabbix monitors Linux hosts based on snmp

Preface: The Linux host is relatively easy to han...

JavaScript realizes magnifying glass special effects

The effect to be achieved: When the mouse is plac...

How to forget the password of Jenkins in Linux

1.Jenkins installation steps: https://www.jb51.ne...

Introduction to scheduled tasks in Linux system

Table of contents 1. Customize plan tasks 2. Sync...

Tutorial on logging into MySQL after installing Mysql 5.7.17

The installation of mysql-5.7.17 is introduced be...

Vue implements start time and end time range query

This article shares with you how to query the sta...