Linux basics: file descriptors and redirection

About file descriptors: file descriptors in the form of a non-negative integer, each with an open file descriptor will correspond to the kernel using the file descriptor to access a file, there is most well-known file descriptor stdin (standard input ), stdout (standard output), stderr (standard error), three systems were previously reserved file descriptors 0, 1 for them, we can also give us the file descriptor specified by special order.

Redirection means: Linux in, IO is redirected to the contents of a file descriptor to another file descriptor specified, usually associated with a file descriptor.

The most commonly used content standard we can redirect the output to the specified file. It can be sent to the output file using the redirection operator (> and >>), instead of the terminal. > And >> slightly different, although both can be redirected to a text file, but the former will clear the file, and then write the content, which will append to the end of an existing file, by default, heavy orientation for the operator is the standard output, so> is equivalent to 1>, equivalent to a similar >> >> 1.

1: standard output and standard error redirection

2: The <operator read the file stdin

3: Custom file descriptor

4: tee command

1: for example, the echo command is specified string to standard output, then we can combine the echo command will send the specified string to the specified file:

 This is the content descriptor stdout (1) is redirected to a file by redirecting test.txt operator.

When handling errors, the output from stderr was poured into the file / dev / null in, / dev / null is a special device file, he will discard any data received. Also known as a black hole, because it enters the data in it will be gone forever. The same we can also redirect the standard error output to a file, for preservation.

Next, this command will stderr text printed to the screen, instead of redirecting to the text, because by default for the redirection operator is the standard output, stdout and there is no output, but stderr output

Before stderr redirection operator specifies file descriptor, you can redirect the stderr

2: command read from stdin input to receive data in a variety of ways:

Means less than symbols, we can read the image data file used as stdin: cmd <file; (cmd command requires data file)

When there are used pipe, often before a standard output redirection command to the standard input of the next command, cmd1 | cmd2.

3: exec command to create a file descriptor (in numeric symbol & behind a file descriptor) for reading:

Create a file descriptor for writing:

4: standard output file can be redirected to either, other programs can also pass through the conduit, but not both simultaneously, can achieve this operation command tee, tee command read from stdin, to redirect data to a file, and also redirect data to the standard output (stdout) in: cmd | tee file1 file2 | othercmd

For example ls command lists the cat command to view the contents of the file and the results of both the standard output:

-a specify additional way to save a file

This knowledge need to understand the exec command, as well as file open mode, a preliminary understanding of the standard input and output.

Guess you like

Origin www.linuxidc.com/Linux/2019-10/161162.htm