Sunday, April 12, 2009

Pipe with example in linux

Pipe works as a temporary storage place which takes output of one command and then pass this output to another command as input.
The syntax is using pipe is,
command1 | command2
The output of command1 is passed to command2 as input.

Some examples of pipe are shown below:
We know with who command on linux we can see the user who is logged on. And with wc command we can print the number of newlines.

A sample who command output is,
# who
root pts/0 2009-04-12 00:22 (114.31.1.71)
root pts/1 2009-04-12 04:03 (114.31.1.71)
root pts/2 2009-04-12 04:07 (114.31.1.71)

Now if we want to know the number of users who is logged on to the system then we can use pipe and then wc -l as below.
# who |wc -l
3

The output of who command is passed to wc -l command as input and return the number of lines which in turn returns the number of users.

With ps -ef we can see all processes on the system. If you want to know find the number from all processes where arju name in involved then we can use,
# ps -ef |grep arju |wc -l
9

Output of "ps -ef" is passed to "grep arju" command. "grep arju" command is used to filter output by searching arju text and output of this finally passed input of "wc -l" which prints number 9.
Related Documents
http://arjudba.blogspot.com/2009/04/input-output-rediection-on-linux.html

No comments:

Post a Comment