Unix Lab



Getting help and Pipes



On-line technical help


The man command

If you need to find out about a particular Unix command, there is on-line help. The Unix command named man (for "manual") will give you text-based information about the command you ask it about. For example, if you type:

man ls

you will get the on-line version of the Unix manual page on the ls command. About all that can be said about man is that it is almost always available as long as you have access to a Unix system. Being text-based, it means you can see the contents even if you do not have access at the time to a graphical user interface to Unix. Being text-based means that it is difficult to get to the exact piece of information you are looking for (e.g. the character representing a particular parameter and its function). Often you will scroll past it on the screen window and then have to go back.

The man command shows you the syntax for the command about which you are asking. In particular, you are given the command name, any options, the parameters required and an explanation. Any option or parameter that is contained in square brackets [ ] is optional. Any name that appears underlined is a parameter whose value you must supply if you include that parameter.

Use the man command to find out about the ls command. Which parameters are optional? Which parameters are ones that you will have to supply? What does it mean to have an underlined parameter within a square brackets?

The display of text that you see when you type the man command is controlled by a program in Unix that is actually accessible directly with another Unix command (called more). The more utility displays one screen-load of information at a time. You can scroll back (if you go too far in viewing the man output) by typing b and the spacebar for going forward a screen. You can get a full description of more if you click here.

Use the man command to obtain information about the cp command. What is the purpose of the -i option?

Use the man command to obtain information about the man command. What is the purpose of the -f option? What happens if you use this option to display information about the ls command?

What if you don't remember the name of the command that you want to look up? The man command has an option (-k) that allows you to look things up using keywords. For example, suppose you want to see what Unix has to offer in order to copy files. Try typing:

man -k copy

The command will return a brief description of each command that might relate to copying.

See what you can find out about commands that are related to compressing and decompressing files.

There is a related Unix command called apropos which is really just a synonym for the man command with the -k option. For example, we can duplicate the output from the man example above by typing:

apropos copy

The word "apropos" in English is related to the word "appropriate". Think of it as asking the Unix on-line help facilities for information appropriate or relevant to the given keyword.


The finger command

You might like to find out the login name of someone (like your instructor) so that you can copy a file that the other user wishes to give you or send that user e-mail. The Unix command finger (in old "gangster" films, one bad guy would "finger" his old buddies by telling the police about them) serves this purpose.

Suppose your login name is myName1234 (use your real login name). Type the command:

finger myName1234

You can use someone's login name if you know it and find out their full name or you can try typing someone's family name and obtain their login name. The latter works only on Math/CS systems outside DH450. If there are several matches with the name you give, all users with that name will be displayed.

If you have a friend in the lab, find out their login name.


Learning how to direct input and output

In this section, we will examine the concept of a pipe in Unix and how it can be used to put together chains of operations into larger pieces. We will also examine a command to display the contents of a file that is useful in a variety of contexts.

Standard Input and Output

Whenever you execute a program in a Unix environment, there are three "files" that are automatically part of the package. They are:

Each of these has a default value that you can change if you need to. The first is usually associated with the keyboard. The second and third are usually associated with the terminal screen.

It may seem odd to call these "files" and actually the newer name is "streams". In Unix, files are simply a stream or sequence of bytes. They might be on a disk or they might be coming in from the keyboard or going out to a terminal screen. This concept of a file has the effect of standardizing the input and output across virtually any program. We'll see how this ties in with the concept of pipes.


The cat command

The cat command will copy the contents of a text file to the standard output file. It can be a quick way to view a file although if the file is longer than one screenfull, it will fly by on the screen faster than you can read it.

Copy the file /etc/hosts from the root directory to a new directory called etc in your working directory. Go back to your working directory.

Then, type the command:

cat etc/hosts

Depending on the computer on which you are running, this file may be small or large. If the file is too large to be viewed at one time on the screen, then we must do something else to view it.

The basic form of the cat command takes a single parameter which is the name of the file we wish to view. There are a few options that can be quite useful. For example, the -n option will display a line number for each line of the named file. Try typing:

cat -n etc/hosts

The -v option will display "non-printing" characters such as "control" characters (ones that require that you hold down the control key while pressing another key). The control characters print like ^C for Control-C.


File redirection

Although, as explained earlier, the standard input is normally associated with the keyboard and the standard output is associated with the terminal screen, this can be altered by the user.

Make sure you don't have a file named hostsFile in the directory in which you are working. Type the command:

cat -n /etc/hosts > hostsFile

Now use the cat command on the hostsFile. What appears to have happened?

The '>' character is used after the command that you are issuing to mean that you want to replace the standard output file by the file whose name follows this character.

Copy the file /handouts/cs46blab/BBS-list to your working directory. Use cat to view the contents of this file, then type:

cat /etc/hosts BBS-list > mixedFile

What appears to have happened now? What does cat do if you give it the names of more than one file? What if you were to use three file names?

File redirection can be used with other commands. For example, you could use it with the ls command. How would you create a file containing a line-numbered long listing of all the files in your home directory?

Although we haven't seen enough commands at this point in the lab to provide a good example, the '<' character can be used to replace the standard input file with the file whose name appears after this character. Both the '<' and the '>' characters can be used to redirect the standard input and the standard output files. The order in which these appear is not important. The form, then, for using file redirection is:

command parameters > outfilename

or

command parameters < infilename

or

command parameters > outfilename < infilename


Pipes

Many Unix utility programs expect their input from the standard input file and place their output in the standard output file. Because of this, it is possible to link several such utilities together. One way to do this is to use file redirection to send the output from one utility to a temporary file and then to use file redirection again to use this temporary file as input for the next utility.

There is a better way. Pipes.

A pipe connects the output from one Unix command to the input of another. For example, in a later module, you will learn about more, a command for displaying information on your screen, one screenload at a time. Let's use that command together with the cat command.

Copy the file /handouts/cs46blab/graph.usa to your working directory. Then type:

cat graph.usa | more

The character '|' is the pipe symbol. This character is a vertical bar (not the exclamation character '!'). It is used to connect the command that precedes it with the command that follows it. In particular, the output from the cat command becomes input to the more command. The more utility displays one new screenload each time you press the the spacebar.

The pipe can be used to connect any string of commands in this way. Of course, it has to make sense to send the output from the command before the pipe to the command after the pipe otherwise you will get garbage.

Use a pipe to generate a line-numbered version of the file graph.usa on your screen by combining cat with more.

Use a similar strategy to get a line-numbered long listing of all the files in your home directory. How many different Unix commands do you have to put together?


Click on to go back to the main directory.

Click on to take the quiz for this module.

These pages were developed by John Avila SJSU CS Dept.