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 under linux

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

Share

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

This article introduces the relevant knowledge of "how to use the tee command under linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Tee

Function description: read the standard input data and output its contents into a file.

Grammar: tee [- ai] [--help] [--version] [file.]

Note: the tee instruction reads the data from the standard input device, outputs its contents to the standard output device, and saves it as a file. We can use tee to save the data imported by the pipeline into a file, or even save several files at a time.

Parameter:-an appends to the existing file instead of overwriting it. If the file name given to the tee instruction already exists, the default will overwrite the contents of the file. When this parameter is added, the data is added at the end of the file content without deleting the previous content.

-I ignore the interrupt signal

-- help online help

-- version displays version information

Example:

List the contents of the text file slayers.story and make 3 copies with the file names ss-copy1, ss-copy2, ss-copy3:

$cat slayers.story | tee ss-copy1 ss-copy2 ss-copy3

Tee [- ai] [--help] [--version] [file.]

[function]

Tee takes standard input as input, standard output and file as output.

[example]

Tee file / / overwrite

Tee-a file / / append

Tee-/ / output to standard output twice

Tee-- / / output to standard output three times

Tee file1 file2-/ / output to standard output twice and write to those two files

Ls | tee file

Another: standard error is also read by tee

Ls "*" 2 > & 1 | tee ls.txt

* generate a file with tee that contains what you typed:

The code is as follows:

$tee testfile

In this way, you will be prompted to use standard input, and then enter will write your input to testfile and output to standard output, if you end the input with [Ctrl] d ([Ctrl] c is also fine). If the original testfile has content, it will be overwritten.

* append the content to the end line of the file:

The code is as follows:

$tee-a testfile

The result is similar, but if the original testfile has content, it will not be overwritten but will be appended.

* generate a file and do not accept the interrupt signal when typing:

The code is as follows:

$tee-I testfile

The result is the same as testfile, but does not receive interrupt signals, but can only end with [Ctrl] d, not [Ctrl] c.

* execute ls to list the directory files and save the output to the file test:

The code is as follows:

$ls | tee test

In this way, the ls command is executed as usual and the file name of the current directory is output to standard output. In addition, due to the tee command, a test file is generated, and the content of this test file is the same as that of standard output.

[description]

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. It can be used when you want to see standard output and save standard output to a file.

Parameters:

-an or-- append appends to the existing file instead of overwriting it.

-iMuri or-- ignore-interrupts ignore the interrupt signal.

-- help online help.

-- version displays version information.

Common parameters

Format: tee

Output only to standard output, because there is no file specified.

Format: tee file

When exporting to standard output, save it to the file file. If the file does not exist, create it; if it already exists, overwrite it. (If a file being written to does not already exist, it is created. If a file being written to already exists, the data it previously

Contained is overwritten unless the `- a' option is used.)

Format: tee-a file

When outputting to standard output, append it to the file file. If the file does not exist, create it; if it already exists, append the content at the end instead of overwriting it.

Format: tee-

Output to standard output twice. (A FILE of `- 'causes` tee' to send another copy of input to standard output, but this is typically not that useful as the copies are interleaved.)

Format: tee file1 file2-

Output to standard output twice and save to file1 and file2 at the same time.

Use examples to supplement:

Example 1 comparison of tee command and redirection

[root@web ~] # seq 5 > 1.txt

[root@web ~] # cat 1.txt

one

two

three

four

five

[root@web ~] # cat 1.txt > 2.txt

[root@web ~] # cat 1.txt | tee 3.txt

one

two

three

four

five

[root@web ~] # cat 2.txt

one

two

three

four

five

[root@web ~] # cat 3.txt

one

two

three

four

five

[root@web ~] # cat 1.txt > > 2.txt

[root@web ~] # cat 1.txt | tee-a 3.txt

one

two

three

four

five

[root@web ~] # cat 2.txt

one

two

three

four

five

one

two

three

four

five

[root@web ~] # cat 3.txt

one

two

three

four

five

one

two

three

four

five

[root@web ~] #

Example 2 use the tee command to repeat the output string

[root@web ~] # echo 12345 | tee

12345

[root@web ~] # echo 12345 | tee-

12345

12345

[root@web ~] # echo 12345 | tee--

12345

12345

12345

[root@web ~] # echo 12345 | tee-

12345

12345

12345

12345

[root@web ~] # echo 12345 | tee-

12345

12345

12345

12345

12345

[root@web ~] #

[root@web ~] # echo-n 12345 | tee

12345 [root@web ~] # echo-n 12345 | tee-

1234512345 [root@web ~] # echo-n 12345 | tee--

123451234512345 [root@web ~] # echo-n 12345 | tee-

1234512345123451234512345 [root@web ~] # echo-n 12345 | tee-

123451234512345123451234512345 [root@web ~] #

Example 3 use the tee command to save the standard error output to a file

[root@web ~] # ls "*"

Ls: *: there is no such file or directory

[root@web ~] # ls "*" | tee-

Ls: *: there is no such file or directory

[root@web ~] # ls "*" | tee ls.txt

Ls: *: there is no such file or directory

[root@web ~] # cat ls.txt

[root@web ~] # ls "*" 2 > & 1 | tee ls.txt

Ls: *: there is no such file or directory

[root@web ~] # cat ls.txt

Ls: *: there is no such file or directory

[root@web ~] #

This is the end of the content of "how to use the tee command under linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 232

*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