UNIX


References

A number of UNIX tutorials, references, and summaries appear on the World Wide Web, including the following:

Introduction

A computer system consists of application programs, such as a word processor or spreadsheet; hardware, such as a central processing unit or mouse; and systems programs, such as a compiler, editor, or operating system. An operating system (OS), which is always running on a computer which is powered, is a collection of programs that forms the interface between the user and the hardware. The main goals of an operating system are the convenience of the user and the efficient operation of the computer system. For example, with the former goal the OS provides a high-level abstraction of the operation of a disk drive. The user issues a simple command to save a file, and the operating system manages the low level details, such as dealing with the disk drive's error status flags. As an example of the second OS goal of efficient computer operation, if several jobs need to print at the same time, the OS allocates one job to the printer and buffers the others until the first is complete.

The UNIX operating system has its roots in the MULTICS operating system. In the third generation of computers, approximately between 1965 and 1980, MIT, Bell Laboratories, and GE had a joint project to develop a computer utility, MULTICS, to support hundreds of simultaneous users on different machines. Although the project had numerous seminal ideas, the tasks were difficult, and the project eventually died. Ken Thompson, a computer scientist at Bell Laboratories who had worked on the MULTICS project, wrote a smaller, one-user version of the MULTICS OS. This operating system eventually evolved into UNIX.

Because of its ability to facilitate communication, UNIX is the OS on many of the computers that drive the internet. Many of the features of other microcomputer operating systems derive from UNIX. UNIX is a multiprocess environment in which we can have several jobs running at the same time. Moreover, UNIX enables us to use distributed programming, where we can allocate the tasks of a program over several networked computers. For example, a database might be stored on several UNIX computers. Because many scientific facilities employ this important operating system on its computers, we find it useful to study and use UNIX in our study of databases in computational science.


Command Basics

UNIX is case sensitive, so that ls is not the same as LS or Ls.

To display the previous command entered, we press the up arrow. We can then make revisions to the command and press return (<RETURN>) to execute. Pressing the up arrow several times takes us back the corresponding number of commands. The down arrow takes us forward in this historical command list.

Often, we do not need to type every letter of a command because UNIX offers command completion. If we begin a command and then press the tab key (<TAB>), UNIX completes the command if possible.

Quick Review Question

Quick Review Question 1Choose what to type to view the next-to-last command.  

<TAB><TAB> 2<TAB> <RETURN><RETURN> 2<RETURN> none of these


Account Access

To logout of a UNIX OS, we type logout, exit, or ^d (control-d or hold the control and d keys down at the same time).

To change the password on the account, we type passwd and follow the directions.

Quick Review Question

Quick Review Question 2Choose all the ways to logout. 
^c ^d exit goodbye logout ^z


Help

An online manual, man, is available with UNIX. For example, to display the manual page on the ls command, we type the following:

man ls

If we do not remember the exact command, we can employ apropos. For example, to discover all commands related to the keyword list, we type the following:

apropos list

To view the current users on the system, we use the command finger.

Quick Review Question

Quick Review Question 3Give the UNIX command for
a. Viewing the online manual

b. Obtaining a list of commands related to a keyword


c. Viewing current users



Directories

UNIX supports a directory structure with a forward slash (/) between a directory and a subdirectory in a path name. Thus, usr/home/ken is the path name from the directory usr to the subdirectory home to its subdirectory ken. A notation for the current directory is one period (.); and the directory above the current directory, the parent directory, is two periods (..). The home directory is ~.

To create a directory, we use the make directory command, mkdir. If we are in the directory usr/home/ken, the following command creates a subdirectory data with the full path name usr/home/ken/data:

mkdir data

To remove a directory that is empty, we employ rmdir followed by the directory's name.

To have UNIX list the present working directory, we issue the command pwd. If we are in the directory usr/home/ken and we wish to move to the subdirectory data, we change directory with cd, as follows:

cd data

To move to the home directory, we type cd by itself, as follows:

cd

Quick Review Question

Quick Review Question 4Type only one blank between components of a command.
a. Give is a command to move to the home directory.
cd changedir goto home main root  select


b. Suppose we are currently in directory ken/data/psy. Using the full path name, write a statement to change to directory ken/data/bio.


c. Change from directory ken/data/psy to directory ken/data/bio by using two commands. First, go to the parent directory and then to its bio subdirectory. Use as few directory names as possible.


d. Write a command to create a subdirectory called physics.


File Manipulation

To list the files in the current directory, we can type ls. For more complete information about the files, such as the read, write, and execute privileges for the user, group, and world, we add the option -l, as follows:

ls -l

To specify the above privileges for a file, we employ the change mode command, chmod. The creator can specify read (r), write (w), and execute (x) privileges for him or herself (u), members of his or her group (g), and everyone (a). To change the mode for a file, after chmod a plus sign (+) appears between the member and the privileges; the name of the file follows. For example, for the creator to specify read, write, and execute privileges for the file exp.dat for him or herself, the creator enters the following command:

chmod u+rwx exp.dat

To allow the world only to read and execute the file, we employ the following command:

chmod a+rx exp.dat

We use a minus sign (-) instead of a plus sign to take away privileges.

The copy command, cp, makes a copy, leaving the original, while the move command, mv, removes the original version. To make a copy of file orig by the name copy, we type the following:

cp orig copy

To rename orig as NewFile and remove orig, we issue the following command:

mv orig NewFile

We can also use mv or cp to move or copy, respectively, a file from its current location to another directory. For example, if file exp1.dat is in usr/home/ken, our current directory, we can move the file to the subdirectory data by following the directory name with a forward slash, as follows:

mv exp1.dat data/

To move or copy all the files in a directory, we use the wild card, *, in place of the file name, such as in the following move command:

mv * data/

To remove one or many files, we employ the command rm For example, to delete exp.dat, we type the following:

rm exp.dat

However, we should be very careful in making such drastic deletions.

To display a file on the screen in a streaming fashion, we employ the concatenate command cat. Thus, to see the contents of exp.dat, we can type the following:

cat exp.dat

If the file is large, we may choose to display its contents one page at a time with more, such as follows:

more exp.dat

We can redirect output from one command or executing program to a file using the redirection operator, >. For example, suppose atomic is the name of an executable program file. We can execute atomic and redirect the output of this program from the screen to a file, atomicOutput.txt, with the following command:

atomic > atomicOutput.txt

Besides displaying, manipulating, and moving, we may wish to print a file. To print the file exp.dat on the default laser, we employ the command lpr, as follows:

lpr exp.dat

To specify a particular printer, such as pr1, the option -P appears before the name of the printer; and the name of the file completes the command, which follows

lpr -Ppr1 exp.dat

Quick Review Question

Quick Review Question 5Type only one blank between components of a command.
a. Select all commands that will display the contents of a directory.
dir directory list ls ls -1


b. Write the simplest command to move file one.txt into subdirectory sub.


c. We can do Part b in two steps, by first copying one.txt into subdirectory sub and then deleting one.txt from the current directory. Write this copy command.


d. Write the command to delete one.txt from the current directory.


e. Give the command to delete one.txt from the subdirectory sub.


f. Give the command to remove all the files from the current directory:


g. Give the command to remove read privileges for everyone else on files ending in .dat in the current directory.


h. Give the command to display in a streaming fashion the contents of all the files in the current directory.


i. Give the command to print the file my.cc on the laser printer sci.


vi Editor

Many editors for creating text files are available with UNIX. The emacs editor is extensive and widely used, but one of the easiest editors is vi. To create a file or edit an existing file named seq.cc with vi, we type the names of the editor and the file, as follows:

vi seq.cc

The editor has two modes, command mode and edit mode. We start in command mode. If we are in edit mode, we can move to command mode by pressing the escape key (<ESC>).

To save our work, in command mode we type the write command, :w. If we are editing, which is in edit mode, we must remember to press <ESC> before issuing the write command. The write and quit command, :wq or ZZ, saves the file and quits vi. If we have made changes since the last save that we do not want to save, we can quit emphatically with :q! to not save the changes.

To enter edit mode from command mode, we must indicate where to place the text that we are about to type. The append command, a, appends the text after the cursor, while the insert command, i, inserts typing before the cursor. To open a blank line after the cursor, we employ o; and to open before, we use O. After we are through entering the text, we must remember to press <ESC> to return to command mode.

Deleting a single character from the command mode, we place the cursor over the character and type x. The delete word command is dw, and the command dd deletes an entire line. To repeat a command several times, we have the number of times as a prefix. For example, to delete three lines, we type 3dd.

Copy and paste are accomplished with the yank and put commands, respectively. The yank command Y or yy followed by <RETURN> yanks or copies the current line to the clipboard. To yank the next word only, we type yw. To put before the cursor what is on the clipboard, we type an uppercase P. Typing a lowercase p will put after the cursor the yanked contents.

Two undo commands are useful. The command to undo the last change is u, and U will undo all changes on a line.

As well as moving the cursor with the arrow, page up, and page down keys, we can go to the beginning of the current line by typing zero (0) or to the end of the line with a dollar sign ($). The command G makes us go to the end of the file, while a number before G takes us to that particular line. For example, to move to line 15, we type 15G.

To search forward in the file for particular text, we type a forward slash (/) and the search text and press the return key (<RETURN>). Thus, to search for "Anopheles mosquito", we press <RETURN> after typing the following:

/Anopheles mosquito

To continue the search, we type another forward slash (/) and press <RETURN>. If we wish to search backwards in the file instead of forwards, we use a question mark (?) in place of the forward slash.

Figure 1 presents a summary of these and several additional vi commands.

 

Figure 1 vi commands

Command

Meaning

$

Move to the end of the current line

/text <RETURN>

Search forward for text

/

Repeat search

:q!

Quit without saving

:w

Write

:wq

Write and quit

?text <RETURN>

Search backward for text

0 (zero)

Move to the beginning of the current line

a text <ESC>

Append text after cursor

dd

Delete line

dw

Delete word

G

Go to the end of the file

nG

Go to line n

itext <ESC>

Insert text before cursor

nCommand

Repeat Command n times

otext <ESC>

Open a blank line after the cursor with text

Otext <ESC>

Open a blank line before the cursor with text

p

Put text in clipboard before cursor

P

Put text in clipboard after cursor

u

Undo the last change

U

Undo all changes on a line

vi filename

Edit filename

x

Delete character

Y

Yank current line to clipboard

yw

Yank next word to clipboard

yy

Yank current line to clipboard

ZZ

Write and quit



Quick Review Question

Quick Review Question 6This question refers to the vi editor.

a. Check all the commands that cause the system to save the current file and quit vi.
:q :q! :w :wq :ZZ q q! w wq ZZ


b. Check all the commands that allow the user to enter edit mode to place text into the document.

a b c i o O p q s t u b w x y


c. Check all the commands that allow the user to delete text.

c d dd del ds dw 5d d5 d5d p u w x xw y


d. Select the command to move the cursor to the beginning of the current line.

b B e E f F 0 1 $ & *


e. Select the command to move the cursor to the end of the current line.

b B e E f F 0 1 $ & *



f. Give the command to move to line 5.


Program Execution

One of the compilers for C and C++ programs is gcc. Suppose velocity.cc is the source file for an entire C++ program. The following gcc command with option -c causes gcc to compile the program and create an object file called velocity.o:

gcc -c velocity.cc

To link the object file velocity.o with the appropriate library function files into an executable file called velocity, we use the gcc command with option -o, as follows:

gcc velocity.o -o velocity

In the following, we compile and link with one command using the -o option with the source file:

gcc velocity.cc -o velocity

To execute the program, we enter the name of the executable file, here velocity. To run the program in the background, an ampersand (&) should follow the executable file's name, as in velocity&.

If a program is contained in several files, say calc.cc and sub.cc, we can compile each separately:

gcc -c calc.cc

gcc -c sub.cc

Then, we can link the object files to the appropriate library function files to form an executable file, say calc, as follows:

gcc calc.o sub.o -o calc

Alternatively, we can compile and link the .cc files with the following command:

gcc calc.cc sub.cc -o calc

As with programs whose source is in one file, we type the name of the executable file, here calc, to execute the program.

Sometimes, we wish to save everything that appears on the screen for later reference. For example, we might want to record all input to and output from execution of an interactive program. To start saving in a log file, say called mylog.txt, we employ the command script, as follows:

script mylog.txt

To terminate such saving, we type exit or ^d. We can then view the log file with an editor, display the file on the screen with cat or more, or print the file.

Quick Review Question

Quick Review Question 7.
a. Give the name of the C and C++ compiler of this section.


b. Give the gcc option to compile a source file.


c. Give the gcc option to link object and library files to form an executable file.


d. Give the command to log everything that appears on the screen in a file called log0702.txt.


Mail

The command line mail program available on most UNIX systems is mail, which we begin by typing mail. To obtain help in using the system, we can type help from within mail. The command q results in an exit from the mail program.

To send an email message to shifletab@wofford.edu, we type the following from either inside or outside the mail program:

mail shifletab@wofford.edu

We specify the subject and type the message. Upon completion, we press ^d (control-d) to send the message.

From inside mail, a numbered list of messages appears. To view a particular message, we type the associated number. The command r allows us to reply to that message, and f enables us to forward the message to another user. To save the message to a file, we employ the command s and give the name of the file. We use d to delete the current message. To delete a message by number, say message 5, we type the following

d 5

For the sequence of messages 5 through 10, we can delete with the following command that employs a hyphen for the numbered sequence:

d 5-10

Other mail programs, such as pine and elm, provide full-screen interfaces for sending and reading email and are available on many UNIX systems. Especially appropriate for the novice user, pine has easy-to-use operations for viewing, replying, forwarding, deleting, saving, printing, sending, and exporting,

Quick Review Question

Quick Review Question 8This question applies to the UNIX mail program. Type at most one blank where necessary.
a. Select the keystroke combination to end and send a mail message.
^c ^d ^e ^s ^q ^z


b. Give the command from within mail to delete messages 1 through 26.


c. Give the command that appears before someone's email address to send a mail message to that person.


Jobs

To kill an interactive job, such as an interactive program, we press ^c (control-c). To suspend execution, but not kill the job, we type ^z. To resume execution, we bring the job into the foreground with the command fg To have a suspended job resume execution in the background, we type bg.

The jobs command, jobs, lists all running and suspended jobs and their process identification (PID) numbers. To kill the job, type kill followed by the PID number. For example, to destroy job 1234, we issue the following command:

kill 1234

Quick Review Question

Quick Review Question 9Give the command at the UNIX command prompt to do each of the following tasks:

a. List the suspended jobs


b. Destroy the job with identification number 8000 from the current jobs


c. Destroy the current interactive job

^a ^b ^c ^d ^k ^q ^s ^z


d. Suspend the current interactive job. Use the radio buttons above.


e. Resume the current job


Summary

Figure 2 presents a summary of the UNIX commands of this module. Figure 1 contains a summary of vi commands.

Figure 2 Summary of some UNIX commands

Command

Meaning

*

Wild card

.

Current directory

..

Parent directory

/

Directory name separator

~

Home directory

<TAB>

Command completion

>

Redirection operator

Previous command

^c

Kill an interactive job

^d

Logout

^z

Suspend execution

apropos

Commands related to a keyword

bg

Background

cat

Display a file

cc -c

Compile with cc

cc -o

Compile and link or link

cd

Change directory

chmod

Change mode

cp

Copy

exit

Logout

fg

Foreground

finger

Current users

jobs

List all running and suspended jobs

kill

Kill job

logout

Logout

lpr

Print

lpr -P

Print to a specific printer

ls

List files

ls -l

List files with privileges

mail

Start mail program mail

man

Online manual

mkdir

Make directory

more

Display a file one page at a time

mv

Move

passwd

Password

pwd

Present working directory

rm

Remove

rmdir

Remove directory

script

Save to log file


Projects

Complete the following "UNIX Introduction Assignment." Click  here to download a pdf version of the assignment.

Perform the operations below. Write the commands you use to perform each operation and answer all questions.

Log into a UNIX computer.

Make a directory called DB.

Make DB your working directory.

List the name of the working directory.

What is the full path name for your working directory?

Get into the mail program mail.

View a summary of the mail commands.

Get out of mail help.

Type the command to start sending yourself a mail message.

Type a message of at least three lines. How do you end and send the message?

Read mail you sent to yourself.

Save the message to yourself in a file called myfile.

Exit mail.

Display the list of files in your directory.

Make a copy of myfile called mycopy.

Display the list of files in your directory.

View the online manual pages on vi.

Abort the display.

Using the vi editor, open myfile.

Delete lines 3-5.

At the beginning of the file, insert a line giving the name of the file and a blank line. What command do you type for such an insertion?

What do you press to get out of insert mode?

Delete "From" and the blank that follows "From" without deleting the entire line.

At the end of the last line, add "The End."

Get out of edit mode, save (write) and quit.

Get back into editing the file.

Insert a line.

Quit the editor without saving.

Display the file.

Display the list of files with permissions.

What are the permissions?

Move mycopy to your home directory.

Move to the home directory.

Delete mycopy.

Send a message to the professor using a UNIX mail system. Indicate problems and questions related to this assignment. Also, indicate the concept(s) and command(s) you found most difficult.

Log out

To recap, this assignment covered the following UNIX commands and concepts:


Copyright 2014, Dr. Angela B. Shiflet
All rights reserved