"Linux Command Line": 1-6: Redirection and pipe (very exciting)

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

https://www.toutiao.com/i6937081007581430303/?tt_from=weixin&utm_campaign=client_share&wxshare_count=1&timestamp=1615424046&app=news_article&utm_source=weixin&utm_medium=toutiao_ios&use_new_style=1&req_id=20210311085406010212086222172135EE&share_token=70404BA8-D8AB-45B2-B6A9-227D45EF42A5&group_id=6937081007581430303

One: standard input, standard output and standard error

For a command or program, after pressing the Enter key, either the result of the program operation or the status and error information will be displayed.

Take ls as an example. When the ls command is pressed, it will send its running result to a  special file called standard output (stdout) , and its status information will be sent to a  file called  standard error (stderr) in. Both standard output and standard error will be connected to the screen, and then output, it will not be saved in the disk.
We all know that commands are input to the computer through the keyboard. This keyboard is called standard input (stdin)

By default, standard input and standard output are performed according to this logic, and the I/O redirection function can change the sending purpose of the output content (that is, not let you send it to the screen), and you can also change the input content. The origin of (that is, it can even come from a file)

In short, usually the output content is on the screen, and the input content comes from the keyboard, but redirection can change this logic

(1): Standard output redirection

The standard output redirection symbol is >'or >>, which means that the content on the left is redirected to the right.

A:> Redirect

As follows, use the ls command to redirect the output of the ls command to a.txt

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


The instruction just now is correct, because the directory listed in the ls command exists, but now we change it. We redirect a directory that does not exist at all

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


The result is expected, it does not exist, but there is a very strange question, since this directory does not exist, then why the final a.txt is still generated?

Then we use the long list to display this file

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


An even stranger thing happened, the size of this file turned out to be 0. Using redirection ">", when redirecting, the target file will be rewritten from the beginning of the file, but above we ls a directory that does not exist at all, so when redirecting to rewrite this file, an error occurs The operation is stopped, so the content of the file is deleted, but the file is not deleted

  • Therefore, this gives us an inspiration, we can use this method when we need to create a new empty file or delete the content of the file.

B: >> Redirect

The above> redirection can only be rewritten from the head of the file, which sometimes causes the file content to be deleted, and using >> redirection can add output content from the end of the file

In order to verify this, we first redirect the correct content three times with the previous> redirect

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


It can be found that even if it is redirected correctly three times, the final file size can only be 54 bytes

But in the same way using >> to complete, it still redirects correctly three times, and you can find that the size has tripled, which is 162 bytes

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 

  • From a certain perspective, you can understand> redirection as an overriding redirection, and >> resetting as an additional redirection

(2): Standard error redirection

Earlier, we found a strange place when we deliberately wrongly redirected

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


Why is this error message being output to the screen, shouldn't it be added to the file as a log type of information?

In fact, this question can also be answered earlier. The ls command will not send its error information to the standard output file, but redirects it to the standard error file. Here we only did one thing that is to redirect the standard output. , So naturally it is output to the screen

When rushing to standard error is different from the previous one. To put it simply: add the corresponding index when redirecting, 0 means standard input, 1 means standard output, 2 means standard error, so you can do this when you want to redirect standard error

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 

(3): Redirect standard output and standard error to the same file

Under normal circumstances, we need to redirect standard error and standard output at the same time when redirecting (after all, it is log information)
just use &> to redirect standard error and standard output at the same time

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 

(4): Standard input redirection

Let me introduce the cat command first, and there will be better standard input commands in the future, because the cat command is actually very vague, and sometimes it is not detailed, but there is a function that must be remembered. For files whose functions are not too long, you can use It view

The exact point of the cai command will be the files used to merge. For example, downloading a movie on the Internet is not to download the movie all at once, but to download it in sections. These files may be smaller than movie.avi1, movie.av2, movie.av3········ · If you use the cat command, you can use wildcards to merge all these files at once

cat movie.avi* >movie.avi

The cat above has parameters. If you only enter a cat command here, you will find that the cursor flickers and is waiting for my input

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


Just enter some text at this time

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


Then press ctrl+D to inform that cat has reached the end of the file

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


Due to the lack of a file name, cat will copy the standard input content to the standard output file (the standard output file at this time is the screen), so you will see duplicates.

Now we add the file name, and then use the output redirection, so we have made the world's simplest word processor

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


Use cat again to view the file (here can explain why cat has the function of viewing the content of the file, it will copy the file to the standard output)

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


By now we know that cat defaults to the standard input source keyboard, so if I use <here and the file name on the right, then the standard input source becomes the file.

Two: pipeline

From the narrative in the first part, we can know that the command gets data from the standard input and then sends the data to the standard output. This process is actually two processes , but why does it feel that it is instantaneous when it is executed? This actually makes use of pipelines.

Use the pipe operator "|" to send the standard output of one command to the standard input of another command  Command 1 | Command 2

(1) Less command

The less command can accept standard input, and the less command can be used to display the input of any command in pages. This command can display the input of any command in pages and send the result to standard output (screen)

Type ls -l /usr/bin | less as follows

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


You can understand the above as this: ls -l /usr/bin> test.txt, then less test.txt

(2) Filter

The pipeline can complete complex operations. The content on the left side of the pipeline is sent to the pipeline, and then the operation is performed on the right side. After the operation on the right side is completed, it is passed to the more right side, which is a bit like filtering, so it is called a filter.

The following ls /usr/bin | sort | less means sending the contents of /usr/bin to the pipeline, then sort processes the contents of the pipeline, and then hands it to less, and then less sends the contents to the screen, so what you see will be a Sorted files

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 

(3) uniq-remove duplicate lines

The uniq command can remove some duplicate lines. For example, I deliberately set many lines in the following file

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


First, use the cat command to send it to the screen. At this time, the content will be sent to the pipeline as standard input, and then uniq will process the pipeline content, and then give it to less for viewing.

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 

  • Note that uniq -d means only view duplicate lines

(4) wc-print lines, words and bytes

When wc does not have any file parameters, the keyboard is used as the standard input source by default.
The following is the use of wc and pipeline

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 

(5)grep

The function of grep is very powerful. You can simply understand it as grabbing certain characters . Grep can not only match simple characters, but with regular expressions, you will achieve unexpected results, but this section just shows its basics. Usage is
as follows, with the pipeline, I can list the text that contains the zip line in the file

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


If you add option -n to the input, you can print out the line number of the text

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


There are other common options, readers can try

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 

(6) head/tail- only look at the beginning or end

For some files, you only need to view the first few lines or the last few lines. Here, the head and tail commands can help you complete. By default , head and tail will output the first 10 and last 10 lines of the file.

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


If you need to change the number of lines, you only need to add -n at the end, where n represents the number of lines

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 


Among them, tail has a -f option which is very useful, you can view the progress status of the log file being written .
For example, the message file under /var/log contains security information, it will be updated frequently, so you can use tail -f to monitor (may need to increase the user level to operate), such as sudo tail -f /var/log/messages

(7) tee- read data from stdin and output to stdout (no file parameter) or file

When we used the pipeline earlier, the commands behind the pipeline can directly manipulate the contents of the pipeline, but now I need to save the contents of the pipeline to a file (if no parameters are added after the tee, then the standard output file will be sent by default. (That is, the screen) What should I do? It can be done using the tee command

"Linux Command Line": 1-6: Redirection and pipe (very exciting)

 

Favorites

Report

Guess you like

Origin blog.csdn.net/z136370204/article/details/114651768