\documentclass[11pt]{article}

\setlength{\oddsidemargin}{0.0in}
\setlength{\evensidemargin}{0.0in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9.0in}
\setlength{\parindent}{0in}
\setlength{\parskip}{2mm}

\usepackage{times}
\usepackage{hyperref}
\usepackage{fancyvrb}
\usepackage{relsize}


% \renewcommand{\thefootnote}{\arabic{footnote}}

\begin{document}

\title{Unix---the Bare Minimum}

\author{Norman Matloff}

\date{September 27, 2005 \\
\copyright{}2001-2005, N.S. Matloff}

\maketitle

\tableofcontents

\newpage

\section{Purpose}

The information here is intended to be a review for those who have had
a bit of prior exposure to Unix, and as a quick introduction to Unix
for those who have never seen it before.  (Some of the material may
be new even to those with some prior exposure to Unix.)

\section{Shells}

A \textbf{shell} is a program\footnote{Yep, it's a program, likely written
in C.  You could write a shell too, with a little knowledge of Unix
processes.} that inputs Unix commands from the keyboard and relays them
to the Unix system for execution.  Shells typically include various
shortcuts for users to use in stating their commands, and also a
programming feature, in which users can make programs out of sets of
their commands.

The first popular Unix shell was the Bourne shell, named {\bf sh}.  It
still very popular, in a modernized version called the Bourne Again
Shell, {\bf bash}.  

There are many other shells.  Our UCD CSIF system accounts are set up so
that your login shell is the C-shell; its official name, taken as a
command itself, is \textbf{csh}.  We actually use an extension of
\textbf{csh}, called \textbf{tcsh}.  

\subsection{Switching from One Shell to Another}

If you wish to temporarily use another shell, just type its name in an
existing shell, e.g.

\begin{Verbatim}[fontsize=\relsize{-2}]
$ bash
\end{Verbatim}

That will start an instance of the {\bf bash} program (executing within
your original C-shell, but that won't matter).  Or if someone has given
you a {\bf script}, i.e. a file {\bf x.sh} containing {\bf bash} commands,
type

\begin{Verbatim}[fontsize=\relsize{-2}]
$ bash x.sh
\end{Verbatim}

If you wish to make {\bf bash} your default shell, use the {\bf chsh}
(``change shell'') command:

\begin{Verbatim}[fontsize=\relsize{-2}]
$ chsh -s bash
\end{Verbatim}

This introduction will focus mainly on the C-shell, but once you learn
the material here, it will be easy to learn other shells, say BASH.
There are many tutorials on the latter on the Web, e.g. at
\url{http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html}.

\subsection{Shell Conveniences}

A nice feature of modern shells is \textbf{command line editing}.  Make
sure to use it!  Here is how it works in the C and BASH shells:

Say I wish to type a command

\begin{verbatim}
cd /altpkg/tex/texmf/fonts/tfm/public/cm
\end{verbatim}

(As you will see later, the {\bf cd} command changes directories, but don't
think about that now.)  Suppose, though, that I mistype it as

\begin{verbatim}
cd /altpgk/tex/texmf/fonts/tfm/public/cm
\end{verbatim} 

Suppose I have not yet hit the return key.  Then I can 
go back to change the ``gk'' as follows:  use the left-arrow key to go
to the `k'; hit the Delete key, which will remove the `g'; use the
right-arrow key to go to the `/'; hit the g key to put the `g'
back in; and then hit the return key to process the command.

On the other hand, if I have already hit the return key when I notice
my typing error, I can use ctrl-p to go back to my previous command.
(Use ctrl-p to go back through several previous commands, and ctrl-n
to go forward.)  Note that you can also use this to repeat (without
change) a previous command.

Once you get used to this, it saves you a lot of typing, and allows
you to concentrate better on your work.

The {\bf tcsh} (and some other shells, such as {\bf bash}) also allows
you to do \textbf{file name completion}, again a great saver of typing and
time.

Suppose for example I have a file named {\bf jack.and.the.beanstock},
which I need to copy to a file name {\bf gy}.  The command, which you
will learn later in this tutorial, is

\begin{verbatim}
cp jack.and.the.beanstock gy
\end{verbatim}

But rather than typing that long name by hand, suppose that this is
the only file which begins with ``ja''.  What I can do is type

\begin{verbatim}
cp ja
\end{verbatim}

and then hit the Tab key.  The shell will then complete that file
name for me, so that the command line on the screen will now be

\begin{verbatim}
cp jack.and.the.beanstock
\end{verbatim}

I now continue typing, adding " gy", producing

\begin{verbatim}
cp jack.and.the.beanstock gy
\end{verbatim}

and hit the return key. 

Many text editors, e-mail utility programs and so on include some
kind of file-completion feature.

Note carefully that {\bf tcsh} is an extension of {\bf csh}.  The {\bf
tcsh} version does use a different startup file, {\bf
~/.tcshrc},\footnote{The ``~/'' means your home directory; see Section
\ref{fd} below.} but will use {\bf csh}'s counterpart, {\bf ~/.cshrc},
if {\bf ~/.tcshrc} is not there.  I recommend that you NOT have a {\bf
~/.tcshrc} file (remove it if your system has already placed one there),
and that you use {\bf ~/.cshrc} instead; that way you get the same
environment when you run either {\bf csh} or {\bf tcsh}.  (Often free
software downloaded from the Web will use {\bf csh}.)  The startup file
for {\bf bash} is {\bf ~/.bashrc}.

\section{Files and Directories}
\label{fd}

Unix uses a \textbf{hierarchical} file system, meaning the following.  
When you first log in, you will be at a point in your file system known 
as your \textbf{home directory}.  Within that directory you can make 
subdirectories, and within them you can make sub-subdirectories, and 
so on.  So, your file system has a tree-like shape.  (And your file
system is in turn a subtree of the collection of all files on the
machine.)

This hierarchical system helps you to organize your files.  For example,
suppose you are taking ECS 40 and Stat 32.  You could make directories
with these names, and then keep all your files for a given class in that
directory.  Similarly, you may be looking for a job, so you might create
a directory named JobHunting, and then keep all your resume's, cover
letters and so on in that directory.

\subsection{Creating Directories}

To create a directory, use the \textbf{mkdir} command.  Suppose, for example,
you are in your home directory, and wish to make a subdirectory named
ECS40, as suggested above.  You could type

\begin{verbatim}
     mkdir ECS40
\end{verbatim}

\subsection{Moving to Other Directories}

To go from one directory to another, use the \textbf{cd} command.  For
example, if you are currently in your home directory and you have
created the ECS40 subdirectory, simply type

\begin{verbatim}
     cd ECS40
\end{verbatim}

However, suppose you are currently in the Stat32 subdirectory, and
you wish to go to ECS40.  The above command won't work, since ECS40
is not a subdirectory of Stat32.  Instead, you can type

\begin{verbatim}
     cd ~/ECS40
\end{verbatim}

with the tilde mark \~ signifying your home directory.  In other words,
you are saying, ``Change to ECS40, which is a subdirectory of my home
directory.''

The directory which is up one level in the directory tree can be
referred to as ``..''.  Thus for example, 

\begin{verbatim}
     cd ..
\end{verbatim}

would take you up to that level.  As another example, an alternate
method for moving from the Stat32 directory to the ECS40 directory
in the example above would be

\begin{verbatim}
     cd ../ECS40
\end{verbatim}

\subsection{Some Directory and File Commands}

\subsection{pwd Command:  Which Directory Are We In?}

To see which directory you are currently in (theoretically you should
know, but sometimes you might lose track of which directory you are in),
type the \textbf{pwd} command. 

\subsection{ls Command:  What Files Are Here?}

To get a list of all files you have in the current directory, use the
\textbf{ls} command.  This command is more useful, though, if you use the
-F option, i.e. you type

\begin{verbatim}
     ls -F
\end{verbatim}

This will tell you which files are executable (their names will be
appended with asterisks), and which are subdirectories (their names
will be appended with slashes).  By the way, \textbf{ls} will not report
files whose names begin with a period (there typically are several of
these); to see those, add the -a option, e.g. type `ls -Fa'.

\subsection{rm Command:  How to Remove Files}

To remove a file, use the \textbf{rm} command.  E.g.

\begin{verbatim}
     rm x
\end{verbatim}

will result in the file x being deleted.  

\subsection{cp Command:  Copying Files}

Say you have a file x and wish to make a new copy of it in a file
named y.  Simply type

\begin{verbatim}
cp x y
\end{verbatim}

Say you have files u and v and wish to copy them to files named u and
v within a directory w.  Type

\begin{verbatim}
cp u v w
\end{verbatim}

See the man page (see ``Online Help'' later in this tutorial) for
many more things you can do with cp.

\subsection{mv Command:  Renaming Files}

To rename a file, use \textbf{mv}.  E.g.

\begin{verbatim}
     mv x y
\end{verbatim}

means ``move x to y,'' i.e. take the file x and rename it as y.

\subsection{Applying Commands to Other Directories}

All these commands above can be used on directories other than the
current one.  For example, if we are currently in the directory
Stat32 but wish to know what files are in the directory ECS40,
we \underline{could} use \textbf{cd} to go to the latter directory
and then use \textbf{ls} there, but it is easier to just type

\begin{verbatim}
     ls ~/ECS40
\end{verbatim}

from Stat32, not leaving that directory.

Similarly, if we had a file z in Stat32 which we wanted to copy to
ECS40, we could type (from the Stat32 directory)

\begin{verbatim}
     cp z ~/ECS40/z
\end{verbatim}

In fact, even

\begin{verbatim}
     cp z ~/ECS40
\end{verbatim}

would work.

\subsection{Special Names for Some Directories}

The symbol ``.'' refers to the current directory.  For example, if we
are currently in Stat32 and there is a file abc in the ECS40 directory,

\begin{verbatim}
     mv ~/ECS40/abc .
\end{verbatim}

would move it to the current directory.

The symbol ``..'' means the directory one level up from the current
one.

\section{Viewing, Creating and Modifying Files}

\subsection{Text Editors}

To create a new file or modify an old one, we use a \textbf{text
editor}.  A widely-used editor in Unix and other systems is
\textbf{vi}.  For example,

\begin{verbatim}
     vi x
\end{verbatim}

would be used to create the file x, or to modify x if x already
existed.

You will need to know {\bf vi} or some other editor in order to do many
Unix operations.  See my tutorial on {\bf vi} at
\url{http://heather.cs.ucdavis.edu/~matloff/vi.html }.  You really
should use one of the modern clones of {\bf vi}, not the "plain vanilla"
one.  The two best clones are {\bf vim} and {\bf elvis}; see the above
link.

\subsection{Viewing the Output of a Command ``Slowly,'' Saving
It or Inputting It to Another Command}

\subsubsection{The ``more'' Command}

You can view a file by using an editor, but usually it is quicker just
to use the \textbf{more} command.  For instance,

\begin{verbatim}
     more uv
\end{verbatim}

would display the file uv on the screen, one screenful at a time; just
hit the space bar whenever you are ready to go to the next screenful.
If you wish to discard the remaining screenfulls, just type q (for 
``quit'').  If you want to back up a screenfull, type b.

\subsubsection{Redirection}

Sometimes you will find it useful to save the output of a command.
You can do this by \textbf{redirecting} the output to a file.  For
example, 

\begin{verbatim}
     ls > y
\end{verbatim}

will send the output of the \textbf{ls} command to a file y, instead
of to the screen.\footnote{Similarly, if a command expects input
from the keyboard, you can have it read from a file instead, by
using `$<$'.}

Some programs have another kind of output which shows up on the screen
like ``ordinary'' output, but which technically is considered separate.
The channel through which ordinary output is sent is called {\bf
stdout}, while this special kind of output, called {\bf diagnostic
output}, is sent through {\bf stderr}.  The standard input from the
keyboard is called {\bf stdin}.  You can pipe diagnostic output,
say to {\bf more} from the program {\bf x}, as follows:

\begin{verbatim}
x |& more
\end{verbatim} 

\subsubsection{Pipes}

Often it is useful to \textbf{pipe} the output of one command as input
to another command.  Say you find the output of \textbf{ls} to be
very long, zooming by on the screen before you have had a chance
to read all of it.  One solution to this problem would be to send
the output to a file and then view the file as your leisure, but
an easier, more direct method would be to pipe the output of \textbf{ls}
into \textbf{more}, i.e.

\begin{verbatim}
     ls | more
\end{verbatim}

which would allow you to see the output of \textbf{ls} one screenful
at a time; again, you would hit the space bar whenever you are
ready to go to the next screenful.  

\section{Online Help}

You can get online information on almost any Unix command, by using
\textbf{man}.  For example, to get information on all the options
available for the \textbf{ls} command (there is a very large number
of them), type

\begin{verbatim}
     man ls
\end{verbatim}

and a detailed (though terse) description of everything \textbf{ls} does
will then appear on the screen.\footnote{{\bf Note:} This is how most
people learn more about Unix---by reading these ``man pages,'' rather
than say, formal lectures in a course.} By the way, this description
will be automatically piped through \textbf{more}, so as usual, just hit
the space bar when you are ready to go to another screenfull. 

At the end of the output, there will also be pointers to other commands
related to the one requested, as well as lists of any files used by the
command, such as ``startup'' files via which the command will have
certain options set before execution.

\section{The ``script'' Command}

The \textbf{script} command is quite useful.  It gives you a full
record of your Unix session.  For example, if you are encountering
some errors which you can't fix, you could e-mail a script file
to the instructor, so that you can explain to the instructor
precisely what you did, and precisely what error messages you
got.

To use \textbf{script}, simply type ``script''.  Try it out as a test
first:  Type ``script'' and then after the prompt reappears, type
a couple of commands, say \textbf{cd} and \textbf{ls}.  Then type ``exit'',
and a file named typescript will now exist.  Look at that file;
you'll see a full record of the Unix session which you just exited
(your \textbf{cd} and \textbf{ls} commands, and their outputs).\footnote{Don't
use cursor-movement programs like \textbf{vi} within \textbf{script}, since
the latter will record the cursor movements, and thus the file typescript
will be almost impossible to view.}

If you are sending a ``script'' file to someone for help in fixing
an error, be sure to not only run the program which produced the
error but also run

\begin{verbatim}
ls -l
printenv
\end{verbatim}

so the the purpose who you are seeking advice from will know the
environment in which the error occurred.

\section{Leaving}

To end your Unix session, simply type

\begin{verbatim}
     exit
\end{verbatim}


\end{document}
