Input and output redirection and piping for Linux

Source: Internet
Author: User

1. Before we get to the redirect, let's look at the file descriptor for Linux.

Linux file Descriptor : Can be understood as Linux in order to track open files, and the allocation of a number, (people are based on the file name tracking files, the system is based on the file descriptor), this number is similar to the C language operation of the file when the pointer, through the pointer can be implemented file read and write operations. User can customize the file descriptor range is: 3-num, this maximum number, with the user's: Ulimit–n defined number has a relationship, cannot exceed the maximum value.

After Linux startup, 3 file descriptors will be opened by default: standard input 0, normal output 1, error output after the file is opened after opening. The new file binding descriptor can be incremented in turn. A shell command executes, inheriting the file descriptor of the parent process. Therefore, all running shell commands will have a default of 3 file descriptors.


Linux provides three I/O devices to Programs

Standard input (STDIN)-0 accepts input from the keyboard by default

Standard output (STDOUT)-1 Default output to terminal window

Standard error (STDERR)-2 default output to terminal window


The following procedure is performed for any Linux command:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/85/3B/wKioL1edqciB8dB5AAA7KobIu2Y777.png-wh_500x0-wm_3 -wmp_4-s_257263030.png "title=" pipe. PNG "alt=" Wkiol1edqcib8db5aaa7kobiu2y777.png-wh_50 "/>


The execution of a command requires standard input of the keyboard, command execution, and correctness or error, where each double-headed arrow is a channel, so that the data flow can flow both to the file (that is, redirect or pipe), or to the screen if the command requires input that is not from the keyboard, but from the specified file, This is the input redirection. Similarly, the output of the command can be written to the specified file instead of being displayed on the screen, which is the output redirect.

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

2. Standard error redirection symbols

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 means standard output

>& redirect a standard error output to a file or device to overwrite the original file C-shell

|& output a standard error pipe to another command as input

Next look at the actual application of standard I/O redirection in constructing a Linux command.

3. Input redirection and output redirection


[[email protected] testdir]# WC aa.txt//command equals wc< aa.txt but generally Jane is written in the former [[email protected] testdir]# ls >ls_file.txt


The first command takes the file Aa.txt as input to the WC command, counting the number of rows, words, and characters aa.txt. The second command saves the output of the LS command in a file named Ls_file.txt. If the file behind the > symbol already exists, the file will be rewritten.

Using pipelines

Using the pipe symbol "|" provided by Linux Separates the two commands, and the output from the left command of the pipe character is entered as the command to the right of the pipe. Continuous use of the pipe means that the output of the first command will be entered as the second command, and the output of the second command will be entered as the third command, and so on. Let's see how the pipeline is applied in constructing a Linux command.

4. Use the pipeline to enter the standard input and standard errors of the preceding command into the Wc–l to count the number of rows

[Email protected] testdir]# lsa.txt [[email protected] testdir]# ll a.txt b.txt |& wc-l2


5. Save standard input and standard errors separately to file Corr.log and Err.log

 [[email protected]]# ls a.txt   [[ Email protected] testdir] #ll  a.txt b.txt |& wc -l 2[[email  protected] testdir]# ll a.txt b.txt >>corr.log   2>>  Err.log[[email protected] testdir]# lltotal 20-rw-r--r--.  1 root root  136 jul 30 16:11 a.txt-rw-r--r--.  1 root root 47 jul 30  20:46 corr.log-rw-r--r--.  1 root root 51 Jul 30 20:46  Err.log [[email protected]]# cat corr.log -rw-r--r--.  1 root root  136 jul 30 16:11 a.txt[[email protected] testdir]# cat err.log  ls: cannot access b.txt: no such file or directory 


6. Output the standard output and errors to the same file:

[[email protected] testdir]# ll a.txt b.txt >>all.log 2>&1[[email protected] testdir]# ll a.txt B.txt & >>all2.log [[email protected] testdir]# cat All2.log ls:cannot access b.txt:no such file or Directory-rw-r--r--. 1 root root 136 Jul 16:11 A.txt[[email protected] testdir]# cat All.log ls:cannot access b.txt:no such file or direct Ory-rw-r--r--. 1 root root 136 Jul 16:11 a.txt


Note: Both of the above methods are OK, the former is rhel5 previous versions are supported,&>> is a newer way of writing


7. Multi-line redirection:

[[Email protected] ~] #mail-S "jack_cui send mail to root" root << enf> Hello root> this is Testmail t O You > Enf


Where Enf is the text end identifier, you can define the text end identifier yourself


This article from "Jackcui" blog, reproduced please contact the author!

Input and output redirection and piping for Linux

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.