Home Programming Email me
PC Assembly Tutorial
Common C Errors
Debugging Tips
Multi-module Programs
Using Emacs
Blog
Introduction to the UNIX Operating System

Table of Contents



1. Preliminaries

2. Files and directories

3. Redirection

4. Pipes and Filters

5. Wildcards

6. Control keys and processes

7. Useful Commands
  7.1 Equivalent UNIX commands to DOS commands
  7.2 General format
  7.3 The ls command
  7.4 The cat command
  7.5 The more command
  7.6 The cp command
  7.7 The mv command
  7.8 The rm command
  7.9 The cd command
  7.10 The pwd command
  7.11 The mkdir command
  7.12 The lp command
  7.13 The script command
  7.14 The man command

8. Compiling C, C++ and Pascal programs

9. The editors available on the AIX system
  9.1 The pico editor
  9.2 The vi editor
  9.3 The Emacs editor

10. The dbx debugger

1. Preliminaries

UNIX is a popular operating system. There are many different versions of UNIX available. Linux is a very popular UNIX-like operating system and the AIX operating system is IBM's variant of UNIX. Almost all of this document applies to all UNIX's. Outside of the DOS world, UNIX is the most popular operating system. It is hard to find a workstation that does not use UNIX.

UNIX was originally developed in the early 1970's for DEC PDP computers. Today UNIX is used on everything from PC's to supercomputers. UNIX is not particularly user-friendly. It uses many terse commands which can be combined in seemingly cryptic ways; however, it is a very powerful operating system. Dennis Ritchie (one of the original creators of UNIX) compares UNIX to a sharp tool, in novice hands it can be very dangerous, but an experienced user can perform powerful tasks quickly. One of UNIX's unusual features is that it is case-sensitive. All UNIX commands are in lowercase. For example, take the command cat. To use this command, it must be typed in lowercase. Similarly, file names are case-sensitive. The files x.c and X.c are two different files!

2. Files and directories

UNIX files are referenced by their name. A file name may be composed of more than 200 characters. Any character except the slash (/) may be used. Unlike DOS, UNIX file names are not divided into two parts by a period. Often UNIX files have periods in them, but the period has no special meaning to the operating system.

A directory is an object that contains other files. UNIX directories are hierarchical as are DOS directories (in fact, DOS stole this idea from UNIX). One important difference between DOS and UNIX directories is that UNIX directories are separated by the slash character (/), not the backslash (\) as in DOS. The top of the hierarchy of directories is called the root directory and denoted by a single slash (/). Your account has its own directory which is known as your home directory. The tilde (~) always represents your home directory. When you first login, this becomes your current directory. The period (.) represents the current directory; two periods (..) represents the directory directly above the current directory in the hierarchy.

3. Redirection

UNIX allows the input and/or output of a program to be redirected to files. The > and < characters control redirection. To redirect the output of a program named prog into a file named out.dat, use the following:

$prog >out.dat

Everything prog would have printed to the screen instead is written into out.dat. To redirect the input of prog from a file named in.dat, use (don't type the $, this is just the prompt that the AIX has already printed out):

$prog <in.dat

To do both, use:

$prog <in.dat >out.dat

4. Pipes and Filters

A pipe is a connection between two programs that connects the output of the first program to the input of another. A filter is a special kind of program that is primarily designed to be used with pipes. A common example involves the ls command. This command displays all the files in the current directory. If there are many files, some of the names may scroll off the top of the screen before they can be read. To pause the screen after a screenful of data, the more filter can be used. The filter takes as input, the output of ls. At first, it simply passes its input as output to the screen; however, after a screenful is displayed, it pauses and waits for the user to press the spacebar before another screenful is displayed. To tell UNIX to do this, enter:

$ls | more

The vertical line represents the pipe. It tells UNIX to take the output of ls and use it as the input to more.

5. Wildcards

Wildcards allow you to specify concisely an entire group of files that match a pattern. There are two wildcard characters available: the asterisk (*) and the question mark (?). The asterisk matches any number of characters; the question mark matches exactly one character. If the following files are in your home directory:

abc	abd	abe	abcd	acc	bbc

The command ls a* says to list all files that match the given pattern. In this case, all files that start with a, thus every file except for bcc would be displayed. The command ls ab? displays every file that starts with ab plus any other single character. The command ls ?b* displays every file whose second letter is b. DOS uses wildcards in a very similar way. However, there are some subtle differences in how wildcards work in UNIX and in DOS. UNIX replaces each wildcard expression with a list of file names that match the pattern. So, the command:

$ls ab?

is converted into

$ls abc abd abe

The ls command simply echoes these arguments. Realizing UNIX is replacing patterns by lists is particularly important in understanding how to use the cp and mv commands. What does the command below display?

$ls ?b? *c

6. Control keys and processes

A control key is the combination of the Ctrl-key and another key. The Ctrl-key is used like the Shift key. UNIX uses control keys for several purposes. The most common purposes involve processes. A process is an active program managed by the operating system. In a UNIX system, there are many processes. Every program (such as editors, compilers, programs you write) run on the system becomes a process.

It is not uncommon to want to abort a program while it is running. This is very common for student-written programs which often contain bugs. To abort a program, hit Ctrl-C. This should always work for student programs. Other programs (such as editors) may ignore it.

It is also common to be in the middle of one program (such as an editor) and need to run another program. One possibility is to exit the first program and start the second. However, UNIX allows the user to suspend processes. A suspended program is frozen in its current state. To suspend a program, hit Ctrl-Z. A prompt should appear. You may now enter any command you wish. To unsuspend and return to the original process, use the fg command. (Unfortunately, this does not work with the nedt editor.) If you try to logout when suspended processes exist, you will see the following message:

Stopped jobs exist.

Please do not logout with processes suspended, use fg to return to them and exit them normally.

Ctrl-D is used to mark the end of a file from the keyboard. If a program is reading from the keyboard when it is hit, it will stop processing keyboard input (see the script command below). If Ctrl-D is hit at the command prompt, you will be logged out. So, be careful when using it.

7. Useful Commands

7.1 Equivalent UNIX commands to DOS commands

The following table shows a list of DOS commands and their UNIX equivalent.

	DOS		UNIX 

	dir		ls 
	type		cat 
	more		more 
	copy		cp 
	rename		mv 
	del, erase 	rm 
	cd		cd 
	mkdir		mkdir

7.2 General format

Most UNIX commands follow this basic format:

verb -options arguments

where

verb
indicates a program name (like ls).
options
are arguments that begin with a hyphen (-) and contain one or more flags. Flags are single letters that modify the program's behavior. Sometimes flags require their own arguments as well. Remember that UNIX is case-sensitive; the flags -i and -I are different!
arguments
provide information to the program. Often arguments are file names.

7.3 The ls command

The ls command displays file information. It takes a list of files as its argument. This list may be explicit or generated by wildcards. If no list is used, all normal files are displayed. This command has many options, the more useful ones are:

-a
List all file names, including hidden files that start with a period.
-l
Use the long listing format. All the information of the files is displayed, such as, their size in bytes, their protection bits, the last time they were changed, etc.
-x
Arrange the output in columns across the screen
-F
Special files are appended by characters denoting their type. Directories are appended by a slash (/) and executable programs by an asterisk (*). Note that this is a capital F (lowercase f does something completely different!).

A useful form is ls -xF.

7.4 The cat command

The cat command concatenates the files that are its arguments. The files are written to the screen. A common use of cat is to display the contents of a text file on the screen.

$cat x.c

This command prints x.c on the screen.

$cat x.c y.c

This command prints x.c and then y.c on the screen. To actually concatenate the two files into another file requires redirection.

$cat x.c y.c >z.c

This copies x.c and y.c into z.c.

7.5 The more command

If a large file is displayed using cat, much of the file will scroll off the top of the screen. The more command works like cat except it pauses after each screenful of text.

$more x.c

This displays x.c, but a screen at a time. This command is often used as a filter as discussed earlier. If a directory listing is particularly long, the following may be used:

$ls -l | more

When a screen is paused, hitting the space bar will display another screen, hitting the return key will display only the next line, and hitting q will exit and return to the shell prompt.

7.6 The cp command

The cp command makes copies of files. The format is:

cp file1 file2 file3 ... target

where the files are names of the files to copy. The target is either a directory or a file. If target is a directory, the files are copied to it with their original names. The target can only be a file if there is only a single file being copied. In this case, the file is copied and renamed to the target file name. This format allows wildcards to be used to form the list of files to copy.

$cp *.c tmp

This command copies all the files that end in .c to directory tmp. The cp command can not be used to copy and rename a list of files (unlike the DOS copy command).

7.7 The mv command

The mv command renames (or moves in UNIX lingo) files. Its format is identical to cp.

$mv *.c tmp

This command moves all the files that end in .c to directory tmp. Just as for cp, a single file may be renamed, but a list of files can not be.

7.8 The rm command

The rm command deletes (or removes a link in UNIX lingo) a file. Its only argument is a list of files to delete. Be very careful with this command!

$rm *

This command deletes every file in your directory! You will not be asked if you really wish to do this! If you do want to be asked used the -i option for rm.

$rm -i *

This command asks if you wish to delete each file. Hit y for yes or n for no.

7.9 The cd command

The cd command changes the current directory. It works exactly like the DOS cd command.

7.10 The pwd command

The pwd command displays the full path to the current directory. It takes no arguments.

7.11 The mkdir command

The mkdir command makes a new directory. It works exactly like the DOS mkdir command.

7.12 The lp command

The lp command prints files on the system line printer. Its format is exactly like cat's. Only print text files on the printer! Try using more on any files before you print them out. If it does not look right on the screen, it will not look right on the printer. Executable programs like a.out are not text files!

7.13 The script command

The script command copies all text written to the screen into a file. The text is still written to the screen unlike when the output is redirected. This command should be used to turn in printouts of code and program runs. All output is stored in the file until a Ctrl-D is pressed, indicating end of file. (Do not worry, you will not be logged off unless you press Ctrl-D a second time.) By default, the output is stored in a file named typescript. However, if a file name is used as an argument, the output is stored in that file. Do not use any editor (or other full screen program) while script is active! This will put control characters into the script file that the printer can not handle!

Here's an example session:


$script                # start script, output goes into file typescript
$cat prog.c            # display prog.c on screen (and into typescript)
$xlc prog.c            # compile prog.c
$a.out                 # run the executable program
$Ctrl-D                # hit Ctrl-D to stop scripting
$more typescript       # examine file on screen (Does it look ok?)
$lp typescript         # send to line printer

7.14 The man command

UNIX has on-line documentation of all its commands. The man command accesses this online manual. The argument to man is the name of the command. The man pages for this command will be displayed using more. To find out more information about any of the commands presented here or others not discussed, use man.

8. Compiling C, C++ and Pascal programs

The standard name of the UNIX C compiler is cc. The AIX system does include this compiler; however, it is a K&R C compiler, not an ANSI C compiler. The ANSI C compiler is called xlc on the AIX system. Students are required to use this compiler. The xlc compiler catches more errors that the cc compiler. The C++ compiler is called xlC and the Pascal compiler xlp. Almost all of the options of the compilers are identical. The general format is:

compiler_name -options source_files

where

compiler_name
either xlc, xlC or xlp
-options
is a list of the options listed below.
source_files
is a list of file names. C files must end in .c, C++ files in .C and Pascal files in .pas.

The compiler command will compile the source files and link the resulting object files. The options for the compilers are:

-c
compile only, do not link. Object files (with a .o extension) are created.
-g
include debugging information. (This flag is required to use dbx.)
-o name
Defines name of executable file to name. If omitted, the executable is called a.out.
-I path
Defines a directory path to search for non-system header files. Pascal can use header files. One difference between the compilers is that both xlc and xlC search the current directory automatically for header files, xlp does not. To have xlp search in the current directory use -I. as an option. (Remember that a period represents the current directory.)

9. The editors available on the AIX system

There are four editors available on the AIX system for editing text files.

9.1 The pico editor

The pico editor is the same editor that the pine mail program uses. It is very simple to use.

9.2 The vi editor

The vi editor is the traditional text editor for UNIX. All UNIX's have it. However, vi is awkward to use if you are used to DOS editors. The vi editor is modal. That is, it has two modes: control mode and input mode. To use vi, you must be constantly aware of which mode it is in. Many UNIX books explain how to use vi.

9.3 The Emacs editor

The Emacs editor is not a modal editor like vi. It operates more like a DOS editor. It is more powerful than the other editors. Most UNIX systems have Emacs and is available for many other systems (such as Win95/NT).

10. The dbx debugger

The UNIX operating system supplies a debugger named dbx. This debugger is in some ways more powerful than PC debuggers (such as Turbo Debugger). A basic knowledge of dbx can save lots of time and hassle.

To effectively use dbx, your program must be compiled so that debugging information is stored in the executable program. The -g switch tells the compiler to do this. For example:

xlc -g -o prog prog.c

compiles a C program and puts the debugging info into prog. (dbx works with C++ and Pascal too.)


Maintainer: Paul Carter ( email: pacman128@gmail.com )
Copyright © 2002 All Rights Resevered
Last Updated: Thu Apr 25 14:29:24 CDT 2002