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

How to use Tee command in linux system

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

Share

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

Editor to share with you how to use the Tee command in the linux system. I hope you will get something after reading this article. Let's discuss it together.

The Tee instruction reads the data from the standard input device, outputs its contents to the standard output device, and saves it to a file.

Tee command syntax tee option file name example 1: basic method

The main function of the tee command is to display the output of the command and save it to a file. In the following example, we examine the block devices in the system and send the results to the tee command, which displays the output to the terminal and saves it in a file in devices.txt:

[root@localhost ~] # lsblk | tee devices.txt

You can use the cat command to check the contents of the devices.txt file, as follows:

Several examples of the use of the [root@localhost ~] # cat devices.txtTee command several examples of the Tee command example 2: the command output is written to multiple files

In addition, the output of the command can be written to multiple files, separated by spaces. As follows:

[root@localhost ~] # hostnamectl | tee file1.txt file2.txt

We called the hostnamectl command to print the hostname and other details of the system, and saved the standard output as two files, file1.txt and file2.txt

Use the cat command to view file1.txt and file2.txt:

Example 3: silently output to a file

If you want to hide or disable the tee command from printing on the screen, redirect the output to / dev/null, as follows:

[root@localhost ~] # df-Th | several usage examples of the tee file4.txt > / dev/nullTee command several usage examples of the Tee command IV: append output to the file

By default, the tee command overwrites the contents of the file. To append the output and prevent the current content from being erased, use the-an or-append options.

[root@localhost ~] # lsblk | tee file1.txt [root@localhost ~] # date | tee-a file1.txt [root@localhost ~] # cat file1.txt

In the above command, the first command outputs the contents to file1.txt, and the second command tee adds the-an option to file1.txt. The third command uses cat to view file1.txt and finds that the output of the above two commands has been saved.

Example 5: tee command is used with sudo

Suppose you can use only one sudo user and want to write to a file to which the root user has permissions. Any promotion operation requires that the sudo user be called before the command. To do this, simply precede the tee command with sudo, as follows:

[bob@localhost ~] $echo "10.200.50.20 db-01" | several use examples of the sudo tee-a / etc/hosts/Tee command several use examples of the Tee command 6: redirect the output of one command to another command

Using the tee command, we can redirect the output of one command to another. Here, the output of the first command will be used as input to the second command. Examples are as follows:

[root@localhost ~] # grep 'root' / etc/passwd | tee / tmp/passwd.tmp | wc-12 [root@localhost ~] # cat / tmp/passwd.tmproot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin [root@localhost ~] #

Several examples of using the Tee command several examples of using the Tee command example 7: using tee in the vim editor

Suppose you are a non-root user and you are modifying a file owned by a root user and forgot to open the file with sudo permission. Now you want to save your changes. The example is as follows:

[bob@localhost ~] $vim / etc/hosts

Now to save the changes to the / etc/hosts file in the vim editor, run:

W! sudo tee%

The above prompts for the user's password, which can be saved successfully after the input is completed.

After reading this article, I believe you have a certain understanding of "how to use Tee commands in linux system". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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

Development

Wechat

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

12
Report