3.5 Using the Command Line

To use a command line interface (as opposed to a graphical interface like X Windows), you need to run a special kind of program called a shell. What is a shell? It is a program which lets you use a terminal or terminal window as a place to type commands. By far the most commonly used shell in the Unix world is the so-called C shell. By default, every terminal you log in to and every X terminal window in X Windows will execute a C shell, so it is a good idea to know a little bit about this shell. By reading this page, you can learn about:

How to Start an X Terminal

Find an existing X terminal (an icon or window titled xterm), and type:
xterm &
and press Return. A new window should appear, with a prompt which looks something like:
(myname@gamba)
Whenever you see this prompt, you can type commands into the window. First make sure the window is active, however.

If you are using the Motif window manager, find a piece of exposed root window and click the right mouse button to bring up the root window menu. Then, select New Window (which really is a misnomer, and should be called "Launch X terminal"). Just as above, new window should appear, with a prompt which looks something like:

(myname@gamba)

Displaying and Changing Directories

A typical directory /home/ugrad/myname might look something like this:
MyFile.txt   Picture.gif  Dir2     Stuff.doc
This example directory has four files in it, of which one is a directory in its own right. To see a listing of this sort, you must use the ls command (short for 'list'). Make a terminal window the active window on your display, and at the prompt type ls and then press RETURN.

The directory which the ls command shows you is the shell's idea of its current directory (also called working directory). Each shell has a current directory to help you avoid typing the same directory name over and over. To find out the name of the current directory, type pwd (Print Working Directory) and press RETURN.

You can make any directory become the current directory, by using the change directory command, cd, like this:

	cd Dir2
Simply give the name of a directory as a command line argument, and cd moves your shell into that directory.

Naming and moving files

In Unix, filenames can be practically anything, but it is a good idea to use mostly alphanumeric characters, and to avoid embedding spaces. You can embed spaces if you wish, but it will make using the command line interface a bit trickier. It is also a bad idea to begin a filename with the minus sign character: '-'.

To move a file, use the mv command. For example, to move Project.m up one directory from the current directory, type:

mv Project.m ..

You can also move directories this way.

Renaming a file is just a special case of moving a file, where you give the new filename. For example,

mv Project.m Junk

renames the file Project.m to Junk, and leaves it in the current directory. If a file named Junk already exists, this can overwrite it, so be careful!


Removing files

To erase a file, use the rm command (think "remove"). For example,

rm Junk

tells Unix to find the file named Junk in the current directory, and erase it. A word of warning is in order here for those used to the Macintosh trash can and the undelete commands available in MS-DOS: There is absolutely no way of recovering a Unix file that has been deleted using the rm command!

You cannot remove a directory in exactly the same way as an ordinary file, but instead, the command rmdir allows you to remove an empty directory. You must remove all the contents of the directory yourself.


Running programs from the command line

It bears repeating that everything in Unix is a file. A program is a file. To execute a program from the command line, simply type the name of its file at the prompt. For example, the ls command is a file, named ls. When you type the command ls at the prompt, you are simply telling the shell to go look in the file named ls and do whatever it says there to do.

Search path

The shell remembers a search path, a list of directories in which commands are usually found, in order to save you from extra typing. For example, the ls program is actually located in the file /bin/ls, but you do not have to type the full pathname, since your shell knows that the /bin directory is commonly used for storing programs. For most users, the default search path will suffice. However, it can be customized.

Arguments

A command may be designed to respond to extra information typed after its name. For example, to list only the file Map.txt, you could enter
ls Map.txt
The filename Map.txt is an argument to the ls command. Each command has a set of allowed arguments, with special meanings for that command. ls takes any number of filenames and directory names and lists them. This is in contrast to the default behaviour of ls, which is to list the entire current directory.

Each argument must be separated from the other arguments--and from the command name--by spaces and/or tab characters. This is how the shell determines the end and beginning of each argument.

Command line switches

Many commands modify their behaviour according to certain special arguments, known as command line switches. Such switches follow immediately after the command name, before any other arguments, and are most often a single letter, preceded by a dash (minus sign). For example, to list the file Map.txt in a long format, use the switch -l like so:
ls -l Map.txt
Notice how the switch comes before any other argument.

Some commands allow you to group switches behind a single dash, like ls -FCd. Some do not. It is necessary to read the manual page in order to be sure. While this sounds confusing, it rarely causes problems once you get used to it.


Keith Orpen, who is still writing this, would like to hear your comments and suggestions.