Piping Commands

Piping Commands

The UNIX operating system has been designed to be modular. Instead of providing commands to do everything the user is likely to want to do, the UNIX philosophy is to use the existing primitive commands to build up a new command. This is accomplished by allowing the output from one command to be piped to the input of the next command.

The general form of a piped command line is:

command | command

We have already met a piped command when printing a file:

 

Type: cat filename | lp

Here the output of the cat command is piped to the lp command.

The cat command shows only the contents of a file which, if sent to a printer, might need header information or a page number. To produce a listing of a file, on a printer, with say the filename at the top of every page:

Some Examples Of Piped Commands

To obtain an alphabetised list of the users currently logged in to the system:

Type: who | sort

To obtain the number of logged in users:

Type: who | wc -l

To see the number of files in your current working directory:

Type: ls | wc -w

To filter out the redundant information contained in the file /etc/passwd and display only the lines that contain a specified string use the grep ("get regular expression and print") command. For example:

Type: grep "Mobbs" /etc/passwd

To list all the text files in your current directory:

Type: file * | grep text

Source: http://www.le.ac.uk/oerresources/bdra/unix/page_55.htm