"Linux" redirection

Source: Internet
Author: User

1 redirects


1.1 Redirect Symbol

> Output redirected to a file or device overwriting the original file
>! Output redirected to a file or device forcing overwriting of the original file
>> output redirected to a file or device to append the original file
< redirect input to a program

1.2 standard error redirect symbol

2> redirects a standard error output to a file or device overwriting the original file B-shell
2>> redirect a standard error output to a file or device appended to the original file
2>&1 redirect a standard error output to the standard output note: 1 could be the standard output.
>& redirect a standard error output to a file or device to overwrite the original file C-shell
|& to send a standard error pipe to another command as input

1.3 Command Redirection example

In the execution of the Bash command, there are three main output conditions, namely:
1. Standard input; Code 0; or stdin; used in <
2. Standard output: Code 1, or stdout; used in 1>
3. Error Output: Code is 2, or stderr; used in 2>


[Test @test test]# ls-al > list.txt
Output the displayed results to the List.txt file, and replace the file if it exists!


[Test @test test]# ls-al >> list.txt
Add the displayed results to the List.txt file, the file is cumulative, the old data is retained!


[Test @test test]# ls-al 1> list.txt 2> list.err
The data to be displayed, the correct output to the List.txt error data output to List.err


[Test @test test]# ls-al 1> list.txt 2> &1
output the displayed data, whether correct or error, to List.txt! Error and correct file output to the same file, it must be written in the method above! cannot be written in any other format !

[Test @test test]# ls-al 1> list.txt 2>/dev/null
The data that is displayed, the correct output to the List.txt error, is discarded! /dev/null, can be said to be a black hole device. is empty, which is not saved.

1.4 Why to use command output redirection

? When the information on the screen output is important, and we need to save him;
? Background execution in the program, do not want him to interfere with the normal output of the screen when the results;
? Some of the system's routine commands (such as files written in/etc/crontab) result in the execution of the hope that he can be saved;
? Some execution orders, we already know his possible error message, so want to "2>/dev/null" to throw him away;
? When the error message and the correct message need to be output separately.

2 Pipeline Command (pipe)

As I said earlier, when the Bash command executes, the output data will appear, so if this group of data has to go through a few formalities to get the format we want, how should we set it? This involves a problem with the pipeline command (pipe), and the Pipeline command uses the "| 』。


Example: Simple Pipeline command
Suppose we want to read the last command, what is the "number" of the root login?
So the steps that I take are:
1. Execute last and take all the people logged in this month to the data;
2. Use grep to extract the root of the above output data (stdout), and not others;
3. Use WC This can count the number of rows of the command to calculate the number of rows in the previous step of the data!
Since the last output is a one-time login, so as long as the calculation of a few lines on behalf of the login several times, through the above three steps, the last data gradually filtered, you can get our data! The entire command can be written as follows: [Test @test bin]# Last | grep root | Wc-l
This Pipeline command "| "The ability to handle only the correct information from the previous command, which is standard output (STDOUT), is not directly handled for Stdandard error."

2.1 Basic Pipeline Command Instruction introduction

? Cut
Syntax: [Root @test/root]# cut-d "separator character" [-CF] Fields
Parameter description:
-D: followed by the character to be delimited, the preset is "whitespace"
-C: followed by "Number of characters"
-F: What is the number of blocks followed?
Example: [Root @test/root]# cat/etc/passwd | Cut-d ":"-F 1
Will passwd this file inside each line: used as a separator number, and list the first chunk! That's where the name is!
[Root @test/root]# Last | Cut-c1-20
Remove the last data, 1-20 characters from each line!
? Sort
Syntax: [root @test/root]# sort [-t delimiter] [(+ Start) (-end)] [-NRU]
Parameter description:
-T separators: use separators to separate different intervals, and the default is tab
+start-end: Sorted by start range to end interval
-N: Sort with "pure number" (otherwise it will be sorted in text form)
-R: Reverse Sort
-U: The same line that appears, listed only once!
Example: [Root @test/root]# cat/etc/passwd | Sort will list the individual accounts listed!
[Root @test/root]# cat/etc/passwd | SORT-T: +2N will be in the personal account, with the user ID to sort (by: to separate, the third is the ID, but the first code is 0)
[Root @test/root]# cat/etc/passwd | SORT-T: +2nr reversed-phase sort!
? Wc
Syntax: [Root @test/root]# WC [-LMW]
Parameter description:
-L: How many lines
-M: How many characters
-W: how many words
Example: [Root @test/root]# cat/etc/passwd | Wc-l How many lines are there in this file?
[Root @test/root]# cat/etc/passwd | Wc-w How many words are in this file!?
? Uniq This command is used to delete "duplicate rows out of only one"
Syntax: [Root @test/root]# uniq
Example: [Root @test/root]# Last | Cut-d ""-f1 | Sort | Uniq
? The tee command redirects the data to the file while it is displayed on the screen
Syntax: [Root @test/root]# Last | Tee Last.list | Cut-d ""-f1
Example: [Root @test/root]# Last | Tee Last.list | Cut-d ""-f1
? Tr
Syntax: [Root @test/root]# tr [-ds] SET1
Parameter description:
-D: Delete the SET1 string
-S: Replace duplicate characters!
Example: [Root @test/root]# Last | TR ' [A-z] ' [A-z] ' <== lowercase to uppercase
[Root @test/root]# cat/etc/passwd | Tr-d: <==: This symbol is missing in/etc/passwd!
[Root @test/root]# Cat/home/test/dostxt | Tr-d ' \ r ' > Dostxt-nom
? Split
Syntax: [Root @test/root]# split [-BL] input file output file leading character
Parameter description:
-B: Divide by file size
-L: divided by the number of rows
Example: [Root @test/root]# split-l 5/etc/passwd test <== will produce Testaa, Testab, Testac ... Wait for the file
Description: It's a lot easier under Linux! If you want to split the file, then use the-B size to limit the size of a segmented file, and if it is the number of rows, then use the-L line to split it!
Pipeline commands are quite important in bash's continuous handlers! In addition, the analysis of log file is also a very important link.
The standard input for piping to a command can be controlled more carefully using the standard input parameter "-". Example of a cat command
Eg:sort MyList | More
Sort MyList | Cat–n | Lpr
PWD | Cat–mylist | Lpr

"Linux" redirection

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.