In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to use the tee command in Linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The Linux Tee command is a command-line tool that reads results from standard input and writes them to standard output and files. Using the tee command in Linux, you can kill two birds with one stone: read the results from standard input while printing the results to a file and standard output.
Tee command syntax
The syntax of the tee command is very simple, in the following format:
$tee options file
Here are some options you can use with the tee command:
Linuxmi@linuxmi:~/www.linuxmi.com$ tee-help usage: tee [options]... [file]. Copies standard input to each specified file and displays it to standard output. -a,-- append contents are appended to a given file instead of overwriting-I,-- ignore-interrupts ignores interrupt signals-- p troubleshoots non-pipeline writes-- output-error [= mode] sets the behavior of write errors. See the "mode" section below-- help displays this help and exits-- version displays version information and exits mode to determine the behavior when writing an error to the output: 'warn' diagnoses an error in the output to any file.' warn-nopipe' makes an error in the output to any file except the pipe Diagnose the situation. 'exit', once the output goes wrong Then the default mode of the exit program-p option is "warn-nopipe" if there is an error in the output of the exit-nopipe' and it is not piped. When-- output-error is not given, the default action is to exit immediately when there is an error in writing to the pipe, and diagnose the problem in the event of an error in writing to a non-pipe.
In the syntax of the tee command, a file refers to one or more files.
Let's see how this command is used.
Example 1-basic usage of the tee command
As mentioned earlier, the main function of the tee command is to display the output of the command (stdout) 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 new file named www.linuxmi.com .txt:
Linuxmi@linuxmi:~/www.linuxmi.com$ lsblk | tee www.linuxmi.com.txt
You can use the cat command to check the contents of the www.linuxmi.com.txt file, as follows:
Cat www.linuxmi.com.txt
Example 2-use tee to save command output to multiple files
In addition, you can write the output of the command to several space-delimited files, as shown in the following syntax.
$command | tee file1 file2 file3. . . Linuxmi@linuxmi:~/www.linuxmi.com$ hostnamectl | tee linuxmi.txt linuxmi.com.txt
Similarly, you can use the cat command to confirm that the output in the two files exists, as follows:
Cat linuxmi.txt cat linuxmi.com.txt
Example 3-disable output of tee commands on screen
If you want to hide or disable the tee command from printing on the screen, redirect the output to / dev/null, as shown in the figure:
$command | tee file > / dev/null
For example,
Linuxmi@linuxmi:~/www.linuxmi.com$ df-Th | tee linuxmi.txt > / dev/null linuxmi@linuxmi:~/www.linuxmi.com$ uname-a | tee linuxmi.txt > / dev/null linuxmi@linuxmi:~/www.linuxmi.com$ cat linuxmi.txt Linux linuxmi 5.4.0-48-generic # 52-Ubuntu SMP Thu Sep 10 10:58:49 UTC 2020 x86 "64 GNU/Linux
Example 4-use the tee command to attach output to a file
By default, the tee command overrides the contents of the file (such as example 3). To attach the output and prevent the current content from being erased, use the-an or-append options.
$command | tee-a file
In the second command, as shown in the figure, we append the output of the date command to linuxmi.com.txt, which already contains information about USB devices on the system.
Linuxmi@linuxmi:~/www.linuxmi.com$ lsusb | tee linuxmi.com.txt linuxmi@linuxmi:~/www.linuxmi.com$ date | tee-a linuxmi.com.txt
Example 5-using tee with the sudo command
Suppose, as a sudo user, you want to write on a file owned by the root user. Of course, any promotion requires that the sudo user be called before the command.
To do this, simply add sudo before the tee command, as shown below.
Linuxmi@linuxmi:~/www.linuxmi.com$ echo "192.168.1.100 db-01" | sudo tee-a / etc/hosts
Therefore, tee receives the output of the echo command on the left and promotes it with the sudo command, which eventually appends the output to the file.
Example 6-use the tee command to redirect the output of one command to another
Using the tee command, we can easily redirect the output of one command to another. Here the output of the first command will be used as the input to the second command. Examples are as follows:
Linuxmi@linuxmi:~/www.linuxmi.com$ grep 'root' / etc/passwd | tee / tmp/passwd.tmp | wc-l
Example 7-use the tee command to save changes to a file in the Vim editor
Suppose you are working as a non-root user and are making changes to the files owned by root, but forget to put sudo in front of the command, and now you want to save the changes, as shown in the example:
Linuxmi@linuxmi:~/www.linuxmi.com$ vim / etc/hosts
When you save changes to the file, you will receive a read-only message, / etc/hosts "E212: unable to open and write to the file
Now to save the changes to the / etc/hosts file in the Vim editor, run
W! sudo tee%
It will prompt you for the user's password, and if the user has sudo privileges, the changes will be saved.
Example 8-ignore interrupt signals when using the tee command
Use the "- I" option in the tee command to ignore the interrupt signal (CTRL-C), as shown in the following example:
Linuxmi@linuxmi:~/www.linuxmi.com$ ping-c 5 linuxmi.com | tee-I / tmp/pingtest.tmp
Example 9-tee command usage in shell scripts
The Tee command is also often used in shell scripts, and a common example is listed below:
Linuxmi@linuxmi:~/www.linuxmi.com$ vi linuxmi@linuxmi:~/www.linuxmi.com$ vi print script.shemaking Bingray Bash LOGFILE=/tmp/basic-logs-$ (cat +% 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
This is how to use the tee command in the Linux shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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: 260
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.