UNIX
Every computer you use has an Operating System which completes the
tasks you send it with menu selections, mouse clicks, and typed commands. The
operating system of the NeXT computer is, as you might have guessed,
UNIX.
Up until now, you may have only used menus, windows, and buttons in your
interaction with UNIX. This Graphical User Interface (GUI) is one of the
NeXT's strengths. But a far more common interface that you will find available
on every UNIX system is the Command Line Interface (CLI). In this situation
you exchange information with the computer solely through text input and output
- you type commands and the computer prints responses.
Why would you want to work with just text when you have fantastic
menu-driven programs at hand? That's for you to find out in this course.
Suffice it to say that many people enjoy the power and flexibility of UNIX's
Command Line Interface, and prefer to use it over menus, windows, and mice
wherever possible.
Hopefully the following information will be enough to get you started.
Don't worry about learning every single thing... just absorb as much as you can
and use this CheatSheet for reference as you work with UNIX in the labs.
Where to get help
Manual Pages
These handy guys may well be one of your most useful assets for finding UNIX
information quickly. In a Terminal window, simply type:
man <command>
This will bring up help on the command you type in place of <command>.
If you want help on a particular subject, use the following:
man -k <subject>
This will bring up a list of commands relevant to <subject>. You can
then use the normal man command above to get more information on any
of those listed.
CIT documentation
The packet from CIT entitled, "Introduction to UNIX Computing at
Princeton" has many nitty-gritty details that all of us often
forget. It is a good thing to bring to lab for reference. Anyway, we all know
how much fun these things are to read when you get bored. You can pick these
up in the CIT InfoCenter, at 87 Prospect Avenue.
Concepts
Userids and addresses
Every UNIX user has a unique userid. From the computer's point of view, your
userid is your only real name. People who want to contact you from outside
Princeton must type <your userid>@phoenix.Princeton.edu in
order to reach you. This whole "phrase" is your
own unique Internet address - to an experienced email-er, the
"@phoenix.Princeton.edu" means "on
the machine `phoenix', which is in the domain `Princeton', which is in the
larger domain `edu'."
Passwords
Nothing but common sense here. Everyone has their own password, and no
one should know yours. When you choose a password, don't use a real
word. If you do, there's a chance that someone will discover it, because
with a little UNIX knowledge, it's not too hard to write a program that will
discover every password that can be found in a dictionary.
Why does it matter if someone guesses your password? Unfortunately, there are
scum who would love to enter your account, using your password, and break into
other computers. You would be blamed.
If you want to change your password right now, ask your TA for help changing it
with the Preferences application.
Files
Files are "collections of related data records"
(Webster's). They are more of an intuitive concept than anything else. A
word-processing document, for example, is a file.
Directories
UNIX allows you to organize your files into directories. A
directory is like a folder, which (usually) contains related files. You can
have directories within directories, forming an entire
"tree" of directories. (If you were to draw out
the directory structure on paper it would resemble a genealogical tree.)
Along with every other UNIX user, you have a home directory. This is
where you keep all your own files (and directories). It's also the place you
find yourself when you log in. The computer's shorthand for
"your home directory" is
"~", and its shorthand for someone else's
home directory is ~<userid> . For example, you would refer to
omkensey's home directory as ~omkensey .
Whenever you want to run an executable file, you usually need to tell the
computer exactly where the file is. For example, throughout this course you
will use the handin program to submit your work electronically. Since
handin is in the ~cs111/bin directory, to run it you would
type
~cs111/bin/handin .
Luckily, there's another way. If you type a command which isn't in the current
directory, the computer will search through a list of other directories that
contain commonly-used commands. This list is called your path. If
~cs111/bin is in your path, you could merely type
handin
to run the program. To see your list of paths, type echo $path .
For more information on files and directories, see the first lab.
Disk quotas
Your disk quota is the maximum amount of information you are
allowed to store in your home directory. To get information about how much
storage you have used, and how much space you have left before you hit your
disk quota, type quota -v .
If you use the quota -v command, you will see a number labelled
"limit." This is the absolute, unyielding maximum amount of
information you can store. If you approach this limit, the computer will
let you know, and will ask you to remove some files.
Commands
This section will give an overview of some of the more useful UNIX
commands. It is by no means comprehensive, but it should give you what you
need to know for basic survival. To use these commands, type them while in a
Terminal window (see the Terminal CheatSheet).
Necessary commands
Not-absolutely-necessary-but-still-very-interesting commands
Advanced commands
For those who want to become UNIX wizards
Pipes
Herein lies the beauty and power of UNIX. With a pipe ( | ), you can send the
output of one command into the input of another. For example, if you come
across a directory containing a huge number of files, you might type
ls | more
This will send the output of the ls command into more, which
lets you view it one screenful at a time.
If you think of each UNIX command as a flow of data, pipes are the means of
controlling that flow. Normally, the effect of a command flows right out onto
the screen, the simplest case of data flow. By using a pipe, you can make that
flow of data go someplace else...to another UNIX command, for example. This is
the area in which grep truly shines. Let's look at another example.
The w command lets us see what each person on the system is doing at
the moment. Unfortunately, if you are on a large UNIX system such as phoenix,
there are bound to be many, many users. It becomes tedious to look through all
of the people to find your friends each time you check up on them.
Grep, as we know, will find any lines containing a certain string. So
we want to take the flow of data from w and
"pipe" it into grep, like this:
w | grep <name of friend>
This series of commands will execute w, but instead of printing its
output on the screen, gives it to grep. Grep in turn looks
for your friend's name and since there is no pipe after grep, the flow goes
onto the screen, printing out every line with your friend's name in it.
Voilá!
Output redirection
Filename expansion
Aliases
Regular Expressions
Say you want to search a text file for every line which begins with `a'.
You can't just use grep to search for `a' - you'll get
hundreds of lines! Fortunately, grep accepts certain special characters
- like one that represents the beginning of a line - in its
search-strings. Expressions which use these special characters are called
regular expressions, and they pop up often in UNIX.
Here is a tiny, incomplete list of regular expression characters and what they
do. Include any of these in search strings just as you would include a normal
character.
^ at the beginning stands for the beginning of a line. To find all lines
in the file foo that begin with the letter "a", you might type
grep '^a' foo.
$ at the end stands for the end of a line (it "anchors" the expression
to the end), and it's used just like ^. Type grep `a$' foo to find
every line in foo which ends in "a".
. stands for any single character, so 'r.m' could mean 'ram' or 'rem'.
.* will match anything. So m.*e will find anything which starts in m and
ends in e.
For a more complete list, look for regular expressions in the
man page for ed.
UNIX Cultural note: In the old days, UNIX users had to use ed
as a text editor (you have Edit or vi). ed is only
cute until you try it. In ed, you can't look at your document while
you edit it. Instead, you type commands to edit your file, and use your
imagination to see how it looks as you make changes.
Anyway, one of ed's commands searches for text, and accepts regular
expressions. The command is g/<regular expression>/p, and it does the
same thing as grep. Actually - and this is where the culture shows
itself- "grep" can be found in the
abbreviation for ed's search command: "g/re/p".
Another related note... some UNIX wizards know ed so well that they
use it by choice. To them, arrow keys and pointing devices are cumbersome and
annoying.