Computer Science III - 22C:030/22C:115
Department of Computer Science
The University of Iowa

An Introduction to the UNIX Operating System

by Aditya Sehgal

What is UNIX?

"UNIX is a concurrent multitasking, multi-user computer operating system. Multitasking means that more than one user-program can be run at once. Concurrent multi-user simply means that more than one user can use the machine at the same time." (McMullen, 1999)

 

Logging In

The first step on in a UNIX session is always logging in. Unlike PC machines, which are generally single user systems, UNIX allows more than one user to operate the system (most of the time concurrently). Hence, to distinguish between different users each user must separately identify himself/herself to the system. Assigning each user a name and a password does this. Generally user-names and passwords are case sensitive.

Each user on a "UNIX system" is assigned separate directories to maintain privacy of files. A user is also assigned some permissions. A normal user is not allowed to view and modify files belonging to other users. There is always an all-powerful administrator, who takes care of the system and sees to it that no user misuses the system.

After entering your user-name and password the system tells you when you last logged on and logs you into the system. Once you have logged in the system takes you to a prompt (usually a "$" or "%" sign). This means that the system is now ready to take commands from you. You are placed in a default directory. This is directory is called your "home directory". The program you use to give commands to the computer is called the "Shell". You are allowed to create files and store them in only your default folder. You can delete only the files that are present in your default folder.

 

Shell

Whenever youâ™re using UNIX, youâ™re using a shell program. It is called a shell because it surrounds the kernel (or the core) of the operating system protecting you from the intricacies of the operating system. A shell does the following things from you:

  1. It interprets your commands.
  2. It stores information for you.
  3. It runs programs for you.

These are only some of the things a shell does. Many shells have many more functions than mentioned above.

Using the command line

Unlike the Windows operating system, the UNIX operating system is a command line operating system+. This means that it does you do not use a mouse to do things in UNIX. UNIX commands have to be typed after the UNIX prompt and then they are executed by the operating system.

 

Basic Commands

  1. Identify your current directory.
  2. When you log into a UNIX system, you are given a working directory. You are "in"
    a particular directory at all times. Whenever you give a command, the UNIX system tries to find that command in the directory you are in first and then elsewhere.

    The command you use to identify the directory you are currently in is pwd (print working directory).

    For example:

    $ pwd

    /home/mike

    The working directory is /home/mike, which means the directory mike inside the directory home.

  3. Change Directories
  4. You can change your current directory by using the cd (change directory) command.

    For example:

    $ pwd

    /home/mike

    $ cd /home

    $ pwd

    /home

    The command cd .. moves you to the parent directory of the current directory.

     

  5. List the contents of a directory
  6. You can see what files are in the current directory by using the ls (list) command. To see what files are in another directory, type the directory name after the ls. Normally the ls command does not distinguish between files and directories.

    For example:

    $ ls

    abc.txt a.cpp test.cpp mike.txt

    We can see that the current folder contains four files.

  7. Creating a directory
  8. The UNIX command for creating a directory is mkdir (make directory).

    For example:

    $ ls

    abc.txt

    $ mkdir docs

    $ ls

    abc.txt docs

  9. Deleting a file
  10. The command rm (remove) deletes a file. You have to give the name of the file after the rm command.

    For example:

    $ ls

    abc.txt docs

    $ rm abc.txt

    $ ls

    docs

  11. Deleting a directory
  12. The rmdir (remove directory) deletes a directory.

    For example:

    $ ls

    abc.txt

    $ mkdir docs

    $ ls

    abc.txt docs

    $ rmdir docs

    $ ls

    abc.txt

  13. To see the contents of a file

The cat command is used to see the contents of a file. If the file is not a text file then generally garbage is displayed. The is the UNIX counterpart to the DOS type command.

The cat command is pipelined with more command to display text one page at a time.

$ ls

abc.txt accounts.txt

$ cat abc.txt

â¦â¦â¦â¦â¦â¦â¦â¦â¦..

â¦â¦â¦â¦â¦â¦â¦â¦â¦.. (Contents of abc.txt)

â¦â¦â¦â¦â¦â¦â¦â¦â¦..

$ cat accounts.txt | more

â¦â¦â¦â¦â¦â¦â¦â¦â¦.

â¦â¦â¦â¦â¦â¦â¦â¦â¦. (Contents of accounts.txt, one page at a time)

â¦â¦â¦â¦â¦â¦â¦â¦â¦.

Compiling C++ programs

The C++ compiler generally included in all UNIX systems is the GNU g++ compiler. The g++ compiler is a command line compiler i.e. it does not have an IDE (Integrated Development Environment). The C++ file (which is to be compiled) is given as an argument to the g++ command. The file is compiled to produce an executable file. You can then run this executable file.

For example:

$ ls

test.cpp ("hello world" program)

$ g++ text.cpp

$ ls

test.cpp a.out

$ ./a.out

hello world

Reference: UNIX User's Interactive Workbook, by John McMullen.


Computer Science III (22C:030/22C:115)
Instructor: Prof. Bryant Julstrom
Teaching Assistant: Aditya Sehgal

The VI text editor

The vi (visual) editor (pronounced as vee-eye) was the first visual editor on the UNIX operating system. Before vi all the editors on UNIX were line editors, which displayed text one line at a time (the editor ed is one such editor). The vi editor is actually a visual editor built on top of a line editor (the line editor version is ex).

The vi is considered to be a very powerful editor and has a lot of functions but because vi has so many different functions, it becomes difficult for one to remember all the commands and also the vi commands are very different from the magic key commands (e.g., ctrl-D) people are generally used to. That is why vi is not one of the popular text editors on UNIX. Another editor that is found on most UNIX systems is emacs.

The vi is also called a mode editor. This means it has different operating modes. The vi editor has two modes as follows:

To switch from input mode to command mode, press thei Escape key. There are several commands to switch from command mode to input mode. Each command inserts the text at a different location (beginning of a line, end of a line etc.).

Some commands in vi are letters; these are indicated by the instruction "type the command". Other commands must be finished by pressing Enter; these are indicated by the instruction "Enter the command".

 

Starting a vi session

To start vi, type vi at the command prompt and press Enter. The vi editor is started with a blank file. This file has no name and has to be given a name before the user quits vi. An alternative to this is to give a name to file when running vi from the command. For example,

$ vi abc.txt

will create a file abc.txt and open the file in vi for the user to edit. Empty lines are indicated by the ~ character. When vi is started, it opens in command mode.

To quit vi (unconditional), press Escape to go to command mode and type :q! and press Enter. This takes you to the command prompt.

 

Basic vi commands

  1. Insert Text ⓠTo insert text into a file, ress the Escape key to go into command mode and press "i". This takes you into insert mode. You can now enter new text. New text is inserted before the cursorâ™s current osition.
  2. Pressing "a" instead of "i" inserts text after the cursor's current position.

    Pressing "I" inserts text at the beginning of the line.

    Pressing "A" appends the new text tot the end of the line.

    Pressing "o" inserts the new text on a new line before the current line.

    Pressing "O" inserts the new text on a new line after the current line.

    vi does not insert line breaks for you when typing, you have to press the Enter key yourself.

  3. Moving the cursor
  4. The following commands move the cursor on the screen:

  5. Deleting Text
  6. Saving a file
  7. Undoing Changes - To undo any changes made to the file, use the "u" command. Pressing "u" twice undoes the first "u" command.

Reference: John McMullen: UNIX User's Interactive Workbook. Upper Saddle River, NJ: Prentice-Hall, 1999.