Article Title: Tips for redirecting command lines in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
1. Standard input control
Syntax: The command <file uses the file as the command input.
For example:
Mail-s "mail test" wesongzhou@hotmail.com <file1 file file1 as the content of the letter, the main
The title is mail test and sent to the recipient.
2. Standard Output Control
Syntax: Command> file: send the command execution result to the specified file.
For example:
Ls-l> list: Write the result of executing the "ls-l" command to the file list.
Syntax: Command>! The file sends the command execution result to the specified file. If the file already exists, it overwrites it.
For example:
Ls-lg>! List overwrites the result of executing the "ls-lg" command to the file list.
Syntax: Command> & file: write any information generated on the screen during command execution to the specified file.
For example:
Cc file1.c> & error writes any information generated when the file1.c file is compiled to the file error.
Syntax: Command> file: attaches the command execution result to the specified file.
For example:
Ls-lag> list: attaches the result of executing the "ls-lag" command to the file list.
Syntax: Command> & file: attaches any information generated on the screen during command execution to the specified file.
For example:
Cc file2.c> & error: attaches any information generated on the screen when the file2.c file is compiled to the file error.
Input, output, and error output
In the character terminal environment, the concept of standard input/standard output is well understood. Input refers to the input of an application or command, whether on the keyboard or from another file; output refers to the information generated by the application or command; different from Windows systems, there is also a standard error output concept in Linux, which is mainly set for program debugging and system maintenance purposes, the error output is separated from the standard output, so that some advanced error messages do not interfere with normal output information, so as to facilitate the use of general users.
In Linux: The standard input (stdin) is the keyboard input by default, the standard output (stdout) is the screen output by default, and the standard error output (stderr) by default, it is also output to the screen (the above std indicates standard ). When using these concepts in BASH, the standard output is generally expressed as 1, and the standard error output is expressed as 2. The following example shows how to use them, especially the standard output and standard error output.
Input, output, and standard error output are mainly used for I/O redirection, that is, they need to change their default settings. Let's look at this example first:
$ Ls> ls_result
$ Ls-l> ls_result
The preceding two commands respectively redirect the ls command output to the ls_result file and append it to the ls_result file, instead of the output to the screen. ">" Indicates the redirection of the output (standard output and standard error output). Two consecutive ">" symbols, that is, ">" indicates that the output is appended without clearing the original one. Next, let's look at a slightly complex example:
$ Find/home-name lost * 2> err_result
This command adds a "2" and "2>" before the ">" symbol to redirect the standard error output. Some directories in the/home directory cannot be accessed due to permission restrictions. Therefore, some standard error outputs are stored in the err_result file. You can imagine what results will be produced by the find/home-name lost * 2> err_result command?
If you directly execute find/home-name lost *> all_result, only the standard output is saved to the all_result file, what should I do if I want to save the standard error output to a file like the standard input? Let's look at the example below:
$ Find/home-name lost *> all_result 2> & 1
In the above example, we will first redirect the standard error output to the standard output, and then redirect the standard output to the all_result file. In this way, all output files can be stored in files. To implement the above functions, there is also a simple Syntax:
$ Find/home-name lost *> & all_result
If the error information is not important, run the following command to avoid interference with useless error information:
$ Find/home-name lost * 2>/dev/null
After returning, you can try the following redirection methods to see what the results will be. Why?
$ Find/home-name lost *> all_result 1> & 2
$ Find/home-name lost * 2> all_result 1> & 2
$ Find/home-name lost * 2> & 1> all_result
Another very useful redirection operator is "-". See the following example:
$ (Cd/source/directory & tar cf-.) | (cd/dest/directory & tar xvfp -)
This command is used to compress and decompress all files in the/source/directory, and quickly move all files to the/dest/directory, this command will show a special advantage when/source/directory and/dest/directory are not in the same file system.
There are also several uncommon usage:
N <&-indicates that n is disabled
<&-Indicates that the standard input (keyboard) is disabled)
N> &-indicates that output n is disabled.
> &-Indicates that the standard output is disabled.