UNIX

UNIX offers a wide range of useful commands, and although there are far too many for you to remember, this section provides you with a description of some of the basic ones you will likely need to get started.

Many commands have several options that can be selected. A command option is specified by giving the command name, followed by a minus sign, followed by the option. For example, some of the possible options of the ls command, described later, include ls -a and ls -l. Options can be combined, either by typing them separately, as in ls -a -l or in certain situations, together, as in ls -al.

Changing your password

On any system in which your initial password is an easily guessed name, for example, your student number, it is a very good idea to change this as soon as possible. The command to do so is passwd.

More Help

You can get on-line help about almost any UNIX command and many of the library functions you will use in your programs. The help command is man for manual pages. If you know the name of the command you want described (let's call it command, then type man command. If you don't know the name of a particular command, man -k keyword will provide you with a list of commands that deal with the named keyword. For example, if you wanted to find out the appropriate command to count the number of words in a certain file, you could type man -k count. The resulting list of items will contain a brief description of each possibly relevant command. Of course, once you know the name of the desired command, you can find out more details about it, and how to interpret its output, by typing man command.

Navigating the filesystem

All of the files on a UNIX system are stored in a directory tree. The root of the tree is called /. If your userid is zachary, then your home directory is probably located somewhere such as /home/users/zachary. When you first login to the system, you are automatically placed in your home directory.

To view the contents of your current directory, type ls. You probably won't see anything yet if you haven't created any files, but try typing ls -a to list all files. Now you will see a number of files, each of them beginning with a dot. Any file whose name begins with a dot is considered a hidden file and will only appear in a directory listing when you request all files to be shown.

You might notice two unusual entries, . and .. in this list. The . refers to your current directory and the .. refers to the parent of your current directory. You could type ls /home/users to see the list of files in the parent of your home directory, but a simpler way to do this is to type ls .. from your home directory.

In UNIX, every program, text document, data file, and so on, is saved as a file. Even directories are considered files, themselves. When you use ls, you will see any (non-hidden) files in the directory, including any sub-directories within it. For example, type ls /home/jer/robocup to see the robocup sub-directory in your instructor's account. Unfortunately, this listing does not distinguish between ordinary files and directories. Try ls -F /home/jer/robocup. This time, all of the sub-directories appear with a slash (/) at the end. You will notice that some of the files appear with an asterisk (*) at the end. Any such file is an executable, which means that you can run it, simply by typing its name. If you like this format of file listings, then once you are familiar with one of the editors, you can make this a default by adding the following line to the end of your .cshrc file:

alias ls 'ls -F'
Regardless of where you are currently located in the directory tree, you can refer to your home directory by $HOME or the ~ character. You can also refer to someone else's home directory by ~userid. For example, another way to see the files of your instructor's robocup sub-directory would be to type ls ~jer/robocup.

Another useful option of the ls command is the long format listing, obtained with ls -l. This provides you with the file size, ownership, last modification date, and permissions field of each file. See man ls for more details.

To change directories, use the cd command. For example, to go into your instructor's robocup sub-directory, type cd ~jer/robocup. Typing cd by itself will always put you back into your home directory. If you want to know where you are in the directory tree, you can type pwd for "print working directory."

The remaining commands in this section are listed by function with no explanation. The first two commands are explicitly for directories. The remaining commands work on individual files, groups of files, or entire directories. For more details, consult the appropriate man page.
mkdir make a new directory
rmdir remove a directory
rm remove file(s)
mv move (rename) file(s)
cp copy file(s)
chmod change permission of file(s)
Note that once you remove a file, it is really gone. Although you may be able to recover an older version of a file if it has been backed up by the system (check with your local systems administrator for this), this will not be the case for any files only recently created. Use rm with extreme care.

Viewing files

There are a number of commands you can use to look at files. The simplest, cat, dumps the contents of the named file directly to the screen. The cat command is actually short for concatenate and display, and can be used to concatenate multiple files. If you were to type cat file1 file2, then the contents of file1 would be dumped to the screen, followed by the contents of file2. All you need now is some way to have that output go into another file rather than to the screen. This can be accomplished by using the UNIX redirection operator, > as in cat file1 file2 > file3. Now, file3 will contain the contents of file1, followed by the contents of file2.

A problem with the cat command is that if the file contents do not fit in one screen, then the display will quickly scroll and you will not have a chance to see the beginning of the file. The page, more, and less commands display one page of a file at a time, and offer additional features, such as page skipping and scrolling backwards (less only).

To just view the first few lines or last few lines of a file, you can use the head and tail commands. Both of these commands take an optional argument specifying the number of lines to show, for example, tail -15 file1 will display the last 15 lines of file1.

Sometimes, when you are writing programs or documentation, you may need to determine what has changed from one version to another. You can use the diff command to list the differences between two files. Another useful command is grep, which locates occurrences of any character, word, or string of text in a file. To find out more about these commands, see the appropriate man pages.

You may want to print program listings, data files, and documentation for your own use during the course, and you will certainly need to do so when an assignment requires you to submit a hardcopy. The command to send a file to the printer is called lpr. Again, there are many options for this command, which you can learn about by typing man lpr.

Specifying multiple files

If you want to specify that a command should act on a list of files rather than a single file, you can use the * and ? wildcards. The * will match any group of characters and the ? will match any single character. For example, if you typed cat *.c, then all of the files whose names end with ".c" would be displayed on the screen. Be careful with wildcards. Imagine what would happen if you typed rm *.

Redirection and pipes

As you saw above, we can use the output redirection operator (>>) to cause command output to go to a file rather than to the screen. Similarly, you can use the input redirection operator (<) to supply input to a command from a file, rather than from the keyboard. For example, when sending mail with the UNIX mail command, you would normally enter the body of your letter from the keyboard. However, if you prefer, you can use an editor to prepare your letter beforehand, and then type mail address < file to send the file directly.

Another important operator is the pipe (|). A pipe allows you to pass the output of one command to the input of a second command. For example, you could type diff file1 file2 | more to pass the output of the diff command through the more viewer, so that the output doesn't scroll off the display.

Editors

There are several editors available on UNIX systems, the two most popular being vi and emacs. You can learn how to use vi on the ECE system by typing:
vi ~jer/unix/vitutor
or emacs by typing:
emacs
and then selecting the tutorial from the help menu.

C

C Compilers

The default C compiler on most UNIX systems is called cc. The GNU C compiler, which by default expects ANSI C, is also available as gcc. ANSI C is more strongly-typed than the original C, and will likely help you catch some bugs earlier during coding. It is strongly suggested that you always use the gcc compiler.

Compiling programs with cc or gcc

All of the source files for your program must have a name ending in ".c". You may also wish to include header files, containing various constants, macros, and data structure definitions, in your program. Each such header file must have a name ending in ".h".

Suppose you are writing a program to print a message to the user, and have decided to split the program into two source files. Your main source file might be called "prog.c" and the other source file might be called "sub.c". Additionally, let us suppose that these two source files need to share a data structure definition which is contained in the file "incl.h".

To compile your program, you may simply type:

gcc prog.c sub.c 
which will create an executable program named "a.out." If you prefer, you may specify the name of the executable, for instance, "pgm", as follows:
gcc -o pgm prog.c greet.c 
If you are re-compiling your program frequently, this method is inefficient, especially if you are only making changes to one source file at a time. Instead, you should compile the program as follows:
gcc -c prog.c 
gcc -c sub.c 
gcc -o pgm prog.o sub.o 
The first two lines create object files "prog.o" and "sub.o" and the third line links the objects together into an executable. If you were then to make a change to the "sub.c" file, you could re-compile your program by just typing the last two lines.

This all might seem a little silly for the example above, but if you had ten source files instead of two, the latter method would save you a lot of time. Actually, the entire compilation process can be automated by using a makefile, for example:


# Any line beginning with a `#' sign is a comment and will be
# ignored by the "make" command. To generate the executable
# programs, simply type "make".

# This makefile says that pgm depends on two files prog.o and
# sub.o, and that they in turn depend on their corresponding
# source files (prog.c and sub.c), in addition to the common header
# file, incl.h:

# Note that each of the gcc directives MUST be preceded by a
# tab character (not a number of spaces) for the make to work.

pgm: prog.o sub.o
	gcc -o pgm prog.o sub.o
prog.o: incl.h prog.c
	gcc -c prog.c
sub.o: incl.h sub.c
	gcc -c sub.c