3.4 Pipe



There are some commands to be able to do special processing by putting data from the standard input. Let's enter the following command.


$ ls -l /usr/bin ↵


The file list will be flowed on the screen. There are 1000 or more commands in /usr/bin and can not be displayed on the screen as it is. Next, enter the following.


$ ls -l /usr/bin | less ↵


"less" is the command of the pager that appeared in the previous chapter. In addition to paging files, the less command has function of paging and displaying data from standard input. You can terminate the pager function by entering q.

In addition, here is a "|", which is called "pipe". The role of a pipe is to "connect" standard output and standard input. In case of "ls - l /usr/bin | less", "connect" the file list from the standard output of the ls command and the standard input of the less command. As a result, the output can be displayed with page.

Figure3-2
Figure 3-2: Standard Input, Standard Output and Pipe


You can also use this pipe for standard output and standard error mixed output which is described in standard error section. Please enter as follows.


ls -l no-exist-file /usr/bin 2>&1 | less ↵


Because 'no-exist-file' doesn't exist, a message is output to the standard error


Practice: Paging display of command execution result by less command


The file list of /usr/bin is displayed with paging by less command.


ls -l /usr/bin | less ↵
            :
            : (Operation)
            :
(Quit by q)



Previous Next