"Linux" Data flow redirection (previous)

Source: Internet
Author: User

  Data Flow re-orientation (redirect) in the literal sense, it seems that the "data is transmitted to him elsewhere" appearance? That's right. Data flow redirection is the data that should appear on the screen after a command is run and transmitted to other places, such as files or devices (such as printers)! This thing is important under the Linux text mode! Especially if we want to save some data, it's even more useful!

  1) What is Data flow redirection?

What is data flow redirection? This has to be done by the result of the command! In general, if you are going to run a command, usually he will be like this:

When we run a command, this command may be read into the data by the file, processed, and then output to the screen. The standard output and standard error output, respectively, represent both the "normal" and "Std" outputs, which are output to the screen by default. So what is standard output and standard error output?

 2) Standard output and standard error output

To put it simply, standard output refers to "the correct information returned by the command to run," and the standard error output can be understood as "error message after command failed." For a simple example, our system defaults to/etc/crontab but no/etc/vbirdsay, and when the "Cat/etc/crontab/etc/vbirdsay" command is issued, CAT will:

    • Standard output: After reading the/etc/crontab, the contents of the file are displayed on the screen;

    • Standard error Output: Error message displayed on screen because/etc/vbirdsay cannot be found

Whether the right or wrong data is the default output to the screen, the screen is of course chaotic! Could it be possible to separate the two strands of data through some mechanism? Of course you can! That's the function of data flow redirection! Data flow redirection allows you to transfer standard output (STDOUT) to a different file or device, respectively, to other files or devices, and the special characters to be transferred, respectively, as follows:

    1. Standard input (stdin): code 0, using < or <<;

    2. Standard output (STDOUT): Code 1, using > or >>;

    3. Standard error Output (STDERR): Code 2, using 2> or 2>>;

To understand stdout and stderr, let's start with an example exercise:

Example one: Observe your system root directory (/~]# ll/  <==~]# ll/> ~/rootfile <==~]# ll  ~/rootfile <= = A new file has been created!  11089 Feb  6:xx /root/rootfile

It's weird! How can the screen have no data at all? This is because the original "LL/" displayed data has been redirected to the ~/rootfile file! The name of the ~/rootfile can be taken as you please. If you give "cat ~/rootfile" then you can see the data that should be on the screen originally. If I release again: "Ll/home > ~/rootfile", what does the content of that ~/rootfile file become? He will become "only ll/home data"! Hey! The original "LL/" data is gone? Yes! Because the file was created in the following way:

    1. If the file (in this case, ~/rootfile) does not exist, the system will automatically create it, but
    2. When this file is present, the system will clear the contents of the file before writing the data!
    3. That is, if the > output to an existing file, the file will be overwritten!

So what if I want to accumulate the data and don't want to delete the old data? It's good to use two greater than (>>) symbols. In the example above, you should change to " ll/>> ~/rootfile ". Thus, when (1) ~/rootfile does not exist the system will actively create this file, (2) If the file already exists, then the data will be added to the bottom of the file!

This is the correct data for standard output, what if it is the error data of standard error output? Then go through 2> and 2>>! The same is the feature that covers (2>) and accumulates (2>>)! We just talked about the stdout code is 1 and the stderr code is 2, so this 2> is easy to understand, and if there is only >, then the default code is 1! Other words:

    • 1>: The "correct data" is output to the specified file or device in a covered manner;
    • 1>>: The "correct data" is output to the specified file or device in an additive manner;
    • 2>: The "wrong data" is output to the specified file or device in an overridden manner;
    • 2>>: The "wrong data" is output to the specified file or device in an additive manner;

Note that there are no spaces between "1>>" and "2>>"! Ok! After some concepts let's talk about how this guy is going to apply! When you run the Find command as a general identity, some error messages may be generated due to permissions issues. For example, when you run Find/-name testing , you may have information similar to "Find:/root:permission denied". For example the following example:

Example two: Using General identity account Search/su -dmtsai  <==find /home-name. BASHRC <== Identity is Dmtsai Oh!  Find:/home/lost+found:permission denied  <== standard errorfind:/home/alex: Permission denied        <== standard errorfind:/home/arod:permission denied        <== Standard error/HOME/DMTSAI/.BASHRC                       <== standard output

due to the existence of the account that we created before/home, the family directory of those accounts is of course not accessible to you! So there will be errors and the correct data. OK, so what if I want to output the data to the list file? What happens when you run the Find/home-name bashrc > list? Hehe, you will find that the list contains just the "correct" output data, as for the screen will still have the wrong information appear ! Racking What do I need to do if I want to put the correct and wrong data into separate files?

Find 2> List_error

Note that there is no information on the screen at this time! Because of the results that have just run, the few lines of Permission error messages will go to the List_error file, and the correct output data will be stored in the List_right file! Do you understand that? If it's a bit confusing, take a break and take a look!

  To be continued~

  References: Http://vbird.dic.ksu.edu.tw/linux_basic/0320bash.php#redirect

&NBSP;

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.