Linux redirection command

Source: Internet
Author: User

Linux Redirection command application and syntax[Copy link]

Posted on 2008-12-18 18:24 | From 51CTO Web page

[See him only] landlord

1. Control of the standard input
Syntax: The command file sends the execution result of the command to the specified file.
For example:
Ls-l > List writes the result of executing the "ls-l" command to the file list.
Syntax: Command >! The file sends the execution result of the command to the specified file, overwriting if the file already exists.
For example:
LS-LG >! The list overwrites the results of the "LS-LG" command with the Write file list.
Syntax: the command >& file writes any information that is generated on the screen when the command executes to the specified file.
For example:
CC file1.c >& Error writes any information generated when compiling the file1.c file to the file error.
Syntax: command >> file attaches the result of the command execution 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 to the specified file any information generated on the screen when the command executes.
For example:
CC FILE2.C >>& Error appends any information generated by the screen to the file error when compiling the file2.c file.
3. Command syntax
Command > FileName redirect standard output to a new file
Command >> filename REDIRECT standard output to a file (append)
Command 1 > Fielname redirect standard output to a file
Command > FileName 2>&1 redirect standard output and standard error to a file
Command 2 > FileName redirects a standard error to a file
Command 2 >> filename REDIRECT standard output to a file (append)
Command >> filename 2>&1 redirect standard output and standard error to a file (append)
Command filename2 the command command with the filename file as the standard input, with the filename2 file as the standard output
Command &m redirect the standard output to the file descriptor m
Command Linux command redirection and Pipeline command
Command redirection:In the execution of the Bash command, in addition to the > symbol, there are 3 output input conditions, namely: 1 standard Input: code 0, or called stdin, using the mode of < 2 standard output: Code 1, or called stdout, using the 1> 3 error Output: Code 2, or referred to as stderr, used in 2> >>add to the file, the file accumulates, and the old data is retained. Example: [Test @test test]# ls–al 1>list.txt 2>list.err display data correctly output to List.txt, incorrect data output to List.err
> Comparison with >>, difference:
to output the correct data originally from the screen to the file or device on the > Right >>: Output The correct data originally from the screen to the >> right, Unlike >, the file will not be overwritten, and the new data will be added to the last side of the file in an additive manner. What is/dev/null ?
A : Basically, it's kind of like a bottomless pit, which collects rubbish, and when anything you enter is directed into this virtual dustbin, it is very useful to vanish out of thin air.
Example: [Test @test test]# Find/-name testing 1> list_right 2>/dev/null
If you want to write the data in the same file [test @test test]# Find/-name testing 1> list_right 2>&1

The pipeline Command Pipeline command uses the ' | ' Define Symbol Pipeline command "|" Only the correct information from the previous command, which is the standard output (stdout) information, is processed, and there is no direct processing capability for standard error messages.
Each pipeline part is an instruction, and the input of the latter instruction is the output of the previous instruction.
Commonly used pipeline command: Cut syntax: [root @test/root]# cut–d "separator character" [-CF] Fields parameter description:-D: followed by the split character, the default is a space-C: followed by the number of characters-F: followed by the first few block C UT's main purpose is to decompose the data in the same row, most often when analyzing some data or file data, because sometimes we use certain characters as split parameters and then cut the data to get the data we need (often and analyze our log files).
Sort syntax: [root @test/root]# sort [-t delimiter] [(+ Start) (-end)][-nru]
Parameter description:-T delimiter: use separators to separate different chunks, default is tab +start–end: sorted by Start block to end block-N: Sort with a pure number (otherwise alphabetical sort)-r: Reverse Sort-u: The same line that appears, only once
[Root @test/root]# cat/etc/passwd | Sort sorts the listed personal accounts
WC syntax: [Root @test/root]#&NBSP;WC [-LMW]
Parameter Description:-L: How many lines-m: How many symbols W: how many words
Example: [Root @test/root]# cat/etc/p asswd | Wc–l How many lines are in this file.
Uniq Syntax: [root @test/root] #uniq
Uniq Delete duplicate rows to display only one

Tee example: [Root @test/root]# last | tee last.list | cut–d The –FI uses tee to pass the data to the next command execution, and also writes the data to the Last.list file in the

TR syntax: [root @test/root]# tr [-ds] SET1 parameter description:-D: Delete SET1 this string- S: Replace repeated characters
Example: [Root @test/root]# cat/etc/passwd | tr–d: ":" This symbol is missing in/etc/passwd

Split syntax: [Root @test/root]# Split [BL] input file output file leading character parameter description:-B: The file size to divide-L: The number of rows for the
example: [Root @test/root]# split–l 5/etc/passwd test will produce Testaa,te Files such as Stab,testac
Related Article

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.