UNIX Lecture 2


File protections

UNIX is multi-user
over 1000 in the department
prevent certain users from performing certain operations on other usersŐ files
Each file/directory has a single owner
this is one of the users of the system
Particular sets of users may be formed into groups
staff, it, other, fide
use the command groups to find which group(s) you are in
Permissions granted independently to
the owner
members of a particular group
any other user of the system

Permissions

What kind of operations are permitted/denied?
read
write
execute/enter (for an executable file or a directory)
NINE different permissions to set for each file
3 categories and 3 kinds of operation
Long, group list (ls -lg) displays the permissions
yap{cuttsq}57: ls -lg myfile
-rw-r--r-- 1 quintin staff 24 Nov 20 15:48 myfile



file type: d for a directory, - for a plain file

Changing permissions

chmod (change mode) to change permissions
chmod g+w myfile
chmod a+w myfile
u = user, g = group, o = other, a = all

chmod 664 myfile
consider each set of three permissions as a binary number, if the permission is set then 1 else 0. This gives for myfile 110 100 100. Turn each of these into a decimal digit, giving 644. So the above chmod represents the permissions 110 110 100 or rw-rw-r--
Default setting on file creation can be changed
currently rw-r--r--

Commands as a Sausage Machine


standard in and standard out
by default, the keyboard and window of the shell
but they can be reassigned using < and >
Consider cat (catenate)
cat myfile prints out myfile, stdin = myfile, stdout= shell
cat prints whatever is typed to the shell
NOTE - end of file character is CTRL-D or ^D
Redirecting stdin and stdout
cat myfile > yourfile output redirected into yourfile
mail cuttsq < myfile mails myfile to me

UNIX Pipes

Central concept in UNIX
generalisation of I/O redirection
mechanism for constructing complex commands from many simple ones
Pipes connect several commands together
the results of one command are piped into the next
i.e. the stdout of one becomes the stdin of the next
Simple example
ls -l |more
| is the pipe operator

Shortcuts to Less Typing

Text-based commands can lead to lots of typing
The shell provides various mechanisms to reduce the burden
By example, given a directory containing
test1 fred test2 john test22 west2
ls test* produces
test1 test2 test22
ls *est* produces
test1 test2 test22 west2
ls test? produces
test1 test2

The History mechanism

allows command already executed to be re-executed
a number of the most recently executed commands is retained by the shell
print out with h
each command is numbered - various ways of executing them again
!! repeat last command
!123 repeat command 123 (must be in history list)
!m repeats most recent command starting with m

More History

Fragments of previous commands can be included in current command also
remember that a command consists of a number of words
cat !$ replace !$ with LAST word of previous command
more !72:$ replace !72:$ with last word of command 72
more !72:^ replace !72:^ with 1st word after command in c72
more !3:2 replace !3:2 with 2nd word of command 3
note: word numbers start at zero - the command
^old^new redo last cmd, replacing old with new

Filename completion
pressing ESC half way through typing a file attempts to complete it
completes to the point where a common root splits
pressing CTRL-D half way through a file displays all files with the root so far typed

Creating & editing files in UNIX

Creating empty files - touch
touch myNewFile
Editing in a text-based environment
commands issued using key-strokes
how are these disambiguated from text for the doc?
on-screen view of document may not be very WYSIWYG
usually difficult to start with
but can become very fast with experience

vi - visual editor

The default editor provided on most UNIX sys
Starting vi
vi opens a new empty file
vi filename opens the filename indicated by filename
Command and Input modes
Central concept in vi
in CM, keystrokes are accepted as commands
vi starts up in CM
in IM, keystrokes are inserted into the document

Entering commands

Pressing keys and keys with modifier keys
Last line mode - typing a : (colon) followed by command
:set showmode shows which mode the editor is in
A few commands
Moving the insertion point
RETURN, SPACE, arrow keys, or h, j, k, l
Deleting characters
x deletes the character under the insertion point
dd deletes the line containing the insertion point
dw deletes a whole word
The undo command
u undoes the last command. Only the last is stored
Saving the file
:w saves the file using a name already supplied
:w fred saves the file using the name fred
Quitting vi
:q quits (as long as file has been saved)
:q! forces quit even if file has not been saved
ZZ saves under existing name and quits

Inserting text

To enter insert mode
i insert text after before insertion point
a insert after insertion point
o opens a new line after i.p. and enters insertion mode
Anything typed goes into file
Delete mistyped characters using delete key
cannot delete onto a previous line
cannot delete beyond starting point of insertion
To exit insert mode
press ESC key