Overview of UNIX



Overview of UNIX

(fyi: UNIX – failed operating system, multics, - multiuser OS – too ambitious for 1969. ken Thompson (who worked on multics) designed UNIX similar operating system, but simpler. Name originally unics (pun on multics – later unix).)

4 Main parts of the UNIX Operating System:

1. Kernel – (many people consider operating system and kernel to be synonymous.) Kernel is the master control program of the computer. It resides in the computer’s main memory, and it manages the computer’s resources. It is the kernel that handles the switching necessary to provide multitasking.

2. File System – UNIX organizes data into collections called files. Files may be grouped together into directories.

3. Shell – The part of UNIX that interprets user commands and passes them on to the kernel is called the shell. A typical shell provides a command-line interface, where the user can type commands. (ex. C shell, korn shell, bourne shell, etc)

The shell is a command line interpreter. The user interacts with the kernel through the shell

4. Utilities – software tool included as a standard part of the UNIX OS often called commands.

CONCEPT: The shell is a type of program called an interpreter. An interpreter operates in a simple loop: It accepts a command, interprets the command, executes the command, and then waits for another command. The shell displays a "prompt," to notify you that it is ready to accept your command.

[pic]

$ atrium65:/users1/racfried>

1. Shell displays prompt symbol on screen. This tells you shell is ready to receive your commands

2. You type in command – as you type the shell stores the characters and also echoes them back to the terminal screen.

3. You type return – this is the signal for the shell to interpret the command and work on it

4. The shell interprets your command. In most case the shell looks for the appropriate software to run your command. If the shell can’t find the right software, it gives you an error message; otherwise the shell asks the kernel to run it.

(The shell knows where to find software to run by looking through a predetermined list of directories called the search path. )

5. The kernel runs the requested software. While the command is running, the shell “goes to sleep.” When the kernel finishes, it “wakes up” the shell.

6. The shell displays a prompt symbol. This indicates that the shell is once again ready to receive your commands.

UNIX Shells

When hit return, it is the shell that interprets the command.

Bourne Shell Family

Bourne Shell (sh) – written by Steven Bourne at AT&T Bell Labs. sh first major Unix Shell. Part of nearly every UNIX system since version 7 in 1979.

Korn Shell (ksh) – created by David Korn at AT&T, improved the Bourne Shell. First version – 1986, with major upgrades in 1988 and 1993.

Bourne Again Shell (bash) - Originally written by Brian Fox for the GNU project of the free software foundation, bash was released publicly in 1988. Further development of bash has been carried on by Chet Ramey, who released version 2 in 1996.

C Shell Family

C Shell (csh) – written by Bill Joy (also wrote vi editor) at the University of California at Berkeley. Included with BSD UNIX in 1979, and has been found on most subsequent UNIX systems.

TC Shell (tcsh) – improved version of C Shell, developed in late 1970s and early 1980s by a number of programmers (most notably Ken Greer at Carnegie-Mellon University and Paul Placeway at Ohio State).

(Chsh (change shell) sometimes can do it yourself – other times System Admin does it for you.)

To see which shell you are using you can type

$echo $SHELL

/usr/bin/ksh (k shell)

UNIX File system structure

[pic]

Bin – (binary) software for the shell and most commonly used UNIX commands. Ex. man, less, etc

Dev – (devices) special files needed to operate peripheral devices such as terminals and printers.

Etc – administrative files – ex. list of users authorized to user the system and their passwords

Home – users’ home directories (some large systems there may be several directories) (ours doesn’t use home – uses /users1, etc.)

Tmp – temporary files

Usr – some versions of UNIX keep users’ home directories here, others keep useful things like online manual pages

Var – files which have information that varies frequently. Ex. usr mailboxes are found typically in /var/mail.

Each UNIX system may be set up a bit differently; all UNIX systems have a root directory at the top.

Directory – “parent” or “child”, ex. root is the parent of bin, dev, etc, home, tmp, usr, and var, and these are the children of root. (child directories = subdirectories). Every directory (except root) has exactly one parent, but may have many children.

Home directory usually the same as your login – normally cannot rename. Other files can choose names.

Files names: 1 – 255 of the following characters in any combination:

Uppercase letters (A-Z)

Lowercase letters (a-z)

Numerals (0-9)

Period (.), underscore (_), and comma(,)

Most cases avoid file names with spaces or characters such as & * \ | or other ones, and avoid UNIX command names as files.

Try to use descriptive names that give an idea of content of file. Some users prefer short names.

Consider File extensions:

Suffix attached to identify the data kept in the file. Extension after period. Ex. book can put each file in chapters.

Mybook.ch1

Mybook.ch1.v2

Mybook.ch2

Mybook.ch3.revised

Mybook.appendix1.old

Convention – file names in lowercase letters, directory names capitalized. – don’t need to observe.

Pathname:

To use a file in currently directory, all you need is a name. If it’s in another directory, need pathname.

Pathname – address that shows the files position in the file system

Absolute of full pathnames give the location in relation to the top of the file system. Simplest is for root /. (forward slash)

[pic]

Can be long – can shorten ~ is your home directory’s pathname.

~jack – jack’s home directory

Relative Pathnames:

Usually interested in a position of file or directory from current working directory. (.) is shorthand for current working directory. (..) signify the parent of working directory (one above it). Dot and dotdot.

To find Animals from jack ../jill/Animals

To see what’s in Animals:

ls ../jill/Animals

Answers: dog cat

Hidden files - begin with period. .hidden

ls –a

. .. oceans fish

create a file using

1. cp: copy file dog to Pets directory

2. redirection:

in Animals directory

ls > animallist

redirects results into file called animal list.

To concatenate use >>

ls –l

drwxrwx--- 2 racfried adjunct 512 Aug 30 16:20 Tempdir

d - file type. A d indicates directory. Regular file (-)

rwxrwx--- access privileges. ReadWriteExecute, etc.

2 – links – link = file or directory. Directory has at least 2 because each directory contains (.) and (..). regular files have one but can create more using ln.

you - owner (racfried) – login of person who owns the file

adjunct – owner’s group. Group is a collection of users to which the owner of the file belongs

size 512 – size of file given in bytes

date and time – last modified

file name – name of file or directory is listed last

rwx on file:

read (can’t change) file

write (change the contents of a file)

execute (if it contains a program, run that program)

rwx on directory:

read – list contents using ls

write – change the contents of the directory by creating new files or removing existing ones

execute – search using ls –l. also move to the directory from another directory, and copy files from the directory.

3 categories of users:

owner (owns the file)

group (other users belonging to the user’s group)

public (all other users on the system)

first 3 permissions –owner , then group, then public.

r—r—r—example

changing file permissions:

chmod

u (user), g (group), o (others), a (all), = (assign a permission), + (add permission), - (remove permission)

ex. to give owner execute:

chmod u+x filename

chmod g-rw filename (group – removed read and write)

chmod a=4 filename (everyone read permission)

chmod u=rw, go=r filename (user read/write everyone else read).

chmod 0755 filename, etc

chmod 755 file1 or chmod u=rwx,go=rx file1

other commands:

cal 12 2007

cal 2007

cal 2007 > 2007

(redirecting into an existing file will overwrite what is already in the file)

cat 2007

(too fast)

more 2007

display as much fits on screen – tells you

--More—

if more – hit space bar and you will see next screenful

q to exit

(as alternative can use pg – return key to see more)

cat filename1 filename2

displays filename1 then filename2 – can redirect to have both in 1 file.

Printing (lpr/lp)

Mkdir Cal

Mkdir Cal/Months

Ls Cal/Months

Cat Cal/Months/08.2007

Cd, rmdir

openwin

Program: set of coded instructions contained in a file. Ex. ls – sequence of computer instructions contained in the file /bin/ls.

Process: You get a process whenever the computer runs a program. When you issue the ls command, the computer creates an ls process by running the program in the file /bin/ls.

Although there is only one ls program, there may be many active ls processes.

When run a UNIX command or other program, the OS opens up 3 standard channels for input and output:

Standard Input (stdin) - The file where the program normally looks for input

Standard Output (stdout) – the file where the program sends output

Standard error (stderr) – the File where the program sends error messages

We defined as files, but UNIX file is nearly anything that can send or receive data. Usually standard input is keyboard, and standard output and standard error mean the terminal screen.

Can redirect standard output, although usually the screen.

Ex. cal 2007 > calendar.file

You can also redirect standard input – so a process takes in input from a file rather than the keyboard.

Ex. mail jones < my.message

Will send user jones the content of the file my.message.

Can combine both:

Counts the lines and words and characters in the file input and redirects the results into file output.

Wc < input > output

Return key signals shell to begin work. Can have more than one command per line.

Who; ls; cal

Instead of doing:

Cal 6 2007 > summer.2007

Cal 7 2007 >> summer.2007

Cal 8 2007 >> summer.2007

Can do it in one step:

(cal 6 2007; cal 7 2007; cal 8 2007) > summer.2007 < return>

Pipes:

To look at 3 calendars – goes to fast – can move to file and then use more or pg – but need to make a file for this. Another way is using pipes.

Command1 | command2

(cal 2007; cal 2008; cal 2009) | more

redirection places the output from a utility to a file, piping directs the output to another utility.

Tee – 2 things at once.

1. save the output from a command in a file

2. pipe the output to another rcommand.

(cal 2008; cal 2009) | tee calfile | more

places copies of the calendars for 2008 and 2009 into the file calfile as they are also displayed on the terminal screen by more.

Filters:

Filter – takes data from standard input, transforms the data in some way, and sends the results to the standard output. Often used with pipes and tees.

Ex. who | sort

sort poems

Other examples: cat (catenate and displays), diff(displays line by line differences between two files), etc. (pg. 121)

Wildcards:

Can abbreviate files names using wildcards. Wildcards: asterisk (*), question mark (?), square brackets [ ].

* - most common – any character or string of characters, including blanks

Examples:

ls f*

Lists all files beginning in f.

Ls *ball

All ending with ball

Ls *ack*

Contain sequence of ack

Ex. backgammon, track, blackjack, etc

? – one character at a time

Ls ?iking

Biking, hiking

[ ] – match any characters that appear inside the brackets

? ls team[ABXYZ]

teamA teamB teamX teamY teamZ

$ls team[1-4]

team1 team2 team3 team4

$ls [a-c]*

any files names that have a,b,or c at the beginning

backgammon baseball biking camping checkers chess

$ls *[m-z]

any files having names that end with any letter from m through z.

backgammon checkers chess hearts

this is why files can’t contain special characters such as

\ | [ ] { } $ < > ( ) # ? ‘ “ / ; ^ ! ~ %

because it is treated with a special meaning.

Background Processing

UNIX is a multitasking operating system. It can run more than one program at the same time. You can run a command and put it in the “background” to continue running while you work another task in the foreground. Type & at the end of the command line before pressing return.

If you wanted to run a long running program, called longrun, you can run it like this.

? longrun &

you will see a number like

[1] 3216

It gets assigned a process identification number or PID (most shells (except bourne) also assign a job number which is the number in brackets. You will see a prompt, which means the shell is ready to process another command line even though longrun isn’t done.

PID is important if you want to terminate it before it finishes.

Kill 3216

Some are hard to kill – require stronger medicine:

Kill -9 3216

Can also do these commands through scripts.

#!/bin/ksh

Text Editors

Text Editor – can use to create and modify files. Word processor does much more (controls the appearance of the text, the layout, paragraph styles, etc and with some create tables and insert graphs, etc).

Most widely used text editors:

VI – written by Bill Joy, graduate student at the University of California at Berkeley (also wrote C Shell and later became one of the founders of Sun Microsystems). Originally found only on Berkeley UNIX, vi is now the standard editor on POSIX-compliant systems.

Because the POSIX standard specifies vi as the standard editor, it is likely to be found on every UNIX system. It works well on dumb terminals and terminal emulators. However, it does not have the flexibility of emacs.

Emacs – powerful, feature-rich editor known for its flexibility: can be personalized to fit taste of the user. Some supported windowing before windowing systems became common on UNIX.

Although not officially a standard editor, emacs is widely available. It can be used on a dumb terminal or terminal emulator. It can be customized to suit your tastes. Some users prefer it to vi.

Pico – developed for composing messages in the pine mail program. The menu structure makes it easy for beginners to learn.

In general pico is easier to learn than vi or emacs, and would be a good choice for a beginner. The user interface is simlar to pine mail. If you use pine, you will find it easy to use pico as well. Moreover, pico can run on a dumb terminal or terminal emulator. Its major drawback is that it might not be available on your system.

(Xedit – this X client is a simple window-based text editor. Available on most systems that run X. – not covered in this book.)

CDE Text Editor – The Common Desktop Environment (CDE) includes a simple, yet powerful, text editor.

Most powerful editor, easiest to learn. First choice for anyone working in the common desktop environment, but can’t run on dumb terminal or terminal emulator. (dumb terminal – can’t display graphics)

If your concern is to learn one editor that will be universally available on all standard UNIX systems, you should consider vi (although emacs would be a close second). For ease of use, choose pico or Text Editor.

You could learn a couple of them if you want, I will teach vi, but feel free to use any others if you’d like.

When you start a text editor the editor opens up an empty work buffer.

If it’s an existing file, the editor makes a copy of it and places the copy in a temporary workspace called the work buffer.

Original UNIX editor ed is line editor – makes changes in the buffer line by line. Need to specify the line first then specify the change. Lots of work. Working with a line at a time so it’s hard to see where changes fit into the text as a whole.

Full-screen editor – view and work with as much of the work buffer as will fit on your screen. (vi, emacs, pico)

GUI – new class of full-screen text editors designed to take advantage of the pointer and mouse. CDE Text Editor is an example of this.

Editing with VI

When you first enter the vi editor, it is set to the command mode. All keystrokes are editing commands, not text to be entered into the file. To switch to insert mode I, a, etc. to go back to command mode.

If forget which mode, hit a couple of times to get back to the command mode. If the terminal beeps, you know you’re in command mode.

Spell command – checks spelling, but not grammar – checks for words not in UNIX word list.

/ - searches

(look at vi commands doc)

Other Unix Commands

Write and talk – allow users to send typed messages to each other. Play around with that with each other if you like. Send messages see what happens. In the book it will show you how to respond. Optional.

Mail

mail in our system calls mailx

mail racfried

Subject: Test Message

Cc:

This is a test

You can create a file containing your message and then mail the file.

Create a file using the text editor

Redirect the file to the e-mail program. To send the file poems to the account login, type

Mail racfried < poems

Or you can call vi in your mail program.

In body type ~v

Make changes and save them using

:wq or ZZ

Mailboxes:

2 kinds.

System mailbox – file that holds mail you received but have not read, deleted, or saved to another file. /var/spool/mail/jdoe. Ours is /var/mail/racfried.

Personal mailbox – file named mbox in home directory. Any messages you read but did not delete or save in another file are put into your mbox file when you quit the mail program.

Many shells will check your system mailbox periodically can d notify you of any new messages.

Reading mail:

$mail

mailx version 5.0 Sat Apr 6 14:57:29 PST 2002 Type ? for help.

"/var/mail/racfried": 4 messages 4 new [Read only]

>N 1 Rachel Friedman Tue Aug 9 10:52 16/753 hello

N 2 Rachel Friedman Tue Aug 9 11:01 16/759 test message

N 3 Rachel Friedman Tue Aug 9 11:02 17/758

N 4 Rachel Friedman Tue Aug 9 11:50 19/804 edit with vi in msg

If lots of messages, to see the next screenful type z, previous, z-, h to list current

Type in 1 to read message 1, etc.

You will see mail header and then message

To Reply:

R1 – replies to message one.

Type message – enter and cntrl d

Save a message with s

s1 message1.file1

creates new file in your home directory named message1.file1. if file already exists, message is appended. (original message is deleted from system mailbox).

Quitting:

q = quit – puts any messages that you read into a file named mbox in your home directory. Unread messages are left in the system mailbox.

X – exit – quits the mail program without changing the mailbox, restoring any deleted messages. (q is more commonly used)

To read your mbox:

Mail –f

Process – using the usual mail commands

Quit – q or x

Logging in Remotely:

telnet atrium65.its.brooklyn.cuny.edu

login/password – link on webpage to exactly how and ftping.

-----------------------

root (/)

bin

dev

etc

home

tmp

usr

var

Jill

Jack

var

usr

tmp

home

etc

dev

bin

root (/)

Fish

Toys

Oceans

Animals

dog

cat

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download