Commands

Every time we have done anything useful, we have executed a command to do it. Here we will discuss what a command is, and what they will enable you to do.

Your First Command

We'll start off with a practical example: the cal command. Type the following at the UNIX prompt:

steel /N/fs1/clwolfe/Steel $ cal

   February 2000

 S  M Tu  W Th  F  S

       1  2  3  4  5

 6  7  8  9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29

As you can see, the cal command simply prints out the current month's calendar.

Getting Into Arguments

Of course, what use is a calendar that only shows the current month? If only we could specify what month we wanted....

steel /N/fs1/clwolfe/Steel $ cal 6 1977

   June 1977

 S  M Tu  W Th  F  S

          1  2  3  4

 5  6  7  8  9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30

As you can see, we passed along extra information to the command. Each piece of extra information is called an argument. Commands differ widely in terms of the number and type of arguments they can accept. The same command may behave differently with a different number of argumants, as well. For example....

steel /N/fs1/clwolfe/Steel $ cal 1977

                                1977



         Jan                    Feb                    Mar

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

                   1          1  2  3  4  5          1  2  3  4  5

 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12

 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19

16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26

23 24 25 26 27 28 29   27 28                  27 28 29 30 31

30 31

         Apr                    May                    Jun

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

                1  2    1  2  3  4  5  6  7             1  2  3  4

 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11

10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18

17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25

24 25 26 27 28 29 30   29 30 31               26 27 28 29 30



         Jul                    Aug                    Sep

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

                1  2       1  2  3  4  5  6                1  2  3

 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10

10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17

17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24

24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30

31

         Oct                    Nov                    Dec

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

                   1          1  2  3  4  5                1  2  3

 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10

 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17

16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24

23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31

30 31

With only one argument, cal assumes it is a year, and prints out a 12-month calendar. This could be confusing; what if you wanted August's calendar, and tried....

steel /N/fs1/clwolfe/Steel $ cal 8

                                8



         Jan                    Feb                    Mar

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

 1  2  3  4  5  6  7             1  2  3  4                1  2  3

 8  9 10 11 12 13 14    5  6  7  8  9 10 11    4  5  6  7  8  9 10

15 16 17 18 19 20 21   12 13 14 15 16 17 18   11 12 13 14 15 16 17

22 23 24 25 26 27 28   19 20 21 22 23 24 25   18 19 20 21 22 23 24

29 30 31               26 27 28 29            25 26 27 28 29 30 31



         Apr                    May                    Jun

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

 1  2  3  4  5  6  7          1  2  3  4  5                   1  2

 8  9 10 11 12 13 14    6  7  8  9 10 11 12    3  4  5  6  7  8  9

15 16 17 18 19 20 21   13 14 15 16 17 18 19   10 11 12 13 14 15 16

22 23 24 25 26 27 28   20 21 22 23 24 25 26   17 18 19 20 21 22 23

29 30                  27 28 29 30 31         24 25 26 27 28 29 30



         Jul                    Aug                    Sep

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

 1  2  3  4  5  6  7             1  2  3  4                      1

 8  9 10 11 12 13 14    5  6  7  8  9 10 11    2  3  4  5  6  7  8

15 16 17 18 19 20 21   12 13 14 15 16 17 18    9 10 11 12 13 14 15

22 23 24 25 26 27 28   19 20 21 22 23 24 25   16 17 18 19 20 21 22

29 30 31               26 27 28 29 30 31      23 24 25 26 27 28 29

                                              30

         Oct                    Nov                    Dec

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

    1  2  3  4  5  6                1  2  3                      1

 7  8  9 10 11 12 13    4  5  6  7  8  9 10    2  3  4  5  6  7  8

14 15 16 17 18 19 20   11 12 13 14 15 16 17    9 10 11 12 13 14 15

21 22 23 24 25 26 27   18 19 20 21 22 23 24   16 17 18 19 20 21 22

28 29 30 31            25 26 27 28 29 30      23 24 25 26 27 28 29

                                              30 31

Naturally, cal printed out the calendar for the year 8, not the month. Be sure to do a sanity check on your command before pressing enter. cal is harmless, but you wouldn't want to pass the wrong argument to a more serious command, such as rm (which deletes files).

In addition to arguments, many commands allow you to specify options. An option is simply a modifer - it changes the way a command works. Let's try this on another non-destructive command, ls.

steel /N/fs1/clwolfe/Steel $ ls

Mail         dead.letter   matlabrc.m    samuel.txt

MailArchive  matlab       personal     www

ls prints out a list of all of the files in the current directory. We'll talk about ls in more detail later, but for now, let's try using options with ls to get more information.

steel /N/fs1/clwolfe/Steel $ ls -l

total 10

drwx------   2 clwolfe  staff     1024 Feb 21 14:32 Mail

drwx------   2 clwolfe  staff       96 Jan  6 10:10 MailArchive

-rw-------   1 clwolfe  staff     3148 Feb 17 14:03 dead.letter

drwx------   2 clwolfe  staff     1024 Nov 12 16:54 matlab

-rw-------   1 clwolfe  staff     4725 Aug 16  1999 matlabrc.m

drwx------   2 clwolfe  staff       96 Nov  9 15:31 personal

-rw-------   1 clwolfe  staff     3305 Dec 10 12:19 samuel.txt

drwx--x--x   8 clwolfe  staff     1024 Feb 10 14:05 www

Here we passed the l option (l, as in L, not 1) to the ls command. Most options begin with a -. That's how the command can tell that it is an option, not an argument. Some commands have options that start with two hyphens.

Gluing Commands Together

You may have noticed, when we were playing with the cal commands, that sometimes the output of a command will fly by too fast for you to read. This is a common problem, and it has a simple solution. You might think that there is an option that slows down the output, or something like that. In fact, there is a much more general solution - the more command. The more command is very stupid - all it knows how to do is to take whatever is put into it, and then print it to the screen, one screenful at a time. So, we need a way of gluing commands together.

We can do this using the "pipe operator", which is provided by your shell. It looks like a little vertical line (|), and is typically found near the enter key. Here it is in action:

steel /N/fs1/clwolfe/Steel $ cal 1999 | more

                                1999



         Jan                    Feb                    Mar

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

                1  2       1  2  3  4  5  6       1  2  3  4  5  6

 3  4  5  6  7  8  9    7  8  9 10 11 12 13    7  8  9 10 11 12 13

10 11 12 13 14 15 16   14 15 16 17 18 19 20   14 15 16 17 18 19 20

17 18 19 20 21 22 23   21 22 23 24 25 26 27   21 22 23 24 25 26 27

24 25 26 27 28 29 30   28                     28 29 30 31

31

         Apr                    May                    Jun

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

             1  2  3                      1          1  2  3  4  5

 4  5  6  7  8  9 10    2  3  4  5  6  7  8    6  7  8  9 10 11 12

11 12 13 14 15 16 17    9 10 11 12 13 14 15   13 14 15 16 17 18 19

18 19 20 21 22 23 24   16 17 18 19 20 21 22   20 21 22 23 24 25 26

25 26 27 28 29 30      23 24 25 26 27 28 29   27 28 29 30

                       30 31

         Jul                    Aug                    Sep

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

             1  2  3    1  2  3  4  5  6  7             1  2  3  4

 4  5  6  7  8  9 10    8  9 10 11 12 13 14    5  6  7  8  9 10 11

11 12 13 14 15 16 17   15 16 17 18 19 20 21   12 13 14 15 16 17 18

18 19 20 21 22 23 24   22 23 24 25 26 27 28   19 20 21 22 23 24 25

25 26 27 28 29 30 31   29 30 31               26 27 28 29 30



         Oct                    Nov                    Dec

 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S

                1  2       1  2  3  4  5  6             1  2  3  4

 3  4  5  6  7  8  9    7  8  9 10 11 12 13    5  6  7  8  9 10 11

10 11 12 13 14 15 16   14 15 16 17 18 19 20   12 13 14 15 16 17 18

17 18 19 20 21 22 23   21 22 23 24 25 26 27   19 20 21 22 23 24 25

24 25 26 27 28 29 30   28 29 30               26 27 28 29 30 31

31

That isn't very interesting on a web page, but you should notice that in the telnet session, the output pauses when the screen is full. Press Space to see the next page.

more isn't the only command that works this way. In fact, all commands can be linked using the pipe operator.

Try thinking of a command as a box with pipes going in and out of it, like so:

more as plumbing

In UNIX terminology, the pipe going in is called Standard Input (or STDIN), the pipe going out is called Standard Out (STDOUT), the gauge on top is called Standard Error (STDERR), and the lever on top is the command you use to start the box in motion. Not all commands use STDIN and STDERR, but every command uses STDOUT if it produces output. The pipe operator simply connects the commands together, like this:

cal and more piped together

All the pipe operator does is join the two commands together.

If you would like to direct output to a file rather than your screen (which is where STDOUT goes, by default), you can say so using another shell operator. Try this:

steel /N/fs1/clwolfe/Steel $ cal 1999 > 1999calendar.txt

The greater-than symbol (>) tells your shell that you would like STDOUT to go to a file, rather than to the terminal or to another program. Likewise, the less-than sign (<) tells your shell to feed the contents of a file to a command as STDIN. This won't work with cal, since cal doesn't use STDIN. We'll use this trick later when working with other commands.

What Happens When you Press Enter

When you press enter, after typing a command line, your shell does the following:

  1. Look for any "shell meta-characters" - things like <,>, and |. Split the command line up using these as markers.
  2. Look for an environment variable, called $PATH, and look in the places it lists for commands that match the names of the command you typed. We'll talk about $PATH more later.
  3. If there are any >'s or <'s, open the named files for reading or writing, accordingly.
  4. Connect the plumbing together, and begin executing the commands.

If something goes wrong, the command will output error messages to STDERR. STDERR by default goes to your terminal, regardless of where STDOUT is going.

Command for Commands

The following are commands that are useful for finding out more about commands.

The man command

The man command is by far the most useful command in UNIX. man is an abbreviation for "manual", as in "user's manual". If you need help with a command, you first instinct should be to type

steel /N/fs1/clwolfe/Steel $ man command

where command is the command that you need help with. For example, if you needed help with the cal command, you would type:

steel /N/fs1/clwolfe/Steel $ man cal

Reformatting page.  Wait... done



User Commands                                              cal(1)



NAME

     cal - display a calendar



SYNOPSIS

     cal [ [ month ] year ]



DESCRIPTION

     The cal utility writes a Gregorian calendar to standard out-

     put.   If the year operand is specified, a calendar for that

     year is written.  If no operands are specified,  a  calendar

     for the current month is written.



OPERANDS

     The following operands are supported:



     month   Specify the month to be displayed, represented as  a

             decimal  integer  from 1 (January) to 12 (December).

             The default is the current month.



     year    Specify  the  year  for  which   the   calendar   is

             displayed,  represented  as a decimal integer from 1

             to 9999.  The default is the current year.



ENVIRONMENT

     See environ(5) for descriptions of the following environment

     variables  that  affect  the  execution  of  cal:   LC_TIME,

     LC_MESSAGES, and NLSPATH.



EXIT STATUS

     The following exit values are returned:

     0         Successful completion.

     >0        An error occurred.



ATTRIBUTES

     See attributes(5) for descriptions of the  following  attri-

     butes:



     __________________________________

    | ATTRIBUTE TYPE|  ATTRIBUTE VALUE|

    |__________________________________

    | Availability  |  SUNWesu        |

    |_______________|_________________|



SEE ALSO

     calendar(1), attributes(5), environ(5)



NOTES

     An unusual calendar is printed for September 1752.  That  is

     the  month  11 days were skipped to make up for lack of leap

     year adjustments.  To see this calendar, type:

          cal 9 1752



SunOS 5.6            Last change: 1 Feb 1995                    1



User Commands                                              cal(1)



     The command cal 83 refers to the year 83, not 1983.



     The year is always considered to start in January.

SunOS 5.6            Last change: 1 Feb 1995                    2

Notice that the man page is automatically fed through more for you.

You may find that the man pages are not very helpful, or are confusingly written. You would be correct. Here is some advice about reading man pages:

  • The Synopsis section is often the most useful part. It gives the basic form of the command, with optional options and arguments in brackets ([]).
  • Many of the other sections are there for the system administrator (the person who keeps the UNIX machine at a more tolerable level of disarray). You usually won't be concerned with the Attributes or Exit Status sections.
  • Read the examples, and try them out.
  • Keep in mind that some of the really old commands (such as tar, for example) have so many modes and features that their man page is indecipherable.

Have a look at the See Also section above. Notice that the commands listed there have a number in parentheses following them. This refers to which section of the man system that the command appears in. Historically, man pages were divided into eight (or more) sections, as follows.

  1. User Commands
  2. System Calls
  3. Subroutines
  4. Devices
  5. File Formats
  6. Games
  7. Miscellaneous
  8. Sys. Administration

So if someone tells you to look at the hostname(1) page, they mean that you should look at the man page for the hostname user command. This is provided so that you could also look at the hostname(3) page, which is the C language system call (not, generally, what you want).

If you would like to see man pages from other sections, use this form of man:

steel /N/fs1/clwolfe/Steel $ man -s 3 hostname

which specifies to only display man pages from section 3, for example.

The apropos Command

Of course, what good is the man command if you don't know what the name of the command is? The apropos command searches through the man pages for a keyword that you specify.

For exmaple, if you wanted to find a command for working with calendars, you would use....

steel /N/fs1/clwolfe/Steel $ apropos calendar

cal             cal (1)         - display a calendar

calendar        calendar (1)    - reminder service

difftime        difftime (3c)   - computes the difference between two calendar times

mktime          mktime (3c)     - converts a tm structure to a calendar time

As you can see from the results, there are four results from the search.

The whereis Command

Often, you may know of a application or command that may or may not be installed on the machine. A good way of finding out is the whereis command. whereis looks for command files in the standard locations, based on the keywords you provide.

steel /N/fs1/clwolfe/Steel $ whereis perl

perl: /usr/bin/perl /usr/local/bin/perl5.00404 /usr/local/bin/perl

This example tells us that the popular program perl is installed in three locations.


Next: Files
Prev: The Shell
Up: Table of Contents