Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation for using Linux tee command

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The tee command is mainly used to output to standout (standard output stream, usually the command execution window) as well as to a file. Here is the man information of tee.

Read from standard input and write to standard output and files

Let's familiarize ourselves with the tee command through several application scenarios.

Scenario 1: how to use the tee command

The format of the tee command is:

Tee [OPTION]... [FILE]...

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

[mysql@localhost] $ping baidu.comPING 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 ms64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=128 time=33.1 ms64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=128 time=31.9 ms64 bytes from 220.181.57 .216 (220.181.57.216): icmp_seq=4 ttl=128 time=30.9 ms...

Now that we want to save the output to the console to another file for other purposes, then the tee command can work.

[mysql@localhost ~] $ping baidu.com | while outputting tee ping-baidu.log # to the console Save the content to the ping-baidu.log file 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 ms64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=128 time=30.5 ms64 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 whether 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 ms64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=128 time=30.5 ms64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=128 time=30.2 ms

Scenario 2: how do I append content to a file?

In the above operation, output to ping-baidu.log, if this file exists, first clear all the contents of the file, and then enter the content, for some scenarios this method is not suitable, we want each output is appended to the file, then we can use the-a parameter to specify.

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

Scenario 3: how do I output to multiple files at the same time?

Append the file name to be entered directly after the tee command in the following format:

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

Scenario 4: how to redirect the output of a tee command to the input stream of another command

Directly follow the tee command with | and the next command, which is to do it in the way of pipeline.

Scenario 5: increase the level of permissions to write to a file by using the tee command

In the actual operation of the liunx server, when we configure some files, when we finish configuring and save them, we find that our current users do not have files to save files. At this time, our hearts are very broken. Is there a solution? At this time, tee ordered us to save our troubles.

The specific operation is to enter: W! sudo tee% when the file is saved. 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 rest assured: Q! Quit the file.

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

Reference:

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

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report