Input/output redirect __linux under Linux

Source: Internet
Author: User

Input-output redirection is supported in Linux environments, represented by symbols < and >.
0, 1, and 2 represent standard input, standard output, and standard error information output, which can be used to specify standard inputs or outputs that require redirection, such as 2>lee.dat to output error messages to file Lee.dat.
In the meantime, redirects can be achieved between these three standard input outputs, such as redirecting error messages to standard output, and can be implemented using 2&GT;&AMP;1.
Linux also has a special file/dev/null, it is like a bottomless pit, all redirected to its information will disappear without a trace. This is useful when we can redirect output to/dev/null when we do not need to echo all the information about the program.
If you want normal output and error messages to be displayed, redirect both standard output and standard error to/dev/null, for example:
# ls 1>/dev/null 2>/dev/null
Another approach is to redirect errors to standard output, and then redirect to/dev/null, for example:
# ls >/dev/null 2>&1
Note: The order here cannot be changed, otherwise the desired effect is not achieved, the standard output is redirected to/dev/null first, then the standard error is redirected to standard output, and the standard error is redirected to/dev/null because the standard output has been redirected to/dev/null.
1. Control of standard input
Syntax: Commands < files make the file the input of the command.
For example:
Mail-s "Mail Lee" Lee@lee.com < lee1 the file lee1 as the content of the letter, the subject name is Mail Lee, sent to the recipient.
2. Control of standard output
Syntax: commands > Files
Sends the execution results of the command to the specified file.
For example:
Ls-l > Lee.dat writes the results of the Execute "ls-l" command to the file Lee.dat.
Syntax: Command >! File
Sends the execution result of the command to the specified file, overwriting if the file already exists.
For example:
LS-LG >! Lee.dat writes the results of the LS-LG command to the file Lee.dat.
Syntax: Commands >& files
Writes any information that is generated on the screen to the specified file when the command executes.
For example:
CC lee.c >& Lee.dat writes any information that is generated when the lee.c file is compiled into the file lee.dat.

Syntax: Commands >> files
Attaches the results of the command execution to the specified file.
For example:
Ls-lag >> Lee.dat Appends the results of the Execute "ls-lag" command to the file Lee.dat.

Syntax: Commands >>& files
Attaches any information that is generated on the screen to the specified file when the command executes.
For example:
CC LEE.C >& Lee.dat appends any information generated by the screen to the file Lee.dat when the lee.c file is compiled.

In a character terminal environment, the concept of standard input/standard output is well understood. Input refers to the input of an application or command, whether from keyboard input or from another file; the output refers to some information generated by an application or command; Unlike Windows, there is a concept of standard error output under the Linux system. This concept is mainly for program debugging and system maintenance purposes set, error output from the standard output can let some advanced error information does not interfere with the normal output information, so as to facilitate the use of general users.
In Linux systems: standard input (stdin) defaults to keyboard input, standard output (STDOUT) defaults to screen output, and standard error output (STDERR) defaults to the screen (STD-standard above). When these concepts are used in BASH, the standard output is typically represented as 1, and the standard error output is expressed as 2. Here are some examples of how to use them, especially standard output and standard error output.
Input, output, and standard error outputs are primarily used for I/O redirection, which means changing their default settings. First look at this example:
$ ls > Lee.dat
$ ls-l >> Lee.dat
These two commands redirect the result output of the LS command to the Lee.dat file and append to the Lee.dat file, instead of the output to the screen. ">" is the output (standard output and standard error output) redirection of the representative symbol, two consecutive ">" symbol, that is, ">>" means not to clear the original and append output. Let's look at a slightly more complicated example:
$ find/home-name lee* 2> Err_lee
This command has an extra "2" before the ">" Symbol, and "2>" means redirecting the standard error output. Because some directories under the/home directory cannot be accessed due to permission restrictions, some standard error outputs are stored in the Err_result file. You can imagine what the find/home-name lee* 2>>err_result command would produce.
If you execute Find/home-name lee* > All_lee directly, the result is that only standard output is stored in the All_lee file, so what if you want the standard error output to be stored in the same file as the standard input? Look at the following example:
$ find/home-name lee* > All_lee 2>& 1
The above example will first redirect the standard error output to the standard output, and then redirect the standard output to the All_lee file. So we can store all the output in a file. To achieve the above function, there is also a simple way to read as follows:
$ find/home-name lee* >& All_lee
If the error message is not important, the following command allows you to avoid the distractions of many useless error messages:
$ find/home-name lee* 2>/dev/null
After the students go back can also test the following several redirects to see what results, why.
$ find/home-name lee* > All_lee 1>& 2
$ find/home-name lee* 2> All_lee 1>& 2
$ find/home-name lee* 2>& 1 > All_lee
A very useful redirection operator outside is "-", see the following example:
$ (Cd/source/lee && tar CF-.) | (Cd/dest/lee && tar xvfp-)
This command means that all the files in the/source/lee directory are compressed and decompressed, quickly moving all the way to the/dest/lee directory, and this command will show a particular advantage when/source/lee and/dest/lee are not under the same file system.
Here are some of the less common uses:
n<&-indicates that n-number input is closed
<&-means turn off standard input (keyboard)
n>&-indicates that n-number output is closed
>&-indicates that standard output is closed
And give you a very useful command:
grep Lee ' Find. 2>/dev/null|grep sh$ '
Don't see the time call me.

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.