Pipelines and redirection in Linux

Source: Internet
Author: User
Pipelines and redirection in Linux I. redirection output can be redirected to a file through the operator & gt; standard output 1 and standard error output 2. for example, export ls-l&gt;lsoutput.txt;command redirects the standard output to the file lsoutput.txt .... Pipelines and redirection in Linux I. redirection output can be redirected to a file through the operator> standard output 1 and standard error output 2. for example, the command # ls-l> lsoutput.txt redirects the standard output to the file lsoutput.txt. Of course, you can also redirect file descriptors other than standard file descriptors, but this situation is rare. If the lsoutput.txt file already exists, its content will be overwritten. if you want to append the output content to a file, you can use the> operator to redirect the output, for example: # ls-l> lsoutput.txt the command above attaches the output to the end of the specified file. Sometimes, the system uses the redirection output to redirect command errors to a file to shield screen output. The following command redirects the standard output and error output to different files: # kill-HUP 1234> killout.txt 2> killerr.txt if you want to redirect both sets of output to one file, you can use the & operator to combine two outputs, for example: # kill-l 1234> killouterr.txt 2> & 1 This command redirects both standard output and standard error output to the same file. Please refer to the operating sequence. this directive indicates that the standard output is redirected to the killouterr.txt file, and the standard error output is redirected to the same place as the standard output ." If the order is incorrect, the redirection will not be executed as expected. There is also a good way to discard the standard output or standard error output, such: # kill-l 1234>/dev/null 2> & 1 recycle bin/dev/null is like a black hole. after entering anything, the meat steamed stuffed bun can be beaten back without returning it. 2. we can also redirect standard input to the redirection input, for example: # more <killout.txt. However, the above Command seems meaningless, because more can also accept the file name as a parameter, I just need to brush my sense of presence. 3. pipeline we can use the pipeline operator | to connect processes. Linux, unlike MS-DOS, processes connected through pipelines in Linux can run simultaneously and automatically coordinate as data streams pass between them. The following uses the sort command to sort the output of the ps command to describe the significance of the MPs queue. if you do not need a MPs queue, you must perform the following steps: # ps> psout.txt # sort psout.txt> pssort. A more sophisticated out solution is to connect processes using pipelines, such as: # ps | sort> pssort. if you want to display output results by page on the screen, we can connect the third process more and place them on the same command line. this can be done as follows: # ps | sort | there is no limit on the number of processes allowed to connect more. Suppose we want to see the names of all processes running in the system, but not the shell itself, so we can: # ps-xo comm | sort | uniq | grep-v sh | the more command first sorts the output of the ps command in alphabetical order, and then uses the uniq command to remove duplicate content, run the grep-v sh command to delete the process named sh and display the result on the screen. As you can see, this is a more sophisticated solution than using a series of separated commands and each command carries its own temporary file. Note that if you have a series of commands to execute, the corresponding output files are created or written at the same time as these commands are created, therefore, do not reuse the same file name in the command stream. If you try to execute the following command: # cat mydata.txt | sort | uniq |> mydata.txt, you will eventually get an empty file, because the contents of this file have been overwritten before reading the file mydata.txt.
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.