next up previous
Next: Startup Files (.login.cshrc, Up: USING UNIX Previous: Command Aliases (alias)

Pipes and Redirects (|, >, >>, <)

Many UNIX programs take input, transform it, and then give output. This is usually set up so that the input comes from a file. If no filename is given, input comes from what is called standard input. The output goes to standard output. Almost always, standard input is the terminal keyboard and standard output is the terminal screen.

Often you want to control the input and output of programs. This is done with pipes and redirects. What a pipe does is connect standard input to standard output. In other words, it takes the output of one command and makes it the input to the next command. For example,
ls | more
takes the output from ls and sends it to the program more which displays it page by page.

The redirects work similarly, but they connect standard input or standard output to a file. The greater than sign > sends the output of the previous command to a file. For example,
ls > file1
sends the result of ls (i.e. a list of the files in the directory) to a file called file1. The double greater sign >> is similar but it appends the result to the file.

The less than sign < takes the file that is after it and uses its contents for input to the command that precedes it. An example is
maple < commd.in > commd.out
which takes the contents of the file commd.in and sends it as input to the program maple. After maple processes the file, the result is put in the file commd.out.

The command cat has a special role in all this. It takes a file and sends it to standard output. Thus cat file sends the file to the terminal screen and cat file1 >> file2 puts the contents of file1 at the end of file2. You can put a group of files together in a single file using
cat file1 file2 file3 > newfile
You can combine many commands, pipes and redirects. For example,
ps -elf | grep idraw > idraw.pc
lists all the processes running and pipes this result to grep idraw. This looks for all the lines that contain the word idraw and then sends this result to a file named idraw.pc. One of the main organizing principles of UNIX is to have simple commands that can be easily linked together in various ways to get powerful results.


next up previous
Next: Startup Files (.login.cshrc, Up: USING UNIX Previous: Command Aliases (alias)