I/O redirection in Linux

Source: Internet
Author: User
Tags touch command

 

First, let's talk about what is I/O redirection. The so-called I/O redirection is simply a process, which captures a file, or commands, programs, scripts, even the code block output in the script sends the captured output to another file, command, program, or script as the input.

When it comes to I/O redirection, it involves the concept of a file identifier (file descriptor). in Linux, the system specifies a file identifier for each opened file so that the system can track the file, this is similar to the file handle in C programming. The file identifier is a number. Different numbers indicate different meanings. By default, the system occupies three, they are 0 standard input (stdin), 1 standard output (stdout), 2 standard error (stderr), and 3-9 are reserved identifiers. You can specify these identifiers as standard input, the output or error is used as a temporary connection. This usually solves many complicated redirect requests.

Standard Input usually refers to the keyboard input
Standard output usually refers to the output of the monitor.
Standard errors are usually directed to the display.

Take the following example to describe their relationship

#ls /dev

This command lists all the files in the/dev directory and outputs the results on the screen.

Here/Dev is the standard input (input from the keyboard) of the LS command, and the result printed on the screen is the standard output (content in the/dev directory)

Return to the title. redirection is to change the standard input or output to another method. See the following example.
Or equivalent

# Ls/dev 1> filename # Note: "1" and ">" have no spaces in the middle

The above command will redirect the standard output of the command to a file filename instead of display on the screen. If the file identifier is not specified, the default value is 1, so 1 can be omitted.

If you change ">" to ">" in the preceding example, the output is appended to the end of the filename file. If the file does not exist, it is created. As follows:

#ls /dev >>filename

You can also redirect standard errors to files.

#ls -qw  /dev  2>filename

Apparently-QW is an error parameter and usually reports an error message on the monitor. However, because stderr is redirected to the filename file again, the monitor has no error message, the information is written to the file.

The following command directs both standard output and errors to the file

#ls /dev &>filename

"&" Indicates the standard output and standard error. Both the normal output and the error message are written to filename.

Redefinition of standard input, output, and incorrect file identifiers

You can use the I> & J command to redirect the File Identifier I to J. You can think of "&" as "Get address"

See the following example.

#exec 5>&1

Object Identifier 5 is directed to the standard output. This command is usually used to temporarily Save the standard input.

The standard input can also be reoriented. refer to the following example.

# Grep search-word <FILENAME or grep search-word 0 <FILENAME

In general, the grep command searches for strings in a given file. The above command uses the file filename as the standard input for the grep command, rather than input from the keyboard.

As mentioned above, the system specifies a file identifier for each opened file so that the system can track the file. What is the default file identifier? The answer is 0, that is, standard input, or input from the keyboard. Of course, this file identifier can also be specified by yourself. Please refer to the example below

# Echo 123456789> filename write the string to the file filename # exec 3 <> filename open the file filename, and specify the File Identifier as 3 # Read-N 4 <& 3 read 4 characters from the file. The handle has reached the end of the fourth character # echo-N.> & 3 Write a dot at 5th characters, covering 5th characters.-N indicates no line break # exec 3> &-disable file identifier 3

Now the result of the cat filename file is 1234.6789

Command j <> filename indicates opening the file and specifying the file ID as J
"&-" Indicates the identifier of the disabled file.

For more information about how to disable a file identifier, see the following.

N <&-Disable the Input File Identifier N0 <&-or <&-Disable stdinn> &-Disable the output file identifier N1> &-or> &-Disable the stdout

There are other commands as follows:

2.:> filename or> filename

Indicates that the file filename is set to null, that is, the file content is cleared. If the file does not exist, an empty file is created (equivalent to the touch command): indicates an empty output, the only difference between the two commands is that> filename does not work properly in all shells.

 

Finally, let's take a look at some common redirection commands (note >>and <):

CMD> file: redirects stdout to the file;
CMD> file: redirects stdout to the file (append );
CMD 1> Fiel redirects stdout to the file;
CMD> file 2> & 1 redirects stdout and stderr together to the file;
CMD 2> file: redirects stderr to the file;
CMD 2> file redirects stderr to the file (append );
CMD> file 2> & 1 redirect stderr and stderr together to the file (append );
CMD <File> the file2 cmd command uses the file as stdin and the file2 file as stdout;
Cat <> file: open the file in read/write mode;
CMD <file cmd command uses file as stdin;
CMD <delimiter here document, read from stdin until the delimiter Delimiter is encountered.

 

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.