Tlcl-redirection (redirected)

Source: Internet
Author: User

Redirect (redirection)

So-called redirection can be simply understood to re-specify the direction of the input and output, since it is the direction of re-enactment of the input and output, then the original input and output direction is how?

The computer's I/O has standard input, standard output, output error, the default standard input is connected to the keyboard, the standard output is connected to the screen, as for the output error is also the default connection to the screen.

And I/O redirection allows us to change these default connections, read from a file, write output to a file, and so on, all redirected.


1. REDIRECT Standard input

The LS command connects the output to the screen by default, and we can change the output by the > redirect operator

ls > Ls-output.txt

So the LS will output the content to output to the Ls-output.txt file

But the next time you redirect to this file, the contents of the file will be overwritten, and of course we can solve this problem by using the >> redirect operator.

LS >> ls-out.txt

The content of the LS output will be added at the end of the original file

Note: redirected content is wrapped automatically


2. REDIRECT Standard input

Before introducing the standard input redirection, review the Cat command first.

Before, just know that cat used to display the contents of a shorter file, but did not find it a very useful place, cat can be used to open multiple files display, we sometimes download a package, but the text will be divided into a lot of parts, text1,text2 ..., At this time a view is very laborious, we would like to have a thing to connect the text together to form a file it? Tell you, cat can do it.

Cat Text1 Text2 ...

The cat connection file is also the default output to the screen, how to redirect the output to a file, of course, need to use the > operator

Cat Text1 Text2 ... > Text

So all the content is in the text file.

Let's think about it, what if cat doesn't have parameters?

Cat

At this point, cat waits for standard input, the default standard input is the keyboard, a few characters are struck, and the ctrl-d (end of file) is pressed, and the characters you tap are printed on the screen.

It is easy to understand that the standard input connects the keyboard to the standard output connection screen.

So how do you change the connection of the standard input? This is where the < operator is needed.

Cat < filename

This way, cat will not wait for the keyboard standard input, because cat has read the content from filename, of course, because the standard output does not change, filename content will be displayed on the screen

Cat <filename >filename1

So the contents of the filename will be written into the filename1, as if there is no meaning.

Did we find out if we could think of cat filename as omitting <?

3. REDIRECT Output error

Since there are < operators, there are also > operators, is it not the redirect output error with the <>

Of course not

In fact, when the program is written to the file stream, the "first three streams do" number, called the file descriptor (Files descriptor),0, 1, 2 for standard input, standard output, output error , so in the use of > operator is we can explicitly " Specifies the flow of the output.

LSS 2> Error-output.txt

The command error message is then written to the Error-output.txt file (the LSS command cannot be found), and you should never expect the LSS 1>l-output.txt file because 1 specifies "standard output stream".

What if the standard output and the output errors are written to the file?

LS >ls-output.txt 2>&1

Be careful not to do that

LS 2>&1 >ls-output.txt

Because the "2 stream" output is redirected to "1 stream", "1 stream" default standard output is connected to the screen ah, this is if an error occurs, the standard error will be displayed on the screen.

In fact, there is a simpler way:

LS &> ls-output.txt

This makes it a lot easier.

4. Introduction of several simple commands and operators

Pipe Operators |

| You can use the standard output of one command as a standard input for another command

ls | Less

The standard output of LS is piped as the standard input for less


Filter filters

Sort sorts the rows of the output content

Cat Test | Sort


WC Print line, Word and number of bytes

WC filename


grep Print Matching lines

grep searches for matching lines from text and prints them out

grep Hello Test

Search for a line containing hello from the test file and print it out

-I option, ignoring the case of the search

-v option to output rows that do not match


Tee

accepts standard inputs while outputting to standard output and files

Who |tee Who-output.txt

Display the WHO content on the screen (standard output) and output to the Who-output.txt file

Tlcl-redirection (redirected)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.