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. REDIRECT

Command_output > 2  # REDIRECT stdout to a file.   3  # If this file does not exist, create it, or overwrite it.   4  5  ls-lr > Dir-tree.list 6  # Create a file that contains a list of directory trees.  7  8 : > FileName 9  # > operation will change the file "filename" to an empty file (that is, size 0).  10  # If the file does not exist, create a 0-length file (the same effect as ' touch ').  11  #: is a placeholder that does not produce any output.  12  13  > FileName 14  # > operation will change the file "filename" to an empty file (that is, size 0).  15  # If the file does not exist, create a 0-length file (the same effect as ' touch '). 16  # (same as ": >" above, but some shells may not support this form.) 17  18  command_output >> 19  # will Stdou  T redirects to a file.  20  # If the file does not exist, create it and append it to the file if it exists. 21  22  23  # single-line redirection command (affects only the row they are in): 24  #--------------------------------------------------  ------------------25  26  1>filename 27  # REDIRECT stdout to file "filename".  28  1>>filename 29  # Redirect and append stdout to file "filename". 30   2>filename 31  # REDIRECT stderr to file "filename".  32  2>>filename 33  # Redirect and append stderr to file "filename".  34  &>filename 35  # redirects stdout and stderr to the file "filename".  36  37  m>n 38  # "M" is a file descriptor that defaults to 1 if not explicitly specified.  39  # "N" is a file name.  The 40  # file descriptor "M" is redirected to the file "N".  41  m>&n 42  # "M" is a file descriptor that defaults to 1 if not explicitly specified.  43  # "N" is another file descriptor. 44  45  #============================================================================== 46  47    # REDIRECT StdOut, one line at a time. 48  logfile=script.log 49  50  echo "This statement are sent to the log file, \" $LOGFILE \ "." 1> $LOGFILE 51  echo "This statement was appended to \" $LOGFILE \ "." 1>> $LOGFILE 52  echo "This statement was also appended to \" $LOGFILE \ "." 1>> $LOGFILE 53  echo "This statement was echoed to stdout, and would not be appear in \" $LOGFILE \ "." 54  #After each line, these redirect commands automatically "reset".    58  # REDIRECT stderr, one line 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 redirect commands will also automatically "reset". 66  #============================================================================== 67  70  2&GT;&A  mp;1 71  # REDIRECT stderr to stdout.  72  # sends the output of the error message to the place pointed to by the standard output. 73  74  i>&j 75  # REDIRECT File descriptorIToJ. 76 # PointIAll output of the file is sent toJ. &GT;&AMP;J 79 # Default, redirected file descriptor1(stdout) toJ. 80 # All outputs delivered to stdout are sent toJto the.  Bayi 0< filename < filename 84 # accepts input from the file. 85 # with">"are paired commands and are usually used in combination. Search-word <filename [J]<>filename 91 # to read and write "filename", the file "filename"  Opens, and assigns 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, then the default is FD 0, stdin.  94 # 95 # This application is usually done to write to the place specified in a file.  1234567890 echo > File # Writes a string to "file".  The $ exec 3<> file # opens "file" and assigns FD 3 to it.  98 Read-n 4 <&3 # reads only 4 characters. Echo-n. >&3 # Write a decimal. + EXEC 3>&-# close FD 3.101 cat File # ==> 1234.67890102 # Random visit Q. 106 |107 # Pipe. 108 # General purpose processing and command chain tool. 109 # with">", very similar, but actually more generic. 110 # Useful when you want to concatenate commands, scripts, files, and programs. 111 Cat *.txt | Sort | Uniq > result-file112 # Sorts the output of all. txt files and deletes duplicate rows. 113 # Finally Save the result to"Result-file"In.

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 < filename >filename2 the command command with the filename file as the standard input, with the filename2 file as the standard output
Command < filename use the command command as a standard input to the filename file
command << delimiter reads from the standard input until the delimiter delimiter is encountered
command <&m The file descriptor m as standard input
Command >&m redirect the standard output to the file descriptor m
command <&-turn off the standard input

2. Bidirectional redirection

That is, while redirecting the data to the target file, you also need to ensure that the data is handled normally, 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相当于对last的结果备份了一次

Linux redirection command

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.