You are on page 1of 36

Unix 101: Introduction to UNIX (i.e.

Unix for Windows Users)

Mark Kegel
September 7, 2005

1 Introduction to UNIX (i.e. · solid and basic understanding of unix


Unix for Windows Users) Ù not enough time in the regular school year
to teach you this and teach you everything
• The cold hard truth else you need to know
Ù and also because the CS department
· this course is NOT sponsored by the CS doesn’t have the resources, or want to of-
dept. fer this course on its own - maybe they will
· you will not receive any credit at all in the future
Ù we needed something to do with our sum-
• introduce ourselves to the students
mer vacation
· Should introduce myself and Marshall Ù the goodness of our hearts, etc.
Ù are seniors in CS and CS/Math • To make this perfectly clear...
• And what do we think we’re doing... · will not teach you to program
· will not teach you emacs tricks; will provide
· here to teach you fundamentals of Unix
a reference sheet
· to teach you the student to teach yourself Ù not about learning to use a single applica-
about unix tion
· to give you the necessary basics to go out Ù will not solve world hunger, peace, or to be
and teach yourself whatever you might need a better person
to know to get work done on unix
· catch a fish for a man vs teach a man to
fish
2 Computers on Campus
· trying to follow Mudd philosophy - teach • Available computing resources
you to teach yourself
· graphics lab
• who this course is targeted at · terminal room - printer
· AC and LAC labs - printer and scanner
· those students who have never worked with
unix before and would to get some experi- · Engineering lab - to be avoided
ence doing • Servers
· those students who have had some experi-
· odin, thuban, banshee
ence but would like to learn more
· knuth, knuth, wilkes, muddcs, arc, cortana
· someone who is lost, confused, and looking
Ù knuth is the one you care about
for spiritual enlightenment
Ù main server everyone will be working on -
Ù mashall has a great religion for you to join give specs.
- we sin so jesus didn’t die in vain
• People to know
Ù ask around, see who has what experience
· Tim Buchheim and Roger Wiechman
• Some reasons to learn about Unix
Ù Bring them milk, cookies and be nice to
· linux and OS X them, they make things work

1
3 Philosophy and History of Ù about as different as mango, peach, kiwi, straw-
berry ice-cream and peach, strawberry, kiwi,
UNIX mango ice-cream with bits of vanilla
• Origin of Unix Ù especially that way in linux, its all just annoying
Ù computers were mainframes that people Ù it doesn’t matter what you run, what we are
worked at, much like knuth going to teach you here will apply across most
Ù teletypes - basically a typewriter plugged unixes though not all
into a computer
• What is Unix?
· Ken Thomson implemented the first UNIX
environment Ù unix in some sense is not an operating sys-
tem but rather a programming language
Ù had been working at Bell Labs on a project
(C)/philosophy/interface (POSIX)
called Multics (Multiplexed ...)
Ù the development of C (and the internet) are
Ù Unix was kind of a castrated Multics - much
all connected
smaller because invented on an underpow-
ered system - PDP-7 Ù unix is (kind of) C, the Standard C Li-
brary, and POSIX (Portable Operating Sys-
Ù became popular mainly because it was dis-
tem Standard) which is the API that pro-
tributed to Universities
grammers use to interact with the system
Ù will want to display Unix timeline kernel

• Unix Today Ù the design and implementation of unix also


follows a certain philosophy about how
Ù programmers environment - because of this an OS should be designed, how programs
is extremely powerful should be written, etc.
· still very much a tinkerer’s environment Ù everything else is just historical baggage
· unix encourages and support ”hacking” the Ù makes unix extremely flexible - can start
system from scratch with a new implementation
but still same ”design”
· Fundamentally multi-user and multitasking
Ù designed in the days of mainframe environ- Ù what would be some good philosophical ideas to
ments when many people would all work on follow when building an OS?
one computer system
Ù what are some good rules of thumb? what trade-
Ù we still use the same model in many ways offs do you want to make?
with knuth
• Unix philosophy
Ù clean, simple interface
· programs should be designed to operate to-
Ù Chalkboard exercise gether
Ù unix is also multi-layered in design, mean- · output of one could be used as the in-
ing that functionality separated into levels put to another
going from the hardware to the user, stuff Ù text streams -> universal input
is also abstracted aways at each layer
· Programs are viewed as simple tools that
Ù user | (shell, other programs) | OS(kernel) combined will do complex tasks
| hardware
· each program should do one thing ex-
Ù at top user at bottom hardware, what’s in- tremely well
between?
Ù LEGO blocks - simple tools but can create
Ù scales very well - from supercomputers to very complex systems
microwaves (embedded systems)
Ù another analogy - hammer, wrench, saw
Ù two different flavors of unix - BSD (FreeBSD, Ù Avoid the ginormous programs that do ev-
OSX) and System V (Solaris, Linux) erything

2
Ù Windows has this problem (think of Of- make commands and programs interact
fice) with each other, not just from treating
Ù Windows programs also don’t integrate each as a fixed black box.
very nicely - no pipeline · Richness of utilities (over 400 standard
· want to keep everything as simple as possi- ones) often overwhelms novices. Doc-
ble umentation is short on examples and
tutorials to help you figure out how to
· KISS - keep it simple stupid use the many tools provided to accom-
Ù this applies everywhere - both in OS plish various kinds of tasks.”
interaction (system calls and library
Ù ”Unix *is* user-friendly. It’s just picky
calls) and in program creation
about its friends.”
· separate mechanism from policy
Ù very true - have to be able to understand
Ù Unix (and programs that run on Unix) the system at some intuitive level to be able
provide a mechanism to do something to work with it
but do not lock you down to one policy
Ù also if you don’t like something you can fix
Ù policy - how a program should look and
it, maybe not easily, but you can fix it
behave
Ù users have to decide on policy Ù What makes a system secure? What needs to be
Ù games and GUIs are generally a good protected?
examples - mods and skins
Ù What is a good way of protecting things, like
Ù walk up to some one and try to scare them property? Ownership
· principle of least surprise
• Unix security
· applications will generally produce no
output if successful · concept of ownership - everything on the
· applications that fail will make it evi- system is owned by someone
dent (sirens if necessary) Ù ownership implies responsibility - so who-
· programs should do the least surprising ever owns these files is in the end responsi-
thing ble for them
Ù do not try to feed emacs a stray cat · protect users from one another and from
· there is no ”one true way” - unix is not doing dangerous things
perfect! · protect privacy -> data
Ù unix has things wrong with it, and · protect work -> programs
things that unless you understand will · protect programs and files from users - or
cause you a great deal of pain rather allow only certain users to do certain
Ù The Unix Hater’s Handbook - written things
to expose the flaws in unix
Ù security: not perfect, can break, and will
· from a stanford site: cause you to scream a few months down the
http://pangea.stanford.edu/computerinfo/ line
unix/overview/advantages.html
· ”The traditional command line shell in-
terface is user hostile 4 Types of users
· designed for the programmer, not the
casual user. Ù what powers is each user going to have?
· Commands often have cryptic names Ù do you need to have a class of users that manage
and give very little response to tell the the system?
user what they are doing. Much use of
special keyboard characters - little ty- Ù what kinds of users are there going to be?
pos have unexpected results.
• administrative users
· To use Unix well, you need to under-
stand some of the main design features. · root - is the best known account of this type
Its power comes from knowing how to - aka The SuperUser

3
· complete control over everything on the sys- Ù for security reasons do not want to have a
tem web server running as root
· can do anything that they want (mostly) - Ù what if someone owned your web daemon -
has to respect file perms. would own your system...is bad
· can become any user on the system Ù unless you’re setting up your own system
Ù loaded gun with the safety off, easy to shoot don’t have to worry about this
yourself in the foot Ù and even then still don’t really have to
· is a standard account on every worry about this since most of these users
UNIX/Linux/BSD machine will have been created by default - rarely
have to create a new system user
· used for system administra-
tion/maintenance Ù now that we have users, how do we manage
· what this is will cover later them?

Ù any other user however can be granted ad- Ù should all users be lumped in to one category?
min privileges, so root isn’t strictly neces-
sary to run a system • groups

Ù why would you want? what if you tried to Ù are the unix solution to separating respon-
delete all the files on the system? shouldn’t sibilty amoung users
be able to do · every user on the system belongs to one or
more groups
Ù should there be different types of normal users?
is there a better way to organize responsibility? · these are sets of users which all share some
characteristic
• ”normal” users
Ù on the board draw a Venn diagram
· have a home directory - directory to keep Ù one group, on knuth at least, is students
all your files to yourself
Ù every student that has an account belongs
Ù cannot generally see others home directo- to this group
ries, Odin had this problem and Charlie still
Ù consultants are additionally members of the
does
operator group
· can use most of the programs on the system
Ù however since consultants are also students,
· any special configuration files are generally we draw another distinction
stored in your home directory
· every user has a primary group - this is a
· on knuth you have about 120 Mb for all group to which file which you create will be
your files assigned
Ù normal users have to obey the security poli- Ù can generally be a member to as many
cies of the system, superusers can ignore groups as the admin wants you to be
most of them
Ù some groups serve special purposes along
Ù on your own system can have as much as with the ”special” users
you want ;-) or if you’re CS staff
· can have groups ftp, www, etc.
Ù the CS department does daily backups so if
· exists to allow easier management of a sys-
you lose a file you REALLY want then you
tem by administrative users
can ask them to restore it
Ù for what jobs do you need a third type of user?
5 Logging In
Ù what jobs do you not want root doing, or a nor-
mal user? webserver • Loggin In

• other users: nobody, ssh, ftp, etc. · two parts: username and password
Ù username can be anything, though some
· used to run daemons in the background are reserved for special purposes (root, bin,
Ù run ps -aux | grep daemon etc.)

4
Ù on knuth it will be the first character of Ù the teletype would send the input, and the
your first name, followed by your last name output would be printed out on a sheet of
(hopefully); on knuth the username limit is paper, this meant that the teletype and the
16 characters computer could be located either next to
Ù so on knuth I am: mkegel; Marshall is each other or across the country, the com-
mpierce puter couldn’t tell
Ù later the sheet of paper changed to a CRT
• password should be strong monitor - would just print the text to the
screen
· what makes a strong password?
Ù over time the teletype changed to a full
· min 8 characters in length (turing only pays keyboard but the fundamental relation be-
attention to the first 8 characters of a pass- tween the teletype and computer (input
word) and output) for most things hasn’t really
· special characters (Numbers and changed
@ ! # $ % ^ & . , ( ), even spaces) Ù obviously today you have both upper and
lower case characters
· uppercase characters
Ù most of you probably come from either a win-
· should not look like a dictionary word
dows or mac world where most things can be
· pass-phrases are good things done with a graphical interface, for certain things
· I took CS60# could become -> a GUI (graphical user interface) can be very
1 t00k CS 6o# (thats a one with good (ex. web browsing), however, certain other
two zeros in took and a lower case O) things are made much simpler by the use of the
cmd line.
Ù Demonstrate logging in to knuth
Ù for you, however, the best reason to learn the
Ù notice that no password characters show up command line is that you have to
when typing
Ù the CS department has chosen to have students
do most of their work on unix systems, therefore
6 Introducing the shell to make your life easier you should learn how to
use them
Ù log into knuth with the default knuth setup
Ù Why should you learn to use the command line?
Ù what you see when starting What does it offer?

Ù the default knuth setup, easiest and most • The Blinking Cursor of Doom
familiar setup · simple, quick, and powerful
Ù components of the prompt: username, time, Ù what does that mean?
command number, and directory Ù can do an enormous amount of work, with
very few commands
Ù change the prompt to something much simpler,
like % Ù for certain tasks are much superior to a
graphic interface
• Sessions of the Heart
· becoming a unix power user involves be-
· unix is at its heart a cmd line system - in coming familiar with the cmd line
some respects this is much more powerful Ù we will focus this course on using the command
than having just a graphical interface line
Ù it was created in the days of the teletype,
like a telegraph with a keyboard only up- Ù change prompt to something simple - probably
percase letters though - so very limited use a special account on farmboy

Ù so the teletype would be hooked up to the Ù for now we want a simple prompt, don’t want to
giant room filling computer distract you

5
• Home sweet Home · things in brackets optional, angle brackets
not optional
· when you log in you will automatically be
at your home directory Ù cmd [switches] [arg1] ... [argn]
Ù that is how commands will appear and be
· home directories on knuth, and many other
used on the command line
systems (OS X is different) are located at
/home/username • switches
· on OSX home directories are at
Ù What is an argument and what is a switch?
/Users/username
· switches are arguments that you pass into a
Ù for me: /home/mkegel (knuth, farmboy)
program that modify the programs behav-
/Users/mkegel (shadow)
ior - switch the behavior of the program
· directories are denoted with /, windows · sometimes called flags, we’ll use the term
uses switch though
Ù there are other directories that we will get · different than what is generally referred to
to later, but on knuth you should never have as an ’argument’
to worry about them
· a switch will generally have the form ”-?”
• Unix directory tree where ? is some character
Ù most programs take multiple switches and
Ù what do i mean the home is /home/mkegel?
also accept multiple forms for what is the
no c:?
same action
· file systems can generally be seen as a tree
· other style, GNU style, is "--switch"
· a cdrom or hard-drive can generally be lo-
· most programs accept many switches
cated anywhere in the tree
Ù gpg (encryption key generator) accepts 360!
· no preset structure
Ù demo the tar stuff from the command line
· working with devices we’ll get to much later
so just sit tight for now Ù can have multiple switches, example tar

Ù there is no c:, d:, a: like on windows, every- Ù tar (tape archive) is an archiving program
thing starts at / (root) from the days of tape drives
Ù file archives are given the extension ”.tar”
· is filesystem independent
(gee big surprise!)
• Introducing the Shell · to uncompress will most often type
Ù what is the shell? talked about multi- Ù tar -xvf [tar file]
layered design but what is it? Ù ”-xvf” could have been typed ”-x -v -f” but
· The shell is the highest level interaction easier to write the first way
layer between you and the OS Ù on old archaeic systems this may not actu-
· it’s what sits between the teletype and the ally be the case but on new systems it most
computer - makes the computer easier to often is
use • Arguments
· it runs whatever commands you give it,
· data that you want to program to operate
with whatever arguments you give it
on
· the most important thing in learning to use
Ù what is an argument? what would you pass
unix in a productive manner is to become
in?
comfortable with using a command line
· filenames, text strings, man pages
Ù that’s not a computer fan, that’s the shell
purring... Ù all of this stuff is sensitive to spaces, so ”ab”
is not ”a b”
• syntax of a command
Ù not every command takes arguments; some re-
· will use quire them

6
· some basic commands to get started with - these · there are also special characters will in dif-
commands are more complicated than what we ferent contexts will be interpreted differ-
are actually showing here, you’ll be able to find ently
out more stuff about them after the end of this
lecture • Ctrl+World = Mwahahaa!
· discuss C-? and M-? notation
Ù these are also the commands that you’ll be using
the most often · you see this everywhere, particularly in doc-
umentation, so is very useful to know
Ù students are going to have to write all of this
· is incredibly simple: C == Ctrl; M = Alt
down, will probably want to put on the chalk-
(but stands for meta)
board
· therefore C-x is Ctrl and lowercase x held
Ù cd [directory] :: changes the current di- together
rectory · are what might be called shortcut keys un-
Ù pwd :: prints the current directory you are der windows
in · one other notation is caret notation; ^h
Ù mv <source> <target> :: moves the would mean C-h
’source’ file to ’target’ file/directory
• quitting programs
Ù cp <source> <target> :: moves the
’source’ file to the ’target’ directory · most programs can be quit through either
Ctrl-c or Ctrl-d
Ù rm <file1> [file2] ... [filen] :: removes
files · C-d sends an EOF character
Ù ls :: gives you a listing of the files in the · C-c sends an interrupt signal that causes
directory the program to clean up and then to exit

Ù mkdir <name> :: creates a new directory · if C-c doesn’t work and you have to quit the
program, then C- will cause the program to
Ù rmdir <name> :: removes an empty direc- exit without cleaning up first (as well as
tory dumping core)
Ù man [command name] :: prints the ”man
• tab completion
page” for various system commands
· wouldn’t it be nice if say you have some
Ù some commands are interactive
long directory name, ./thisdirectorynameis-
Ù all these programs just run, do their thing, and toolong/ but there was some way that
then quit you didn’t have to type it out every time,
hmmm...
Ù what we generally refer to as an application is · most shells (and any that you would
interactive; commands are generally not interac- normally use) provide whats called tab-
tive completion, allowing you to complete par-
Ù emacs and the shell are both applications tially written statements into their full form
Ù demo this feature
• some things to know
Ù so given partial data the shell will deter-
· note: unix is case sensitive - AAAA is dif- mine what sequences of characters will ap-
ferent than aaaa - this shouldn’t be too sur- propriately complete what you’ve written
prising on the cmd line
· also most unix commands are done in lower- Ù this is accomplished by hitting the tab key
case as you’ve just seen Ù hit tab key to show different completions
· most switches to programs will also be in Ù once you’ve typed enough to make the rest
lower case, although some need to use both of the string unique (that is there is only
- pay careful attention to both the switch one possible thing that you could be typing)
you’re using and the case of the letter(s) the shell will automatically complete what
you may be passing in you’re typing when you hit tab

7
Ù demonstrate on the long directory name · the shell interprets a number of characters
Ù since directory names have to be unique on as being special
the same level, tab-completion work really, · that is some characters have meanings to
really well the shell other than what you may have
Ù but what if you want to tab complete other typed
things? · so which characters are special?
Ù like say the arguments to a functions, or
· # $ * ? [ ] ( ) = |
switches...
^ ; < > ‘ $ " ’ \
Ù luckily this is setup by default with the shell
you’re using, so you can tab complete any Ù what do you need special characters for?
number of things, man pages, arguments, Ù for example the semi-colon ’;’ separates
etc. commands on the cmd line
• files and directories Ù cmd1; cmd2; cmd3;...
Ù up arrow gives old commands, continue Ù but what if you want to pass a special char-
pressing to work back through your en- acter to a program?
tire command history, useful for when you
have to type the same commands again and • Escape From ”The Shell”
again - like
Ù make program Ù what is you want to pass these characters
into a program?
Ù the down arrow with of course cycle forward
in your history · have to escape them; \?, where ? is the
special character you want to use
• command line editing
· demo echo semi; versus echo semi\;
· the left and right arrow keys move the cur-
sor allowing you to edit the cmd you’ve · so when you type \; the special meaning of
typed on the screen - that way you can go ; is disabled
back and correct something without having · you can also disable the special meaning by
to retype the entire command surrounding the character in quotes, either
· other useful key commands are (assuming ” or ’ (double or single quotes)
they aren’t already used): · demo echo ";"
· C-? will delete back
· how quoting is more powerful since all the
· C-d will delete forward special characters inside of the quotes get
· C-u will delete an entire line disabled - well mostly, there are some rules
· C-k will delete all of the line forward
from the cursor · pg. 527 UPT
· C-l clears the terminal · ’xxx’ - everything get disabled
· C-y will paste the characters you have · "xxx" - everything but $ ’ and \
deleted
· the character has another meaning that
· C-a will move the cursor to the begin- you have to worry about
ning of the line
· C-e will move to the end of a line · if you have a at the end of a line then the
· M-b moves back a word new line will be removed and the shell will
treat the two lines as one long continuous
· M-f forward a word
line
· C-_ will undo the last thing typed
· C-r will do a history search Ù echo Who \
stole \
· there are many more...and most impor-
the cookie, \
tantly you can create your own!
from the \
• special characters cookie jar \?

8
7 Finding help · is a great resource for general informa-
tion, system policy, etc.
• Finding Help Ù give the URL for the QREF’s
Ù our goal is not to teach you unix, but to · http://www.cs.hmc.edu/qref/
give you the basics, then have you be able · the professors will also help you out as much
to teach yourselves what you need to know as they can
Ù but sometimes you have to ask for help, · some good mailing addresses for questions
and often times this is the best way to
· linux-l@cs.hmc.edu - linux mailing list
learn something new, don’t beat your head
for hmc students and others
against the wall if someone you know al-
ready has the answer and can help you fig- Ù give out others (?), maybe fo-
ure it out! rums.muddstudents.org
· so part of teaching yourself is knowing when • Built In Docs
to ask questions
· man pages
· there is one resource that is the best and it
is? ...Google! · purpose is to document how each of the
various programs/commands on the
· whenever you have a question about some- system work, and how to get them to
thing the first thing that you should always do what you want
do is google for the answer - most likely
· generally short on examples, but in
someone else has already taken the time to
general well worth reading
solve and write up whatever solution you
are looking for · can document other things
Ù get in the habit of using man pages -
· lots of very good resources
saves time and effort!
· websites (particularly forums), books,
· what man pages are not
documentation, and people
· general reference, programming
· some good websites to check out are:
guides
linuxdoc.org, freebsd.org
Ù what man pages are
· some good books are: UNIX Power
Tools, FreeBSD Handbook... · very specific references on a limited
set of system commands
· people: Marshall, Mark, Mac, and
many others!! · essentially they document how to
use the programs on the system
· Computer Science Department Staff and
Ù some man pages break these rules but
Consultants
in general they seem to be followed
· they have people hired to help you out!! Ù demo man
You should use them as a resource!
· Other systems
· Staff take care of the various boxen the
department runs · info
Ù give a listing of the boxes, their pur- · is mainly about emacs
pose, and their addresses · the ’help’ command for bash
· knuth.cs.hmc.edu – shell server · Most programs come with more documen-
Ù find new mail server tation than just a man page
· muddcs.hmc.edu – web · check out: /usr/doc and
· Consultants sit in the terminal room /usr/share/doc
and answer Unix and CS related ques- · slackware uses /usr/doc and is really
tions good
· give a list of who are the consultants,
emails, and schedule
· The CS department website
8 How to use man
· QREF’s - documentation written • how man is organized - sections - explain what
specifically for YOU each section covers

9
· 1 - Commands available to users Ù SEE ALSO - other related man pages to see
· 2 - Unix and C system calls Ù BUGS - bugs the program may have
· 3 - C library routines for C programs Ù FILES - files that the program may use
· 4 - Special file names (Devices and Device (usually configuration files)
Drivers) Ù AUTHOR - who wrote the program
· 5 - File formats, protocols, and conventions • navigating a man page
for files used by Unix
· 6 - Games Ù man by default uses more (more is a pager
that sucks)
· 7 - Conventions, Macro packages, Word
processing packages and Misc. Ù on knuth man is setup to use less, but be
aware!!
· 8 - System administration commands and
procedures Ù so learning to use man is really like learning
to use less
Ù always need to keep in mind that one com-
mand could be in two plus different sections · arrows move man page up/down left/right
at the same time · space bar moves a screen at a time
Ù allows for a better organization of documen- · to search would type /pattern, then press
tation enter
• some switches · demo this feature
· typing / or n again moves to the next in-
Ù man -k perl
stance of pattern
Ù so then ”man -k” allows you to search for
· N moves to the previous instance
man pages
· b : back - like page up
Ù ”-s” - specify the section to search
· f : forward - page down
Ù ”-a” - show all man pages that match the
search, not just the first Ù man almost always uses the less program as
its pager, so learn to use less!
Ù man -a sync (demonstrate this)
Ù you’ll also use less for lots of other text
• how to read man pages - the parts of a man page viewing
Ù at the very top is the is the name and sec- Ù exercise - now for a man page hunt
tion number; for man it is man(1)
Ù NAME - just a quick one line description
9 Making programs work to-
Ù SYNOPSIS - all the arguments that the
program accepts gether on the cmd line
Ù how to tell if something is optional or • Working Together
required
Ù optional things are surrounding in Ù ask question before showing slide
brackets ’[xxx]’ Ù unix is about more than just running one
Ù required things are not program at a time (that’s windows job); in
Ù show example - man requires at least unix programs work together, so we’ll show
one argument you how to do that
Ù man man Ù How do you get programs to talk? Files?
Ù DESCRIPTION - a paragraph ”summary” Pass in a program as an argument? What
of the program should handle this mechanism? What
about basic program I/O?
Ù OPTIONS - what each of the switches to
the program do Ù Already know that can accept arguments
and print stuff to the terminal.
Ù ENVIRONMENT - what environment vari-
ables the program expects Ù Text streams are a universal I/O format

10
• Working Together · so stdin, stdout and stderr are files? no, but
are treated as files
· unix runs more than one program simulta-
neously · are actually file descriptors, so being
treated like files
· programs need to work together
· why so surprising? you read() from stdin,
· programs use stdin, stdout, stderr for gen- write() to stdout and stderr
eral I/O purposes
· each program has its own set stdout, stdin
· by default I/O comes from cmd line and stderr that is managed by the Operat-
ing system
• What stdout, etc do
· how do you combine simple programs? how
Ù you should already know about stdin, std- do you get two programs to talk?
out and stderr from CS70 or CS60
Ù good time to use the chalk board
Ù they allow a single program to get input
from the cmd line or print output to the Ù ask group for ideas
cmd line Ù we know that two programs can share the
· stdin - represents the characters you are in- same file - can have the same file open two
puting different times
· stdout - the standard place for the program Ù so to communicate one program could write
to print output to a file, the other could read
· general goes to the terminal you ran the Ù [program one] --> [file descriptor/pipe] -->
program from [program two]
· stderr - like stdout but for error output Ù can use the stdin, stdout, and stderr files to
have programs talk to each other
Ù need to show distinction between stderr and
stdout Ù [program one] --> [stdout/stdin] --> [pro-
gram two]
Ù in C++ the three are accessed through
cout, cin, and cerr Ù the stdout of one program IS the stdin of
another
Ù can accessed in other ways through other
languages Ù very flexible and powerful solution
Ù so we have some idea of what stdin and • Using pipes
stdout are, they represent the input we get
from the terminal and the output we send · stdin, stdout, and stderr are actually pipes
to the screen, but how do you think that · instead of the program printing its output
they actually work? to the screen it is piped to the second pro-
gram
• Everything in Unix is a file
· keyword: pipe
· devices (keyboard, mice, monitor) · you can do this from the shell
· directories (yep even directories) · the syntax is
· links (we’ll cover what these are) Ù cmd1 | cmd2
· and what you normally think of as files Ù the ’|’ character is known as the pipe and
Ù what does it mean to be a file? anything is located on the key, so Shft- is |
used with read() and write()? Ù example of this in action - first run who and
Ù put quotes around file -> ”file”, aren’t re- tell what wc does, then run this
ally files, just treated like files Ù who | wc -l
Ù a file is just a stream of data, in some cases Ù (gives the number of terminals open on the
with limitations as to how that stream is system)
handled Ù get a longer piping example
• Are they file? · programs used in this way are called ’filters’

11
Ù filters are the tools that you use to make Ù have a demo ready to append
tasks easier, and you need them to be good Ù echo "Hello there!" > newfile
Ù if you can’t use filters you’re never going to Ù echo "Good to see you." >> newfile
be able to exploit the power of the system Ù cat newfile
Ù cause you won’t be able to leverage the
• Working with stderr
work of other people
Ù how many people think that they could im- Ù word of warning: this is very shell depen-
plement ls? how about a good implementa- dent, but since you are working with zsh, or
tion? you ought to be the syntax provided here
should work
Ù to make them good you have to follow a
certain set of standards and conventions · send both stdin and stdout with
Ù cmd1 |& cmd2
Ù if you don’t the filter will be difficult to use
Ù cmd >& file
Ù so what are these conventions? we dis-
cussed many of them at the beginning of Ù spacing matters so pay attention to it
the class Ù can also append stderr to a file - better for
log files since you don’t generally want to
Ù KISS, principle of least surprise, you’ll learn
send error messages along a pipeline, only
most of these just by using a UNIX system
want real output
Ù demo piping on terminal · to append stderr very much like redirecting
• Redirecting Output stderr
Ù cmd5 >>& file
Ù can do more than just piping between pro-
· to redirect just stderr to a file
grams
Ù cmd 2> file
· you can redirect the output of a program to
a file Ù redirecting just stderr along a pipe is a bit
more difficult
· you can redirect a program to read stdin
Ù demo this concept
from a file
Ù keyword: redirection • Complex Redirection
· you can have whatever file you want to be · can modify stdin, etc, file descriptors from
stdin, or stdout/stderr, using the shell the cmd line
· redirect file to stdin · stdin=0, stdout=1, stderr=2 : are file de-
scriptors (integers)
Ù cmd1 < file1
· file descriptors 3 to 9 are free for you to use
· You use the < to have file be stdin for the
· examples:
program
· 2>&1
· redirect stdout to file
· 1>/dev/null
Ù cmd2 > file2 · 1>file is same as >file
· So you use > to redirect the output · 2>&1 1>&2 (this won’t actually swap
Ù Some notes on redirecting output stderr and stdout)
· 3>&2 2>&1 1>&3 (this will swap the
Ù the file cannot already exist (if you have
two)
NOCLOBBER set)
Ù won’t execute if it does Ù demo this concept
Ù since this is set by default on knuth you Ù not every program takes things from stdin or
need to be aware of it sends output to stdout
· You can append to the end of a file (add Ù for example can’t do
lines of standard output at the end without
writing over what is already there, useful Ù emacs < myfile
for log files) by doing the following Ù instead have to send myfile in as an argument to
Ù cmd4 >> file emacs

12
10 Another way to combine Ù example: cd is a builtin, its a part of the
shell and not a stand alone program like
programs say emacs is
• The Back-Tick Ù need to have some sense of the basic, underlying
abstractions of how the computer works - not on
· you may remember the ‘ as being a special
a bits and byte levels, but higher up
character
· ‘ is located on the key above tab Ù exercise: back to the layer thing, how the com-
puter is organized
· is called a back-tick or back-quote
· anytime you surround a string with back- Ù again is a layer thing - bottom up
ticks the shell will treat it as a command
Ù hardware - the machine itself - just electri-
and then replace the string with the output
cal signals
of the program
Ù develop on the board a better layered model
Ù kill -HUP ‘cat /var/run/sshd.pid‘
Ù user | shell - applications - libraries | kernel
Ù this will restart your ssh server, since ‘cat..‘ - modules | hardware - remote systems
returns the PID of sshd
Ù kernel - a mediator between the hardware
· another example (and remote systems) and the user - pro-
Ù emacs ‘grep -l error *.c‘ vides a set (library) of functions for con-
trolling the hardware that every program
Ù this will let you edit all of the files that have
you would write would need (ex. drawing
the word error in them
text to the screen, writing to disk)
Ù lots of other uses, mainly in shell scripting
Ù applications - do the things that you want
the computer to do
11 You Ought to Know this by Ù libraries contain useful code that you’d
rather not reimplement
now
Ù the user - running applications and making
• Review sense of the electrical signals that the com-
puter processes - only here do things have
· Philsophy and History of Unix any ”meaning”
· Piping, Redirection, Back-Ticks Ù in understanding unix you don’t so much about
· Finding Help the hardware - leave that to the engineers
· Users and Groups Ù really only care about the applications layer -
· Some Basic Commands how to make programs do what you want
· What the Shell is and why its cool Ù but in order to fully appreciate and use unix you
· Few Other tidbits have to understand at some level how applica-
tions interact with the kernel, and what a kernel
Ù if you don’t know what we’re talking about
is
here, then you’ll want to review before go-
ing on • What is a process, program

Ù what is a program and what is a process?


12 Processes and the Kernel - Ù ask what a program/executable is - just the
Actually running programs file containing instructions
Ù is very broad: does it matter what kind of
Ù do most of this on the board instructions?
Ù some of the commands we’ve been running Ù the instruction could be machine code
haven’t actually been ”programs” in the sense Ù could be byte code like in java or com-
normally described piled python

13
Ù or could be an interpreted scripting lan- Ù kernel does more than this
guage
Ù take Operating Systems if you want to write a
Ù what is process?
kernel
Ù a program in action
Ù an abstraction provided by the kernel Ù take CS105 to learn more about how Computer
Systems work
Ù every process thinks its the only one
running - runs in a self-contained envi- Ù as users all we care about is running programs,
ronment with the kernel being respon- leave the other stuff to the CS geeks
sible for enforcing this abstraction
Ù how is a program different from a pro- Ù and unix is very good at running programs
cess?
Ù so what does this have to do with anything?
Ù how many copies of each do you normally
have? Ù not a whole lot other than making you un-
Ù 1 for executable (one file containing the derstand that you can run multiple pro-
binary code) grams with fear of them crashing the sys-
tem like on some other systems we could
Ù can have different versions but those
mention
don’t count
Ù n for processes • Making Forrest Run
Ù why would you have more than one process
· unix is a multitasking environment
of a single program?
Ù can run multiple processes at the same
Ù need them to be doing different things
time
- two xterm windows for example
Ù no fear of them interacting
• The Kernel? Ù each process runs in its own little envi-
Ù how do the processes get run? ronment isolated from others
Ù unless you WANT them to interact!
· the job of the kernel is to manage the
very key!
many processes on the machine and run
them in some sane order so that the · unix is a multiuser environment
system is both responsive and produc- Ù can handle multiple users using the sys-
tive tem at the same time
· the kernel also provides an interface to
Ù so if you are going to run multiple programs
all the hardware on the system
from the shell(s) it would be helpful if you
· this interface is uniform across archi- knew how to manage them
tectures and hardware
Ù with GUIs most of these things are not
· so you always call read() though the
really an issue, but its still very use-
disk might actually be just a network
ful to have some idea in case you mess
share somewhere in asia
things up
Ù does the kernel just run processes? basi-
Ù can open as many xterms as you want,
cally, and do what processes need done
but at some level you need to know
Ù what might be the other jobs of the ker- this...
nel? Ù from a single shell session can run
Ù manage devices about as many programs as you would
Ù reading/writing files ever want to - constrained by memory
Ù handling system calls and processing speed
Ù enforcing system policies - security for Ù normally though you are only ever run-
example ning one program at once in a shell -
Ù allows for multiple users on a single sys- this is referred to as the foreground pro-
tem cess
Ù abstracts a common interface for differ- · we’ll assume one shell and that you need to
ent hardware - creates an abstration run multiple programs

14
· is called ”job control” · will want to demonstrate that pine can
Ù why would you need this even though we not be suspended by default
have GUIs? • Background Processes
Ù because you won’t always have a GUI,
· when we type
may end up working at just a terminal
Ù cmd
• Making Forrest Run
· this program is the one we’ll be running in
· three types of processes: foreground, back- the foreground
ground, suspended · to run a program in the background we type
· foreground process is the one that receives Ù cmd &
keyboard input (if interactive) and prints to · this causes the program to run, but not in
the terminal the foreground - so it won’t be receiving
Ù mv .. any input that you type unless you switch
to that program
Ù ls ..
Ù just because a process is backgrounded does
Ù rm ..
not mean it isn’t running, and that sus-
Ù sometimes you want to run other programs pended process aren’t running
Ù say stop reading a man page to copy a file, Ù you could have a copy operation running in
or read email the background
Ù could open another terminal - do you Ù music program in the background
really want to do this for every pro- Ù also note that the system has to run all
gram? what if you only have a limited these, so no free lunch!
number of terminals?
Ù remember: limited by RAM, hard drive
· or could ”suspend” the process in
speed (swapping), and cpu power
which case it stops executing
· or put it in the ”background”, and con- · to switch back to the process you just sus-
tinue to let it run pended you use the fg command

· background process don’t recieve input, but · can of course suspend multiple processes
can still print to the terminal · demo two other man pages, also suspend
· suspended processes just aren’t doing any- · will have three jobs in the background with
thing - but are not terminating cmd line open
· open emacs in background and switch to it
• Sending the Right Signals
Ù emacs &
· recall that C-c (SIGINT) kills the program Ù do some thing on the cmd line
you are currently running
Ù fg
· so does C-\ (SIGQUIT)
Ù this is one of the most useful things to know,
Ù sometimes its C-d (sends an EOF – usually so remember it!
for interactive programs)
Ù why this is useful - give this example sce-
· to suspend a running process use C-z nario
(SIGTSTP)
Ù are logged in remotely with no X ses-
Ù common mistake to think that the program sion
has just suddenly died Ù have an emacs window open and
Ù people unfamiliar with the system will just change a bit of code
start over, being angry and frustrated at Ù too slow to exit emacs and restart
the work they ”lost” Ù so press C-z to suspend
Ù do a demo of suspending a process Ù then can rebuild from the cmd line
Ù demo reading ”man page”, suspending, and Ù then can switch back to emacs to con-
using pine tinue working

15
Ù really only need to remember C-z to sus- Ù continues job n in the background
pend and fg to bring the process back Ù also cover %+ and %-
Ù draw on chalk board what is happening when Ù what happens if we background emacs?
you suspend or background a process what happens if we suspend emacs and let
Ù shell -> process -> shell it continue in the background with bg?
Ù shell -> (splits) -> shell (-> process) Ù now that we can manage a few processes
Ù and more what about the processes on the rest of the
machine?
• Mid-level management
· use the jobs command to list all the jobs
that you have running
13 Understanding process own-
Ù jobs ership
[1] + Suspended man page • Process ownership
[2] Suspended man perl · every process on the machine has a par-
[3] - Suspended man printf ent/child status
Ù the number identifies each job · so when you log in and get a shell that pro-
Ù we use the fg command to switch back to cess will act as the parent to all the other
which ever job we want using the number processes you create on the machine
Ù by default fg will bring back the job · there is one master process on the machine,
with the plus sign init
Ù this is the current job · every process is a descendent of init
Ù the minus sign was the job that used to · every orphaned process is a child of init
be the current job
· a zombie is a process which has exited but
Ù the rest of the output of jobs is obvious hasn’t been reaped by its parent
Ù Stopped means it doesn’t receive any Ù draw out a parent/child diagram from init
cpu time down to the shell
Ù running means just that
· init -> getty -> your
Ù example to go back to man perl we would shell (zsh) -> your programs
type
· init -> sshd -> your
Ù fg %2 shell (zsh) -> your programs
Ù nifty trick: if we know the name of the pro- Ù parents can query for the exit state of a
cess, say we have a single emacs process process
along with other can quickly switch back to
emacs by using a pattern matching scheme • Process Ownership
for fg
· each user on the system can own processes,
Ù instead of %n, you have &?xxxx where xxxx so when you log in the shell that you see is
is the first part of the name of the running owned by you
process
· to identify all the processes on the machine
Ù obviously you need enough of the name each process is given a PID or process id
to disambiguate the process that uniquely identifies the process
Ù so returning to emacs could be as simple as · you use the ps command to see the pro-
typing cesses that are running
Ù fg %em Ù run just by itself (no switches) will just
Ù don’t have to use jobs and look there, just print out your process that are being run
do a single fg command from the terminal you logged in on
Ù can also do some of the same tricks with bg Ù to see all processes
Ù bg %n Ù ps -aux

16
· can see what everyone is running with this Ù then press the ’u’ key; then type your user-
Ù point out which column is the process id name, or another username if you want to
see their processes
Ù pids are not always 5 digits
Ù init for example will always have its Ù then enter
PID equal to 1 Ù demo this feature also
Ù on OS X pids are generally 3 or 4 digits Ù to get help on the other commands top of-
Ù linux tends to be 5 digits fers use the ’h’ key when running top
Ù will most always between 2 and 32000 Ù to quit top press the ’q’ key or C-c
Ù so why is this useful? Ù top is really nifty, so spend a few minutes
• How to be like Charlie Manson trying to figure it out
Ù digression: what would happen if say, we
Ù what if we want to kill a runaway process
were root and we typed
or any other process
Ù kill 1
Ù imagine you have a java application in an
infinite loop Ù what would happen? is that even referring
· this would be a good time for CS60 stu- to a process?
dents to pay attention Ù in general killing a parent does not kill
· don’t want it running forever, so want to child, child is orphaned and becomes a child
kill that process of init
· we use the kill cmd to do this Ù init is the exception
Ù kill [PID | %n] Ù yes it is - init (or launchd on Tiger) is given
PID of 1
Ù two ways to kill
Ù so typing kill 1 while root would kill you
Ù (1) if the process is running in the back-
entire system - basically all processes stop
ground, can use background number
and the system has to be rebooted
Ù (2) can use the process id (PID)
Ù this is a bad thing and should be avoided
Ù also demo
Ù killall <proc name> • Process priority
Ù we can use ps to find the process id Ù what is process priority?
Ù demonstrate using ps to find the id and kill
· priority defines how much time the partic-
the process
ular process should get from the machine
Ù but how did we know that the process was
· processes with a low priority gets run less
out-of-control in the first place?
often than a process with a high priority
· nifty utility on unix called top
· as a user you can’t actually change the pri-
Ù will list out the processes in order of cpu ority, only root and the scheduler can do
time that
Ù demonstrate top - point out the PID column · what you can do is set the *niceness* of the
Ù can use this to see if any of your processes process
are out of control
· a higher nice value means that the process
Ù should be able to point out several aspects of is nicer to other processes on the system,
top giving them more time to get stuff done
Ù memory usage, cpu time, state and pri- Ù so a high nice value leads to a low priority
ority and a low nice value leads to a ”normal”
Ù top is made more useful by seeing only the priority
processes that you own Ù to set the niceness of a process you use the
Ù first start top nice cmd
Ù top Ù nice cmd

17
Ù if you are going to be running a long and Ù or just when you want to write to stdout
fairly intensive job on knuth please use, and Ù usage
be, nice!
Ù echo "text string"
Ù nothing worse than locking the system up
for fifty people while you try to compute · or
prime numbers ;-) Ù echo text string and more text
Ù show how mprime has been niced on Ù before using you should really read the man
odin page (it has some very interesting options)
Ù to use nice run it like such
Ù cat
Ù nice [-n NUM] cmd args... NUM=0 to
19 (lowest) (root for 0 to -20 (highest)) Ù like echo, instead takes file names as argu-
ments
Ù find a demo for nice
Ù prints the contexts of each file in order to
Ù check out the man page for more info
standard out
Ù does nice have an effect?
Ù one of the more useful commands, espe-
Ù nice will have an effect if the process is *cpu cially if you want to look at a small text
bound* meaning that it requires the proces- file
sor more than anything, doesn’t really need
any input Ù cat is short for concatenate - you may think
they should have used ”cons”
Ù on *IO bound* processes not so much since
they wait for input anyway, and while wait- Ù usage
ing the system will do other things, won’t Ù cat <file1> <file2>
have much effect Ù if no file is given will take its input from
Ù now know all about managing processes and stdin
how the system handles the Ù end the text stream with C-d
Ù will then print back to stdout
14 Some programs to remem- Ù most useful when combined with redirec-
ber tion and piping

Ù this is a list of programs that we consider to be Ù clear


the most useful or that are basic to being able Ù clears the screen (also C-l) - useful for when
to use a unix system your terminal has filled up with junk and
Ù locate, echo, cat, clear, less, head, tail you’d like to make it go away
Ù clear
Ù locate
Ù more
Ù allows you to find files on the system
Ù has to search a database so not always up- Ù is a pager
to-date Ù sometimes you have more text than can be
Ù usage fit on a single window, so need to way to
see the entire text of a document
Ù locate <filename>
Ù more lets you view the entire text of a docu-
Ù accepts wildcards for files which we’ll get to
ment, but is limited in that you can’t scroll
later but which you may already be familiar
up
with...
Ù use the space bar to advance to the next
Ù echo screen
Ù prints whatever arguments that you give it Ù less
to standard output
Ù useful for when you want to write/append Ù less is also a pager
some string to a file Ù less is a better pager than more however

18
Ù everyone knows the old adage ”less is Ù ”+NUM” : shows all but the first NUM
more”, there is also another pager called lines of a file
most
Ù now on to some other commands
Ù most > less > more
Ù have already had exposure to this program Ù the next two are both searching commands -
through the man command one is for searching through stdin, the other for
searching through a directory tree
Ù man opens up its man pages in less (not by
default but generally) Ù grep
Ù but of course can use less to view more than
just man pages Ù allows you to search for a regular expression
within a file
Ù usage
Ù often though you just want to find a string
Ù less <file> within a file, not everything matching a cer-
Ù and some of the key commands to make less tain pattern
do what you want - interactive Ù this is really just a copout since we don’t
Ù arrows move man page up/down left/right want to show you regex’s until later
Ù space bar moves a screen at a time Ù so to search through a single file
Ù to search would type /pattern, then press Ù grep [switches] "string" file1 ... fileN
enter
Ù grep has some useful switches that make it
Ù demo this feature nicer to work with
Ù typing / or n again moves to the next in- Ù ”-c” : instead of printing lines that match
stance of pattern will instead count the number of lines that
Ù N moves to the previous instance match and print that instead
Ù b : back - like page up Ù ”-n” : print the line number on which the
pattern was found
Ù f : forward - page down
Ù ”-H” : prints which file the pattern was
Ù head found in
Ù prints out the first n lines of a file to stdout Ù ”-r” : if a directory name is given will recur-
Ù useful for when you have a sorted list and sively descend through the hierarchy look-
want only the first n ing for matches in all the files in that tree

Ù by default (if n is not specified) will print Ù there are also different versions of grep, so
out the first 10 you’ll want to check out the grep man page,
as usual
Ù usage
Ù head [-n XX] <file> Ù find

Ù tail Ù at its most basic level find just descends


through a directory tree and prints out the
Ù like head, in that it will print out the last n names of whatever files it finds there
lines instead of the first
Ù so you can replicate (in functionality but
Ù more useful in that files, especially log files, not speed) the locate command with
are often appended to
Ù find ./ | grep pattern
Ù can then follow these files for as long as they
are open and see what is being added to Ù this is generally much slower than locate,
them but has to go through the entire file sys-
tem starting at ./ (which is where ever you
Ù usage currently are)
Ù tail [-n XX | -f | +NUM] <file> Ù but find is much more complex than this
Ù ”-n” : gives last XX lines of file and can do locate all on its own
Ù ”-f” : keeps file open Ù find ./ -name glob -print

19
Ù the ”-name” switch will search for the glob, Ù show this output
and ”-print” will print it to standard out
(note that glob != regex) Ù at the end of this would be a good time to poll to
figure out what other programs people would like
Ù find can of course do much more interesting to know about;
things
Ù try to find out what this does, and why you Ù questions like ”what is the program that does
would need it ....?” sorting, calculator, etc.
Ù find ./ -name ".foo" -perm +444

-exec chmod -R a+r {} \; 15 Files, programs and file, and


Ù these are other commands which are useful, but
the filesystem
not exactly necessary to know Ù what is a file? some review from before
Ù w Ù a file is a stream of bytes; a file isn’t always nec-
Ù prints out who is on the system essarily on disk
Ù w Ù traditionally think of a file as something like a
text file or mp3
Ù which
Ù in unix though we know that everything is a file:
Ù prints out which command exactly will be
directories, devices, etc.
executed when you just type cmd
Ù useful for when you want to figure out either Ù review the unix security model
which program you are using or where the
program you are using is Ù users have a home directory

Ù which cmd Ù users not allowed to do random (dangerous)


things - only allowed to do those things that
Ù du they have permission to do

Ù displays the amount of space that a file (or Ù there is a superuser that can do anything
directory) is taking up on disk Ù users belong to groups, and have a primary
Ù du file1 file2 file3 ... group

Ù to get the output to be a bit more useful, • Files


will want the ”-h” switch
Ù how should an OS handle security for files?
Ù demonstrate this
how do you tell who owns a file? what’s a
Ù if file is actually a directory, du will recurse good way to manage sensitive files?
through the directory printing out the size
· files have their own security model
of each file as its gets them finally printing
out how large the directory is in total at the · files have an owner and a group that they
end belong to
Ù this is not very useful, so pass in the ”-s” · user is you
switch to suppress the extra output · group is the group the file belongs to
Ù this creates a useful ”sizeof” command · so when you create a file, the file is created
Ù du -sh file1 file2 file3 ... with you as the owner, your primary group
as the group the file belongs to, and all the
Ù df permissions on
Ù shows a detailed listing of the amount of · so we’ve defined two categories that a file
space being used on each file system that is belongs to, what else is left?
mounted · what about the other people on the system?
Ù show the output of and explain · so in all a file is concerned with three sets
Ù made nicer by the ”-h” option of people: user, group, others

20
· others are by definition everyone else on the Ù examples of common permissions settings
system Ù 666: everyone read, write
· besides owner, group, etc., files have per- Ù 777: do anything to file
missions associated with what people inside Ù 022: user can’t do anything, group w,
of these classifications can do with the file other w
• File Permissions Ù 744: user do everything, group read,
other read
Ù based on the security model above what
Ù as an exercise write out the octal permis-
kinds of permissions should we have? what
sions in the rwxrwxrwx form
kind do we have?
Ù so just because you own the file doesn’t nec- • Changing Ownership
essarily mean that you can write to it, or
Ù do an ls -l on some files
execute it if its a shell script
Ù demonstrate the two following programs
· so what things can you do to a file?
· chgrp - group
· well unix defines only three (there are more
that could be useful, but unix is old and the · chown - owner
spec. hasn’t changed with the times) • Changing permissions
· these three are: read, write and execute
· chmod - will also change ALL aspects of a
· can set these three permissions for any cat- file if wanted
egories: user, group, others
Ù demonstrate some standard examples -
· so the permissions look like: rwx|rwx|rwx make file world readable
· the first rwx is the user, the second group Ù switches u, g, o and the +-= syntax, with
and the third others perms r,w,x,X
Ù have an exercise to figure out what the per- Ù also cover -R for recursive
missions of several files mean
Ù since may want to change all files in a
Ù so when you see rw-rw-rw what does this directory
mean? etc.
Ù now on to octal permissions • special files - directories
Ù so rather than writing out a bitmap each · directories are files too, though can’t
time you want to say what the permissions read/write to it as you would a regular file
of a file are, what would be a more compact · but because are files, they have to have the
representation? same permissions that other files on the sys-
Ù use permissions base 8 tem has
Ù they are permissions base 8, because each · unfortunately the permissions on a direc-
permission group (user, group, others) can tory act a bit differently (which you might
be represented by a number from 0 to 7 expect)
Ù if we write out 0 to 7 in binary we see that Ù list how permissions affect a directory file
we require 3 bits (hmmm!) · r can list with cd
Ù _ _ _ so these bits have value 4 2 1 and are · x can access the file within
mapped to r w x
· w means can write a file to that directory
Ù 421
Ù so 7 looks like 1 1 1 • dot-files
Ù and 4 looks like 1 0 0 Ù so when you type the ls cmd, do you see
Ù combining this we can write permissions out every file?
as a group of three numbers 0 to 7 Ù obviously not since I’m asking this question
Ù permissions look like XXX Ù do an example of a hidden file - .hidden or
Ù the first X is user, the second is group, and ”.you don’t see me”
the third is others Ù is that all the files in the directory?

21
Ù not necessarily... Ù therefore .. is the parent directory of
Ù ls -a the current directory
Ù ../ is the same as ..
· ”-a” switch will show ALL files in the direc-
tory Ù cd ..
· this is useful because dot-files are generally Ù so can probably guess that is you see
used to hide configuration information for Ù /foo/bar/../baz/box/
programs away from the user Ù you know where this refers to? where
· also keeps the configuration information in does this go?
your home directory so that each user can Ù answer: /foo/baz/box/
have their own settings as they like Ù so you can’t ignore these, but can be
Ù some examples of .files useful for when you need to craft a path
Ù one common one you may care about is your to a file
.zshrc, this controls some of the settings you
• other special files
have in your shell as you login
Ù another is .emacs - controls the settings for · tilde (~) denotes your home directory
emacs
Ù cd ~
Ù you can also have .directories
· will take you to your home directory, a plain
Ù these are directories that are hidden cd will do the same
Ù do the same thing as .files (hide config info),
· username denotes the home directory of
but for programs that may have many con-
that user
figuration files that need to be hidden
Ù so is it a nice thing that just ls doesn’t show • relative and absolute paths
all the files in a directory by default? yes,
but is still historical crap Ù can refer to files in two different ways
Ù a better solution might be possible Ù what if you have written a program and
Ù show a gftp listing of my home on farmboy want to access a certain file - config.cfg
Ù so when you do a ls -a, do you recall seeing Ù how do you refer to this file?
two files one named ”.” and the other ”..”? · two ways: relative and absolute path
· probably not but these are two special files
· absolute paths never change and describe in
· the file named . is this directory ”absolute” terms where a file is in the tree
Ù do a literal translation for whatever di- Ù emacs /home/mkegel/school/fall05/graphics.txt
rectory in on board
Ù so if you type · absolute paths will always start with /
Ù cd . · you are starting at the root and working
Ù you stay in the same place down the tree
Ù what if you see a dot in the full path · relative paths start with a ./ - the directory
name of a file? tree before the ./ is not known
Ù ex. /foo/bar/./baz/box/ Ù don’t necessarily know where the program
Ù where is this referring? will be stored, but always know where the
Ù to /foo/bar/baz/box/ config.cfg file will be in relation to the pro-
Ù so it means nothing, should just ignore gram
it Ù or should at least know where it will be
Ù ./ is the same thing as .
Ù can refer to it then by saying
· the file named .. is the parent directory
Ù cmd ./one/two/config.cfg
Ù you should already know that if you
want to go up in the directory hierarchy Ù the ./ is filled in by the OS or the shell -
that you type depending on context

22
16 Links - symbolic and • Creating Links
hardlinks Ù ln [OPTIONS] target link_name
Ù so now that we’ve confused you this much what · the basic syntax is backwards, so to ”link”
if you wanted to have two files have the same emacs to emacs-21.2 you would type
name? Ù ln emacs-21.2 emacs
Ù why would you want this? · to create one pass the ”-s” switch to ln
Ù ln -s emacs-21.2 emacs
Ù how about when you upgrade software - could
have the program named emacs, but the /real/ · this will create a soft-link
name might be emacs-21.2, an older version Ù show output of ls -l
could be emacs-20.7
· symlinks don’t always work, for example
Ù you want to continue to access this program when trying to get to things through ftp
through the same name, but have it always refer - have to mount instead
to the new version

Ù how might you do this? 17 File globbing


• File Links Ù most people are probably familiar with file globs,
but for now, what is a file glob?
· unix uses a thing called links
· links are a special type of file which trans- Ù why might you want file globbing?
parently redirect you to a different file
· many times will want to work with multiple files
· so you can type emacs, but the OS will
know that you really mean emacs-21.2 · say you want to mv or cp or rm all the files
in a directory
· there are two types of links: soft and hard
links · how do you do that?

• Types of links · you use a thing called file globbing

· a hard link, which was what we created · is very similar to UNIX regular expressions, but
above, is literally a different name for the should not confuse them
same file
· globbing is provided by the shell
· the new name is stored in the directory its
supposed to be in so takes up virtually no Ù this is key - is one of the many complaints the
memory people have about unix
· the hard link literally points to the same · the shell is responsible for expanding globs
data on disk
· a hard link cannot cross file-systems (say Ù programs generally only take file names and
you have two different hard drives and want don’t generally do pattern matching
to have one file refer to another) Ù why should is be the responsiblity for the shell
· you cannot hard link to a directory to expand globs? is this a good thing or a bad
· a soft-link is different and more flexible one?
· it is a separate file (not just an entry in a Ù some people see this as a bad thing - for you you
directory file) that the OS interprets as the don’t have to care
real file, basically just a type of pointer
· some standard globs - see page 658 U.P.T.
· soft-links can go across file systems and can
point to directories · * - matches all files in a directory (not dot-
· in practice you should probably almost al- files though) - match anything
ways create soft-links · will not match dot-files, however
· can link both directories and regular files · ? - match one character

23
· [a-z] - any character a to z - character Ù and then we come to the way the directory
classes structure is organized
· (x|y|...) - match either x or y - this is Ù what do I mean by this?
not pipe! Ù so with windows you were probably com-
· {a,b,...} - expands to ”a” ”b” fortable having all your programs be in-
stalled to C:\Program Files\Program; so
Ù give some examples and ask what they would everything you installed was in a common
match place
· a[a-z]* Ù what if some one decided one day
· ?[c]*c to instead install everything to
C:\Your Programs\Program and
Ù can put these anywhere in the expression C:\My Programs\Program; but all the
old ones you had would (by default) install
Ù by far the most common is to do
to the old directory (Program Files)
Ù ex1*ex2 Ù unix has a ”unified hierarchy”
Ù *ex3 Ù this would be quite a pain
Ù ex4* Ù in unix the same thing exists, but across the
different unixes that exist
Ù to match dot-files have to put a dot explicitly
Ù of course unix is supposed to be indepen-
Ù to match all dot-files in a directory would dent of the directory structure on which its
do .* working, just makes it a pain for people in
Ù show listing of ls .* real life
Ù note that this also lists ”.” and ”..” Ù this is an historical artifact
Ù so what would happen say if we did this... Ù in order to gain market share one thing they
Ù chmod -R a+r ./ teach you in business school is to differen-
tiate your product
Ù as root from /home?
Ù so when unix began to come out in various
Ù much badness is what...since more than just commercial flavors the companies changed
the down directory will be changed things a bit to reflect how their product was
Ù .. follows to its parent and so you’ll also different
change all the files in all the directories at Ù the most obvious and easy way to change
the same level and below in the tree an OS is to change where you put things
Ù draw out to illustrate Ù so you end up with apache (a web daemon)
being in four different places in four differ-
18 The tree of life - How the ent flavors of unix
Ù luckily the madness has continued!!!!!
Unix directory tree is orga-
Ù linux suffers from the same problem though
nized perhaps not to the same extent
• Unix Directories Ù where things are put in debian is different
from gentoo, is different from...
Ù now that you understand how to work with
Ù FreeBSD has some resemblance to the linux
files, how to work with programs should
directory structure similar
know a little bit more about the environ-
ment you are working in Ù just to limit the confusion we’ll just go over
Ù unix is in some way a very structured envi- the basics which apply to most unix sys-
ronment tems, except for OS X which is very differ-
ent
Ù 35 years of tradition have specified how you
do certain things and why they are the right · *now the real material*
things to do Ù draw a /

24
Ù so we know that everything in unix starts Ù libexec/ system daemons and system
at / utilities (executed by other programs)
Ù so how do we decide what goes under this? Ù local/ executables, libraries, etc. not
included by the basic operating system
Ù we don’t get to so much, though if you want
create your own distro... Ù sbin/ system daemons and system util-
ities (executed by users)
· so lets start with something you already
Ù share/ architecture-independent data
know /home
files
• General Directories Ù share/man/ manual pages
Ù src/ storage for kernel/other source
Ù cd into each directory as they are being dis-
code under linux
cussed
Ù /var/ multi-purpose log, temporary, tran-
Ù /home is where the heart (and your home-
sient, and spool files
work) is
Ù log/ misc. system log files
Ù in some sense this is true since this is where
the home directories of all the users on the Ù /lib - shared libraries and kernel modules
system are stored Ù /opt - larger static packages - where you
Ù the home directories generally have the would install KDE for example
form /home/username, though this isn’t Ù /boot - kernel image and other boot files
strictly necessary, just useful
Ù this is where the linux kernel gets put
Ù rest of this is taken from
Ù is sometimes a separate partition - Red
Ù man hier (on OSX, also present on Hat does this
FreeBSD, haven’t seen it on Linux) Ù probably a good idea since if your main
Ù see also pg 822 UPT one gets fried can still boot
Ù /bin/ user utilities fundamental to both Ù /mnt - temporary/permanent mount point
single-user and multi-user environments for devices
Ù /dev/ block and character device files fd/ Ù devices can be mounted anywhere, so
file descriptor files; see fd(4) one solution, if you have lots of hard
Ù /etc/ system configuration files and scripts drives, optical drives, cameras, scan-
ners, etc. is to make lots of mount
Ù X11/ configuration information for X points under /mnt, so /mnt/dvd/,
· NFS: exports /mnt/ipod, etc.
· system: group, passwd, and shadow Ù is this a good thing?
· shell: profile Ù all up to your own preferences
· sudo: sudoers Ù /mnt is meant for (at least in FreeBSD)
· network and inet: hosts, hosts.allow, a single device that won’t be mounted
hosts.deny, hosts.equiv for any length of time
· lots of other stuff Ù would mount to a different point, say
Ù /sbin/ system programs and administra- /ipod, so mounted right from root
tion utilities fundamental to both single- Ù are multiple solutions, could also have
user and multi-user environments a /vol (/Volumes)
Ù /tmp/ temporary files
Ù /usr/ contains the majority of user utilities 19 Some special devices
and applications
• Special files
Ù bin/ common utilities, programming
tools, and applications · /dev/null
Ù games/ Unix games, also fortune · this is a sink for input, you can send
Ù include/ standard C include files anything here and it will just go into
Ù lib/ archive libraries the void

25
· useful for when you need a place to get Ù printenv
rid of output, but don’t want to waste · what about shell variables?
space on the disk
Ù set (lists everything: functions, shell, and
· if read from will give you an EOF env. variables)
· /dev/zero Ù pg 705 has a good listing of pre-defined en-
· produces an infinite stream of zeros if vironment variables
read from, good for when you need to
null something out • Common Variables
· if written to will just give a permission · good ones to know about - will be all caps
error (?? Is this always true ??)
· PATH - when you type in a command
· /dev/random and /dev/urandom these are the directories that the shell
· when read produces a random stream will search for the cmd you have typed
of bytes · HOME - where your home directory is
· permission error when try to write to located
· HOSTNAME - name of the system you
are on
20 Environment Variables and · SHELL - full path name of shell you are
Shell Variables using
· USER - who you are logged in as
• Variables · UID - your user id
· these are special variables that all programs · TERM - type of terminal you are using
know about
· they exist in the background and let pro- 21 The Different Shells
grams know about certain things
· where to find programs to run, which Ù now is a good time to talk about what shell you
editor to use, etc are using
· serves the same purpose as the registry Ù there are in fact many different shells!
(sort of) in windows
Ù you’ve probably been using zsh, the knuth de-
Ù cover the distinction between the two
fault, all this time with ever knowing it
· environment variables are inherited
across all shells you may have and all Ù there are a multitude of shells, each with differ-
programs that you run ent strengths and features
· shell variables are local to each instance Ù most fall into two different categories
of a shell that you may have
· each shell gets a clean slate of shell vari- Ù those based on sh, which was the first shell
ables when it starts Ù those based on csh
· analogy - programming: global versus
Ù give proper pronunciation lesson
local scope
· variable naming convention Ù the default on knuth is zsh, which is a hybrid of
sh and csh
· CAPS are environment
· lower-case are shell Ù other shells include bash (probably the most
widely used shell), tcsh, ksh (Korn-Shell)
· please follow convention!!!
Ù you generally only ever care about environ- Ù other random shells: ash, fish, pdksh (free ver-
ment variables sion of Korn-shell)
Ù show listings of the variables Ù probably the best (certainly the most fully fea-
· how do you see what environment variables tured) shell is zsh
you have? Ù we recommend that you use either zsh or, if you
· simple use the printenv command have to, bash

26
Ù why use zsh? Ù you can see the output of a variable by using
the echo cmd
Ù its just plain nifty
Ù echo $ENV
Ù will tab complete many very cool things,
and even not so cool ones Ù have to remember the $

Ù e.g can tab complete kill and man for ex- Ù demonstrate this for the students
ample Ù the most common thing to do is to add
paths to PATH
Ù also we are only going to demonstrate for
this shell, you have the tools to figure out Ù may have to do this for a class
how to do the same things for yourself on Ù may want to do this depending on
your own shell which box you are logged into
Ù so how are they different? what really distin- Ù from the cmd line for zsh we would type
guishes a shell? Ù echo $PATH (to see path)
Ù in terms of user interface, shells are almost iden- Ù export PATH=$PATH:/newpath
tical · or -

Ù shells provide the ability to run more one com- Ù export PATH=/newpath:\$PATH
mand at a time Ù the two are different!

Ù if you string commands together in a file, with Ù first puts the new path at the end
some control structures like if, while, and case Ù the second puts the new path at the front
statements: you get a language Ù is important because when searching for the
Ù called shell scripting cmd to run, the shell will take the first one
that matches, so you want to make sure its
Ù fundamental different between csh and sh - csh matching the right cmd
is more based on C syntax Ù unfortunately you would have to do this
every-time you logged in
Ù sh is just funky and has a weird syntax, but for
shell scripting is better (better control over out- Ù would get boring
put and input)
• Setting Env. Variables
Ù so when we say a shell is based on another, what
Ù maybe there is an easy way fix this prob-
we really mean is that zsh or bash can run scripts
lem?
written for sh
Ù ask crowd for suggestion
Ù and that zsh and bash offer scripting options that
Ù real answer: this is why you have dot-files
weren’t available with sh
Ù we know dot-files hold configuration infor-
Ù the same for tcsh mation
Ù we won’t show you shell scripting yet, but you Ù zsh is no exception to this rule
will learn about it Ù zsh has several files that hold login infor-
mation

22 Setting Environment Vari- Ù /.zprofile


Ù /.zlogin
ables Ù /.zshrc
• Setting Env. Variables Ù and more
Ù depending on default setup will want to
Ù to set environment variables in zsh show how to do this add path
Ù export NAME=value Ù the one that you care about is .zshrc
Ù can also do this Ù once you change your .zshrc it won’t imme-
Ù NAME=value; export NAME diately take effect

27
Ù either you have to log out and restart your · so you can refer to a complicated, or repet-
shell or you can apply the change from the itive, command with a smaller, easier to
cmd line type alias
Ù to apply from the cmd line use the source Ù you always want to use these when you are
cmd typing some long string over and over again
that you cannot just tab complete
Ù source .file
Ù is very much like a macro
Ù will cause the shell to read the configuration
information, and apply the changes Ù what is one common thing that you might
be typing?
Ù but this doesn’t cause the shell to reset its
old configuration, it just overwrites what it Ù how about ”ssh user@knuth.cs.hmc.edu”
changes Ù so to create an alias we need a name: how
Ù so your PATH will be two copies of your about sshtoknuth
old path followed by the added directory Ù from the cmd line would type: (spacing is
assuming you put it at the end important)
Ù not always what you want, and shouldn’t Ù alias sshtoknuth=’ssh user@knuth.cs.hmc.edu’
cause any problems unless you’re doing Ù can also use ” when creating alias’ (good if
some funky things in your .zshrc or .zlogin you want to reference env. variables)
Ù *changing the prompt* Ù this alias is, of course, only good for so long
Ù well if you can change your PATH what as the shell is open
other things can you change? Ù once you quit the alias is gone
Ù lots of things, but the most fun to change Ù the basic syntax is
is the prompt
Ù alias name=’cmd arg1 arg2’
Ù demonstrate some nifty prompts in zsh and
Ù but what would be a good name? some-
how to change them
thing better than sshtoknuth
Ù other shells options
Ù the whole point of the alias (for users) is to
Ù shells are complex beasts and offer loads of make your life easier
options, one of the most annoying is the
Ù so want to choose a simple name that is easy
shell beep
to remember
Ù to alert you to an error when you mistype
Ù how about: knuth
something, the shell will cause the com-
puter to emit a beep Ù says what you want, is simple and short
Ù this is the most annoying thing ever Ù what would have happened though if we’d
aliased this to ssh?
Ù to turn it off we would use the setopt com-
mand that zsh has Ù question of precedence...and the alias wins

Ù setopt nobeep Ù in zsh and bash when you type something


on the cmd line that you want to be taken
Ù we would put this line in our zshrc; by de- as a cmd, then if that text string has been
fault it should already be there aliased to something else it will be replaced
Ù there are lots of options so check them out! Ù is this a good thing or bad thing?
Ù may want to read through the default zshrc Ù used in the appropriate places is OK
to see what’s set
Ù in general, however, is a bad thing because
then you get used to a potentially broken
23 Using alias in the shell or misleading command action.
Ù almost always want to use a new name
• ALIAS
Ù one example is ls
Ù it is a builtin cmd that allows you to ”alias” Ù by default what you get on knuth is not
one set of commands to another /usr/local/bin/ls

28
Ù instead you get ”/usr/local/bin/ls -F” • Useful Applications
Ù do an example of what each outputs Ù these are almost all graphical tools and are
Ù discuss why this might be a bad thing, tell essentially replacements for programs that
about -G on GNU ls (hide groups) students would be using on either their Mac
Ù should watch which options you pass by de- or Win box
fault · web browser - firefox and mozilla, kon-
Ù so how did I know that all of you had this? queror
Ù demonstrate the defaults alias’ that people · text editing - jedit, emacs, vi
have in zshrc · word processing - abiword, open office
Ù for certain programs you’ll want the same · document creation - latex
behavior every-time you login · spreadsheet - gnumeric, open office
Ù so to make an alias more or less permanent · music - xmms, amarok (my favorite)
you would put it in your zshrc
· movies - totem, xine, mplayer, vlc
Ù so if you wanted the alias we talked about
before you would add this line · graphical terminals - eterm, aterm, xterm,
rxvt, gnome-terminal, konsole
Ù alias knuth=’ssh mkegel@knuth.cs.hmc.edu’
· mail - thunderbird, evolution, pine/mutt
Ù spacing is important so follow closely through xterm
· pdf viewers - xpdf, kpdf, gpdf
24 More about zshrc · ftp - ncftp or lftp (cmd line), gftp

Ù so the zshrc can set options and aliases for you, · cd burning - k3b
what else can it do at login? · aim - gaim, kopete
· bit torrent - azureus
Ù the at login is important since you can do this
all from the cmd line; just gets old if you want · file manager - konqueror, nautilus
it every time · spell checkers - ispell and aspell
• creating files and the umask · Windows like desktop - GNOME, KDE
· window managers - fluxbox, xfce, window
· by default every permission is ON, so its maker, enlightenment
your job to turn them OFF
· the umask is the thing that does this
Ù show in zshrc 26 su and sudo
Ù written out in octal permissions (remember Ù up to this point have been fairly general, infor-
those?) mation applies to almost all users of a *nix sys-
Ù write out the permissions of the umask tem
Ù ask what the permissions of a newly created Ù this information is really only useful to those
file would look like who will using Linux on their own system, not
Ù ask if the results are good or not so much for those people who are just going to
Ù what might be a good umask; what would be log into knuth
a bad one? • sudo and su
Ù discussed before how use
25 UNIX programs for Win- · su allows you to assume the role of the supe-
dows geeks ruser (aka root; is default behavior) without
having to directly log in as root
· there are many different tools that unix offers Ù su <username>
· you should now know how to make them work · allows you to become that user (changes
together and some of why they work as they do, your UID and GID), but have to know their
but don’t exactly know what tools are out there password

29
· if you have first become root, you can do Ù one of the most basic things to configure
su <username> without a password on a newly installed linux system are the
· sudo allows you superuser powers without partitions and how the hard disk will be
having to become root - more fine grained managed
control than with su Ù in windows separate partitions would just
· sudo is flexible, you can have limited pow- magically show up (not always, but most
ers, or have the whole shebang times)
· sudo privileges are controlled in Ù more too it than this, but the management
/etc/sudoers of the disks was done mostly by the oper-
ating system
· is a regular text file, like almost all config-
uration files on unix Ù also in windows the most the you would re-
ally ever do is put the disk/partition at G:,
· can add powers for users, or groups
H:, or so on
· generally only modifiable by root (kind of a
security risk otherwise) Ù other devices were located at D: (generally
a cdrom or dvd drive)
Ù can also get a shell out of sudo by using the
-s switch Ù A: - floppy drive
Ù C: - your main disk - the disk you booted
• editing /etc/sudoers
off of
Ù demonstrate adding · these ”locations” are known as mount
Ù mkegel ALL=(ALL) ALL points
Ù %wheel ALL=(ALL) ALL · in unix you don’t have a:, c:
Ù %special ALL=(root) · instead disks and other devices can be
NOPASSWD:/usr/bin/myprog ”mounted” most anywhere
Ù joe user local host=/sbin/prog Ù that is if you have a newly installed hard
· the basic format is: drive you have to decide where in the
USER HOST=[(RUN_AS)] CMD filesystem tree that drive will be accessed
Ù see page 992 of unix power tools Ù this is what makes the file system(s) trans-
parent to both users and programs
Ù would you ever want to let a person run emacs
Ù draw out a diagram of how multiple
(or vi) with sudo? no: they would have access
drives/cdroms/devices can be accessed on a
to a root shell...bad thing need to watch out for?
filesystem tree
reason why you would want: let them edit sys-
tem files Ù generally mount stuff in /mnt, but can be
anywhere
Ù why might you need to use sudo and su? what
kinds of things should be be an administrator to Ù can also mount what you might consider
do? important directories from other drives
Ù for example: /home on my system is an-
other partition that I created for the ex-
27 Mounting partitions press purpose of holding everything that I
would want in my home directory
Ù this information is really only useful to those
who will using Linux on their own system, not Ù / holds everything else I need for my sys-
so much for those people who are just going to tem, but is separate from what I have in my
log into knuth home directory
Ù this is a nice because it is very flexible and
• mounting partitions*
completely transparent to you and to any
Ù when managing a system you will almost program that you might run
always be modifying ”files” that ”normal” Ù one example of this being done is with
users should never touch each of your, and mine, home directories
Ù these files are generally configuration files on knuth

30
Ù your home directories are mounted locally Ù hard drives are generally given the designa-
on knuth when you log in, but where they tion hdXN, where X is [a-f] and is the drive
were before that doesn’t really matter and N is [0-9]
Ù obviously knuth can’t have enough physi- Ù so hda1 would be the first partition on
cal space to hold everything that we might the master hard drive
want - home directories, staff accounts, Ù hda2 would be the second partition
projects, random data, backups Ù hdb would be the second hard drive
Ù all this disk space is provided transparently Ù drives have up to 4 primary partitions,
across a network through something called and can have more logical partitions
NFS or network file system (or No Fucking Ù generally if you just say hda, or hdb you
Security, nfs assumes you are on a trusted mean the entire device and not any one
network) partition
Ù so your home directories are located on one · scsi drives (like what you’d have under
machine, while you are working on knuth firewire) are denoted the same as hard
and doing things to your files, editing, com- drives except with sd instead of hd nota-
piling, etc. tion
Ù however that was just a setup for what · another would be /dev/input/mouse0 (on
you’ll likely do on your own machine my system at least) for the mouse
• mounting devices Ù most often though will deal with hard drives
and partitions or other comparable (NFS)
· in linux, we won’t worry about bsd or other things
*nix, devices are located in a /dev like we
Ù so then how do you get a partition
talked about before
mounted?
· a device is of course just a file - is actually
Ù easy with the mount cmd
a special file called a device node - tells the
system where in the system the device can Ù mount is fairly smart, can usually figure out
be found (for PCI devices major and minor which file system is on the device
number of device) Ù note to windows users: Most any computer
Ù device node != regular file or directory with XP will use NTFS which is a filesystem
that linux can only read (trouble writing)
Ù one thing to note: the linux 2.4 kernel is
different from the 2.6 kernel in one major Ù FAT32 will work just fine
way -> the 2.6 kernel does use the /dev Ù to use mount you need a directory on which
filesystem approach to devices; 2.6 is just to mount, so if we were to mount /dev/hda4
more intelligent and doesn’t enumerate ev- on /mnt/windows, then windows would
ery possible device need to be a directory under /mnt
Ù in the 2.4 kernel every device that you • mount command
could possibly have attached to the sys-
tem was enumerated and given a device Ù also to use mount you have to be root or
node in /dev have root powers (sudo)!
Ù in 2.6 the kernel is a little bit smarter Ù mount /dev/device /mnt/mntpoint
about devices now and can handle
Ù to specify the file system use the -t switch
them with some intelligence
like such
Ù for you this won’t matter too much un-
Ù mount -t vfat /dev/hda4 /mnt/windows
less you’re trying to hook in some ex-
otic device Ù to un-mount the device use the umount
Ù or if you have sound/video/usb prob- command like such
lems (or want to use these devices) then Ù umount /dev/windows
you’ll get to know /dev pretty well Ù also cover --bind (GNU, different on BSD)
Ù generally a device will be owned by root and - can bind one filesystem to two points -
in one of several groups: disk, video, audio, useful for when symlinks won’t work (net-
root, etc. working stuff)

31
Ù when you are setting up a system its quite helpful · to modify fstab you have to be root
to know which tools are available (wouldn’t want random users messing with
this)
Ù most systems you get will either come with win-
dows installed, in which case the entire hard · fstab tells the system which filesystems are
drive is one windows partition to be mounted at boot time
· also lets the system know which mount
Ù or you got a new hard drive
points the system has
Ù (Windows Case) · so say I wanted to mount windows then all
i would have to type would be
Ù You’ll want to get your hands on a program
called Partition Magic Ù mount /mnt/windows
Ù another good program is GNU Parted · and the system would auto-magically know
Ù if you know where to look on the network what to do
(ffs.muddstudents.com *ahem*) you might · you also use fstab to mount NFS exports,
be able to find the program but we won’t cover that here
Ù it can do most anything that you’ll want, · do not fubar this file, will make you un-
including converting, moving and resizing happy
Windows partitions
Ù you probably shouldn’t let Partition Magic
touch any linux partitions
28 Using cron
Ù (New HD Case) · cron is a daemon that runs in the background
and schedules tasks
Ù you’ll want to use cfdisk or fdisk to create
new partitions Ù such tasks might be backing up a hard drive,
updating the locate database
Ù use mkfs to then format the partition
Ù this configuration information is stored in a
Ù (General Case)
crontab file
Ù the cmd df will show how much space is
available on each mounted filesystem Ù generally you just give crontab a file containing
what you want done and when - this can be done
Ù df as a regular user
Ù the cmd du will show you how much space
something is taking up Ù crontab mysched
Ù du <filename> · also, edit the crontab with crontab -e
Ù useful alias
· mysched will then contain single line entries for
Ù alias sizeof=’du -sh’ each task that you want cron to do
• /etc/fstab · each line is formatted like this
Ù So I mentioned that I have /home mounted Ù mins hrs day-of-month month weekday [username] cmd
from another partition
Ù this happens on boot, but how? Ù the username is only there in BSD systems, but
check your local docs first
· /etc/fstab is a configuration file that lets
the system know which devices are to be Ù you can give a single number (7), range (3-8),
mounted, where, who can mount, and with list (5,7,9), or * to indicate all valid numbers for
what other options (executable flag being a given entry
one)
Ù show my /etc/fstab - point out what each Ù mins: 0 to 59; hrs: 0 to 23 (military time); day:
column is 0 to 31; month: 1 to 12 weekday: 0 (Sunday) to
6 (on BSD it goes from 1-7 with 1=Sunday)
· there are other things in things in fstab than
just hard drives, but you can ignore them Ù show a random cron example

32
Ù something scary for Friday the 13th, and some- 31 Sharing files under linux
thing cheerful for Christmas, and school starting
every Monday Ù use a package of programs called samba

Ù cron can also make your life easier by just run- Ù allows you to share and access windows network
ning things on a consistent schedule - say you shares
need to run things on a hourly, daily, weekly, or Ù really easy to use but has issues with some of the
monthly basis newer versions of the windows sharing protocol
- may not be able to access all files
Ù cron will run scripts that you put into
/etc/cron.xxxx/ Ù better yet why don’t you just use ftp?

Ù where xxxx is daily, hourly, etc Ù ftp is much easier to setup, and you don’t have
to dirty yourself with Windows slime
Ù on those intervals

Ù you’ll have to be root to put stuff in these direc- 32 Emacs and vi


tories though
Ù please use emacs, or if you have a graphical en-
vironment something like jedit
29 Wine - Windows ”emula- Ù vi should only ever be used in the most desperate
tion” for linux of circumstances

Ù emacs and vi
Ù use wine!!
Ù hand out reference cards
Ù there isn’t much else to tell you
Ù demonstrate some nifty tricks (M-q for one)
Ù install wine Ù how to exit and save
Ù F10 is menu in emacs
Ù do this from the cmd line
Ù vi is a modal editor - some people ”just get
Ù wine WinApp.exe this” way of editing

Ù hope for the best; does not work for every appli- Ù not our goal to teach students to use these
cation Ù and besides by this point in the semester you
should already know how to use emacs
Ù if you want to run games (or itunes, or quick-
time) then you’ll need a thing called cedega
33 More topics to talk about
Ù there is good documentation, you just need to
find it Ù shell scripting

Ù go read Unix Power Tools


30 Cygwin Ù demonstrate a for loop on the cmd line how-
ever
Ù can get lots of GNU programs
Ù perl and python
Ù can get X11
Ù both powerful interpreted languages
Ù runs on Windows Ù easy to learn and use; generally only need
to know one but should learn at least one
Ù get a poorly implemented version of a terminal Ù checkout these sites:
Ù but can do unix-y type stuff on windows if you Ù diveintopython.org and python.org
absolutely have to Ù perl.org and perl.com

33
Ù cannot stress how useful knowing a lan- Ù grep ’fo?’
guage like one of these is
Ù would match f, fo
Ù learn it!
Ù grep ’f[oe]*’ file1
Ù logging in through ssh
Ù how to X forward with ssh (-X | -Y) Ù would match f, fo, foe, fe, fee, etc, but not
fie
Ù how to use ftp/sftp and scp
Ù grep ’^f[oe]{1,2}$’ file1
Ù why ftp is a bad idea - any password send in the
clear Ù would match fo, fe, foe, feo, fee, foo, but
not f or fooo

34 Things I left out Ù grep ’f[^aieou]*’

Ù speaking of other frustrating things also mention Ù would match f followed by 0 or more non-
the C-q and C-s control flow ”bug” vowels

Ù C-q resume console I/O Ù grep ’(fa|e)o*’

Ù C-s stop console I/O Ù would match fa, faoooo, e, eoooo, but not
fae

35 Regex Info Ù Note that grep ’fo+’ is equivalent to grep ’foo*’

Ù 3 slides for this topic Ù sed is generally used for substitutions. There are
two really handy commands for sed: s(ubstitute)
Ù metacharacters: and d(elete). Coupled with addresses, you can
do some really cool stuff.
. * + ? [] ^ $ { } \ () |
Ù By default, sed applies the commands to every
Ù POSIX classes: [:alnum:] [:digit:] [:blank:] etc line. You can change this by specifying an ad-
dress pattern to use. Patterns can be line num-
Ù two main commands that we’ll cover that use bers, regexes, or things like $.
regular expressions: sed and grep
Ù No address -> apply commands to every line
Ù grep just searches for the given pattern
Ù 1 address -> only on lines that match the address
Ù sed can do that too, but often sed is used to
string
replace things
Ù grep ’foo’ file1 Ù 2 addresses -> only on lines between the first line
that matches the first address and the first line
Ù returns every line that contains the string that matches the second address
’foo’
Ù You can negate addresses by appending a !.
Ù grep ’^foo’ file1
Ù So, just ’d’, without an address, will delete every
Ù returns every line that starts with foo line.
Ù grep ’foo$’ file1 Ù 1d will delete just the first line.
Ù returns every line that ends with foo Ù 1,5d will delete lines 1-5.
Ù grep ’^foo$’ file1
Ù $d will delete the last line. This is not the same
Ù only lines that are exactly ’foo’ as $ = end of line.

Ù grep ’fo*’ Ù /^$/d will delete every blank line.

Ù would match f, fo, fooooooooooooooooo, Ù 1,/^$/d will delete from the first line until the
etc first blank line.

34
Ù All the addressing stuff also applies to the ’s’ 37 screen info
command, but to clarify exactly what ’s’ does,
we won’t mix addressing and s. Ù 2 slides for this topic
Ù screen is basically a way to have multiple ter-
Ù s/tree/cat/ replaces the first instance of ’tree’ minals inside one standard terminal window.
with ’cat’. (Think job control, but instead of pausing one
job to go back to the shell, you could simply have
Ù s/tree/cat/g replaces every instance of tree a bunch of shells running, only one of which was
with cat (g for global). in the foreground at any given time.) In the days
of large resolutions and good terminal emulators
Ù s/tree/(&)/g replaces every instance of tree and window manager, screen isn’t that handy
with (tree). & references the entire match. for doing stuff on a local session. It is, however,
extremely useful for managing shells on remote
Ù s/tr\(ee\)/\1/ replaces ’tree’ with ’ee’. sessions.

Ù s/\(tr\)\(ee\)/\2\1/ replaces ’tree’ with Ù Make a new (default) session:


’eetr’. Ù screen

Ù s/\(tr\)\(ee\)/\2\2\2\1/ replaces ’tree’ with Ù Resume the default screen:


’eeeeeetr’.
Ù screen -r
Ù s/<\([^>]\)>\(.*\)<\/\1>/[\1]\2\[\/\1]/ Ù or
replaces ’<b>foo</b>’ with ’[b]foo[/b]’
Ù screen -x (like -r, but better – allows multi-
heading)
36 tr info Ù Make a new screen session with a title:

Ù 1 slide for this topic Ù screen -S foo


Ù Resume foo:
Ù tr is very simple. It takes stuff it finds in the first
argument and replaces it with the stuff it finds in Ù screen -x foo
the second argument. It does take position into
account: something found in the 17th position in Ù You can also use just -x f, if you don’t have any
the first argument will be replaced by the thing other screens starting with ’f’.
in the 17th position of the second argument. Ù If you’ve got a un-resumable screen session, tidy
it up with:
Ù echo "abc" | tr ’a’ ’z’ = zbc
Ù screen -wipe
Ù echo "abc" | tr ’[a-z]’ ’[A-Z]’ = ABC Ù Show available sessions:

Ù echo "abc" | tr ’[abc]’ ’[cba]’ = cba Ù screen -ls


Ù The screen commands I use a lot:
Ù It’s also handy for deleting newlines or some
other character you may have a particular ha- Ù ^a c make a new shell
tred of: tr -d ’\n’
Ù ^a <space> switch to the next shell
Ù This is what you would do if you really hated Ù ^a k kill the current shell
your grader
Ù ^a d detach; go back to the original shell leaving
Ù strip out all comments the current session intact
Ù remove all newlines Ù The screen commands I use not so much:
Ù get a failing grade on that assignment Ù ^a l redraw the screen in case things got funky

35
Ù ^a a send a literal ^a

Ù ^a ? (shift /) show help

Ù ^a " (shfit ’) show current shells in a list

36

You might also like