Linux redirection command

Source: Internet
Author: User
Linux redirection command standard input, output and error ------------------------------- file descriptor ----------------------------------- input file & mdash; standard input 0 output file & mdash; standard output 1 error output file... linux redirection command standard input, output and error ------------------------------- file descriptor ----------------------------------- input file-standard input 0 output file-standard output 1 error output file-standard error 2 limit 1. redirect 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 # redirect file descriptor I to j. 76 # All output pointing to the I file is sent to j. 77 78> & j 79 # default, redirection file descriptor 1 (stdout) to j. 80 # All output transmitted to stdout is sent to j. 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 91 # to read and write "filename", open the file "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 the standard output to a file command> filename 2> & 1 redirects the standard output and standard error together to a file command 2> filename redirects the standard error command 2> filename in a file 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 file filename as the standard input, use the filename2 file as the standard output command <filename use the command file filename as the standard input command <delimiter reads from the standard input, until the delimiter command <& m uses the file descriptor m as the standard input command> & m redirects the standard output to the file descriptor m command <&-disable the standard input 2. bidirectional redirection means that 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 adds the last content to the end of the file | tee last_backup | cut-d ""-f 1 # tee is equivalent to backing up the last result
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.