Fundamentals of Unix Tutorial

PIPE Commands and REDIRECTION Commands – CH 7

The content of this tutorial also applies to linux.

Piping

UNIX supports and makes extensive use of the ability to connect commands to one another. By piping commands together, you can use the output of one command as the input for another.

The pipe symbol is | (a vertical bar). To pipe commands together you simply place a pipe between each command on the command line. For example

command1 | command2 | command3

Although you can pipe many commands together, you'll rarely need to do so.

Examples of using pipes to connect commands

Here are some examples you can learn from and adapt for your own uses. If you don't completely understand the commands are that are being piped, look them up in other parts of this tutorial or in the glossary of the Cisco-Sun online curriculum.

Example: ls -alFs | more

This command sends the output of the ls command with the options alFs to the more command. The options used with ls indicate that all files, including hidden ones will be listed, in long form, marking directories with a slash (/), executables (programs) with an asterisk (*), and the size of each file will be listed as well. Normally such a list would scroll by too quickly to view, but by piping the output through the more command you can view one page at a time.

Example: who | wc -l

This command takes the output from the who command which lists everyone currently logged in to the system and passes the information to the wc command which, using the -l option, counts the number of lines that the who command generated. Since who generates a line for each person logged in, the number of lines is equal to the number of people logged in to the system.

Example: who | grep Chris

Similar to the first example, this command pipes the output of the who command to the grep command which searches for instances of the letters "Chris". If grep finds a line containing the letters, grep prints the line(s) to the screen. This command would tell you how many people with the user name Chris are logged in.

Example: Connecting several commands together:

ps –ef | grep joe | sort +5 –6 | less

The first command, ps –ef, outputs information about the processes currently running. This information becomes the input to the command grep joe which searches for any lines that contain the username "joe". The output from this command is then sorted on the sixth field of each line, with the output being displayed one screenful at a time using the less pager (similar to more).

Redirection

Normally everything you do on the keyboard is sent to the computer screen for your viewing pleasure. That is, when you type a command, the letters you type are sent to the screen so you can see what you're typing. The results of the command are also sent to the screen so you can see what happened.

Your typing on the keyboard is the "input" and what you see on the screen is the "output." To UNIX, a computer screen and a file are forms of output. The screen is the standard output, normally abbreviated as "stdout." With redirection you can send output to files instead of the screen, or you can use what's in a file as the input for a command. With redirection you can change where the computer gets input, and where it puts output.

You use the symbols < , > , > , and 2> to redirect input and output.

Examples of using redirection

Redirecting standard output: To redirect the standard output from a command use the (greater than) symbol followed by the name of the output file. If the file that you redirect standard output to does not already exist it will be created.

Example: ls -la > mydir

This command lists all the files and directories in long form, and sends them to the file mydir. If the file mydir does not exist, it will be created. If it does exist it will be overwritten with the new data. When you execute this command, you will not see anything appear on the screen (unless there is an error – that is described below).

Example: grep Smith /etc/passwd > popular

This redirects the standard output from the grep command so that it goes to the file popular. This file will contain all the lines containing occurrences of the string “Smith” that were found in the /etc/passwdfile.

Example: cat section1 section2 section3 > Chapter1

This example connects (concatenates) several files in order, and outputs them as one file, Chapter1. The cat command reads files section1 through section3 in turn and appends them to (tacks them onto the end of) the file Chapter1. The contents of section2 are appended to section1 and so on.

Example: who > people

This prints the list of active users to the file people.

Example: sort file1 > sorted_list

This sorts the contents of file1 and saves the sorted list as sorted_list.

Appending standard output: To append the standard output from a command to a file, use two (greater than) symbols followed by the name of the file. If the file does not exist it is created. For example:

Example: ls > mydir

This appends the data from a short form directory listing to the data (if any) already in the file mydir.

Example: If you wanted to add one more section to the file Chapter1, created earlier, you'd append the text using the command

cat section4 > Chapter1

to make sure the text that already exists in the file isn't disturbed.

Redirecting standard input: Redirecting input is just as easy. For example, say you want to mail someone the directory list you just made, called mydir. Instead of having to import it into a mail program, you can redirect the input for the mail command:

Example: mail Tony < mydir

This command uses the contents of the mydir file as the input for the mail command. The effect of this is to mail the contents of this file to the user Tony.

Redirecting error output: Even though you send the standard output to a file, if an error occurs the error message will be sent to your screen. This is because the standard error output, or “stderr”, is the screen. If you wish to redirect error messages to a file for troubleshooting purposes, use the 2> symbol. For example:

cat file1 file2 file3 2> Error_Log

Any errors that occur during the execution of this command will be included in Error_Log. Since we are troubleshooting, the output, if any, of the command will probably be incorrect. If there is any output, it will be directed to the screen, since we did not direct it to a file. We directed only the error messages to a file so we could have a record of them.

Example of combining piping and redirection

You can combine piping with redirection to create files that contain the output.

Example: who | grep Chris > List_of_Chris

In the example above, the list of lines from the who command containing the letters "Chris" is redirected from the screen (the standard output device) to the file List_of_Chris. You could view the resulting text file with a viewer such as the more command, or an editor such TED or Nedit, or the text editor in the Solaris CDE.