Linux redirection command

Source: Internet
Author: User

Standard input, output, and error
---------------------------------
File descriptor
---------------------------------
Input File-standard input 0
Output file-standard output 1
Error output file-Standard Error 2
---------------------------------

1. Redirection

Command_output> 2 # redirect stdout to a file. 3 # If the file does not exist, create it; otherwise, overwrite it. 4 5 LS-LR> dir-tree.list 6 # create a file that contains the directory tree list. 7 8:> filename 9 #> operation, the file "FILENAME" will be changed to an empty file (that is, the size is 0 ). 10 # If the file does not exist, create a zero-length file (same effect as 'touch ). 11 #: it is a placeholder and does not produce any output. 12 13> filename 14 #> operation, the file "FILENAME" will be changed to an empty file (that is, the size is 0 ). 15 # If the file does not exist, create a zero-length file (same effect as 'touch ). 16 # (same effect as ":>" above, but some shells may not support this form .) 17 18 command_output> 19 # redirect stdout to a file. 20 # If the file does not exist, create it. If yes, append it to the end of the file. 21 22 23 # single-line redirection command (will only affect the row where they are located): 24 # -------------------------------------------------------------- 25 26 1> filename 27 # redirect stdout to the file ". 28 1> filename 29 # redirect and append stdout to the file "FILENAME ". 30 2> filename 31 # redirect stderr to the file "FILENAME ". 32 2> filename 33 # redirect and append stderr to the file "FILENAME ". 34 &> filename 35 # redirect both stdout and stderr to the file "FILENAME ". 36 37 m> N 38 # "M" is a file descriptor. If not specified, the default value is 1. 39 # "N" is a file name. 40 # The file descriptor "M" is redirected to the file "N ". 41 m> & n 42 # "M" is a file descriptor. If not specified, the default value is 1. 43 # "N" is another file descriptor. 44 45 #================================================= ========================================================== = 46 47 # stdout redirection, one row at a time. 48 logfile = script. log 49 50 echo "This statement is sent to the log file, \" $ logfile \". "1> $ logfile 51 echo" This statement is appended to \ "$ logfile \". "1> $ logfile 52 echo" This statement is also appended to \ "$ logfile \". "1> $ logfile 53 echo" This statement is echoed to stdout, and will not appear in \ "$ logfile \". "54 # after each line, these redirection commands will automatically" reset ". 58 # redirect stderr, one row at a time. 59 errorfile = script. errors 61 bad_command1 2> $ errorfile # error message sent to $ errorfile. 62 bad_command2 2> $ errorfile # error message appended to $ errorfile. 63 bad_command3 # error message echoed to stderr, 64 # + and does not appear in $ errorfile. 65 # after each line, these redirection commands will automatically "reset ". 66 #======================================================== ========================================================== 67 70 2> & 1 71 # redirect stderr to stdout. 72 # Send the output of the error message to the point pointed to by the standard output. 73 74 I> & J 75 # redirection file descriptorIToJ. 76 # pointIAll output of the file is sentJ. 77 78> & J 79 # default, redirection file descriptor1(Stdout)J. 80 # all outputs passed to stdout are deliveredJ. 81 82 0 <FILENAME 83 <FILENAME 84 # accept input from the file. 85 # It is a paired command with ">" and is usually used in combination. 86 #87 # grep search-word <FILENAME 88 90 [J] <> filename 91 # open the file "FILENAME" to read and write "FILENAME, and assign the file descriptor "J" to it. 92 # If the file "FILENAME" does not exist, create it. 93 # If the file descriptor "J" is not specified, the default value is fd 0 and stdin. 94 #95 # this type of application is usually used to write to a specified place in a file. 96 echo 1234567890> file # Write the string to "File ". 97 exec 3 <> file # Open "file" and allocate FD 3 to it. 98 read-N 4 <& 3 # Read Only 4 characters. 99 echo-N.> & 3 # Write a decimal point. 100 exec 3> &-# disable FD 3.101 cat file #=> 1234.67890102 # random access. 106 | 107 # MPs queue. 108 # general purpose processing and command chain tools. 109 # similar to ">", but more common. 110 # It is useful for concatenating commands, scripts, files, and programs. 111 cat *. TXT | sort | uniq> result-file112 # Sort the output of all. txt files and delete duplicate rows. 113 # Save the result to "result-File.

Command> filename redirects the standard output to a new file
Command> filename redirects the standard output to a file (append)
Command 1> fielname redirects standard output to a file
Command> filename 2> & 1 redirects the standard output and standard error to a file.
Command 2> filename redirects a standard error to a file
Command 2> filename redirects the standard output to a file (append)
Command> filename 2> & 1 redirects the standard output and standard error to a file (append)
Command <FILENAME> filename2 uses the command as the standard input in the filename file and filename2 as the standard output.
Command <FILENAME: The command is input using the filename file as the standard.
Command <delimiter reads data from the standard input until the delimiter Delimiter is encountered.
Command <& M uses the file descriptor m as the standard input
Command> & M redirects standard output to file descriptor m
Command <&-Disable Standard Input

2. Bidirectional redirection

That is, when redirecting data to the target file, you must ensure that the data can be processed properly by using the tee command.

Tee [-A] File

-A: add content to the end of the file

Last | tee last_backup | cut-d ""-F 1 # tee is equivalent to backing up the last result

 

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.