Data Stream redirection in Linux

Source: Internet
Author: User
  • >: Output "correct data" to the specified file or device in a overwriting method.
  • 1>: overwrite the correct data to the specified file or device.
  • 1>: outputs "correct data" to a specified file or device in a cumulative manner.
  • 2>: overwrite the "wrong data" to the specified file or device.
  • 2>: outputs "wrong data" to the specified file or device in an accumulative way.
  • <: Replace the data originally required by the keyboard with the file content.
  • <: Indicates the ending input character 』

For example, "I want to use Cat to directly output the input information to the catfile, and when the keyboard inputs EOF, this input ends", then I can do this:

[Root @ WWW ~] # Cat> catfile <"EOF"> This is a test.> OK now stop> EOF <= enter this keyword. It will end immediately without entering [CTRL] + d [root @ WWW ~]. # Cat catfilethis is a test. OK now stop

Here, EOF can be replaced by any other string. There is also a "<-", mainly to ignore one or more tabs before the statement in the output. Let's look at the example below:

#!/bin/bashcat << EOF123456EOF

Output:

123456 if changed:
#!/bin/bashcat <<- EOF123456EOF

Output:

123456

  • 2> & 1: write all the correct and wrong data to the specified file or device.
For example:
yan@yan-vm:~$ ll /root/ /home/ > result 2>& 1yan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/ls: cannot open directory /root/: Permission denied

User Yan cannot access/root, So permission denied is the error data, and others are the correct data.

The following describes several usage examples. You can guess what the running result is:
ll /root/ /home/ > result 1>&2
ll /root/ /home/ 2> result 1>&2
ll /root/ /home/ 2>&1 > result 

Result:

yan@yan-vm:~$ ll /root/ /home/ > result 1>&2/home/:total 16drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/ls: cannot open directory /root/: Permission deniedyan@yan-vm:~$ cat resultyan@yan-vm:~$

Analysis: the correct data is merged into the wrong data, and the correct data is written to the result. However, the correct data is empty at this time, so it is blank in the result file,

Display the wrong data (including the correct data) to the screen.
yan@yan-vm:~$ ll /root/ /home/ 2> result 1>&2yan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/ls: cannot open directory /root/: Permission denied

Analysis: the correct data is merged into the wrong data, and the wrong data is written to the result. At this time, the wrong data contains the correct data, so the result contains the correct and wrong data.

Write the correct data to the screen, but the correct data is empty, so no information is displayed on the screen.
yan@yan-vm:~$ ll /root/ /home/ 2>&1 > resultls: cannot open directory /root/: Permission deniedyan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/yan@yan-vm:~$

Analysis: this is not the case, SO 2> & 1 is ignored.

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.