% Linux Installation and Getting Started    -*- TeX -*-
% basic.tex
% Copyright (c) 1992, 1993 by Matt Welsh, Larry Greenfield and Karl Fogel
%
% This file is freely redistributable, but you must preserve this copyright 
% notice on all copies, and it must be distributed only as part of "Linux 
% Installation and Getting Started". This file's use is covered by
% the copyright for the entire document, in the file "copyright.tex".

\section{First Steps into UNIX}
Before we begin, it is important to note that all file and command names
on a UNIX system are case-sensitive (unlike operating systems such as MS-DOS).
For example, the command {\tt make} is very different than {\tt Make} or
{\tt MAKE}. The same hold for file and directory names.

\subsection{Moving around}
Now that we can login, and know how to refer to files using pathnames,
how can we change our current working directory, to make life easier?

\index{directory!structure!moving around in with cd@moving around in with {\tt cd}}
\index{cd@{\tt cd}|(}
The command for moving around in the directory structure is
{\tt cd}, short for ``change directory''.  You'll
notice that many often-used Unix commands are two or three letters.
The usage of the {\tt cd} command is:
\begin{tscreen}
cd \cparam{directory}
\end{tscreen}
where \cparam{directory} is the name of the directory which you wish
to change to. 

As we said, when you login, you begin in your home directory. If
Larry wanted to move down into the {\tt papers} subdirectory, he'd
use the command
\begin{tscreen}
/home/larry\# {\em cd papers} \\
/home/larry/papers\# 
\end{tscreen}
As you can see, Larry's prompt changes to reflect his current working
directory (so he knows where he is). Now that he's in the {\tt papers}
directory, he can look at his history final with the command
\begin{tscreen}
/home/larry/papers\# {\em more history-final}
\end{tscreen}

\index{directory!parent!.. to refer to@{\tt ..} to refer to}
\index{directory!. to refer to@{\tt .} to refer to}
\index{parent directory!.. to refer to@{\tt ..} to refer to}
Now, Larry is stuck in the {\tt papers} subdirectory. To move back
up to the parent directory, use the command
\begin{tscreen}
/home/larry/papers\# {\em cd ..} \\
/home/larry\#
\end{tscreen}
(Note the space between the ``{\tt cd}'' and the ``{\tt ..}''.) 
Every directory has an entry named ``{\tt ..}'' which refers to
the parent directory. Similarly, every directory has an entry
named ``{\tt .}'' which refers to itself. Therefore, the command
\begin{tscreen}
/home/larry/papers\# {\em cd .}
\end{tscreen}
gets us nowhere.

You can also use absolute pathnames in the {\tt cd} command.
To {\tt cd} into Karl's home directory, we can use the command
\begin{tscreen}
/home/larry/papers\# {\em cd /home/karl} \\
/home/karl\#
\end{tscreen}

Also, using {\tt cd} with no argument will return you to your own
home directory.
\begin{tscreen}
/home/karl\# {\em cd} \\
/home/larry\#
\end{tscreen}
\index{cd@{\tt cd}|)}

\subsection{Looking at the contents of directories}\label{sec-ls}
\index{directory!listing contents of|(}
\index{ls@{\tt ls}|(}
\index{listing directory contents|(}
\index{files!listing|(}
Now that you know how to move around directories you probably think,
``So what?''  The basic skill of moving around directories is fairly
useless, so let's introduce a new command, {\tt ls}. {\tt ls} prints
a listing of files and directories, by default from your current
directory. For example:

\begin{tscreen}
/home/larry\# {\em ls} \\
Mail \\
letters \\
papers \\
/home/larry\#
\end{tscreen}

Here we can see that Larry has three entries in his current 
directory: {\tt Mail}, {\tt letters}, and {\tt papers}. This
doesn't tell us much---are these directories or files? We can use the 
{\tt -F} option on the {\tt ls} command to tell us more.

\begin{tscreen}
/home/larry\# {\em ls\ --F} \\
Mail/ \\
letters/ \\
papers/ \\
/home/larry\#
\end{tscreen}
From the {\tt /} appended to each filename, we know that these three
entries are in fact subdirectories. 

\index{executable!defined}
\index{file!executable!defined}
Using {\tt ls -F} may also append ``{\tt *}'' to the end of a filename.
This indicates that the file is an {\bf executable}, or a program which
can be run. If nothing is appended to the filename using {\tt ls -F}, the
file is a ``plain old file'', that is, it's neither a directory, or an
executable.

In general, each UNIX command may take a number of options in addition
to other arguments. These options usually begin with a ``{\tt -}'',
as demonstrated above with {\tt ls -F}. The {\tt -F} option tells {\tt ls}
to give more information about the type of the files involved---in 
this case, printing a {\tt /} after each directory name.

If you give {\tt ls} a directory name, it will print the contents of
that directory.
\begin{tscreen}
/home/larry\# {\em ls\ --F papers} \\
english-lit \\
history-final \\
masters-thesis \\
notes/ \\
/home/larry\#
\end{tscreen}

Or, for a more interesting listing, let's see what's in the
system's {\tt /etc} directory.

\begin{tscreen}
/home/larry\# {\em ls /etc} 
\begin{verbatim}
Images          ftpusers        lpc             rc.new          shells
adm             getty           magic           rc0.d           startcons
bcheckrc        gettydefs       motd            rc1.d           swapoff
brc             group           mount           rc2.d           swapon
brc~            inet            mtab            rc3.d           syslog.conf
csh.cshrc       init            mtools          rc4.d           syslog.pid
csh.login       init.d          pac             rc5.d           syslogd.reload
default         initrunlvl      passwd          rmt             termcap
disktab         inittab         printcap        rpc             umount
fdprm           inittab.old     profile         rpcinfo         update
fstab           issue           psdatabase      securetty       utmp
ftpaccess       lilo            rc              services        wtmp
/home/larry#
\end{verbatim}\end{tscreen}

(For those MS-DOS users out
there, notice how the filenames can be longer than 8 characters, and can
contain periods in any position. It is even possible to have more than one
period in a filename.) 

Let's {\tt cd} up to the top of the directory tree, using ``{\tt cd ..}'',
and then down to another directory: {\tt /usr/bin}.

\begin{tscreen}
/home/larry\# {\em cd ..} \\
/home\# {\em cd ..} \\
/\# {\em cd usr} \\
/usr\# {\em cd bin} \\
/usr/bin\#
\end{tscreen}
You can also move into directories in multiple steps, as in {\tt cd /usr/bin}.

Try moving around various directories, using {\tt ls} and {\tt cd}.
In some cases, you may run into a foreboding ``{\tt Permission denied}''
error message. This is simply the concept of UNIX security kicking in:
in order to {\tt ls} or to {\tt cd} into a directory, you must have
permission to do so. We'll talk more about this in 
\index{directory!listing contents of|)}
\index{ls@{\tt ls}|)}
\index{listing directory contents|)}
\index{files!listing|)}
Section~\ref{sec-file-perms}.

\subsection{Creating new directories}

\index{directory!creating}
\index{mkdir@{\tt mkdir}}
It's time to learn how to create directories. This involves the use
of the {\tt mkdir} command. Try the following:

\begin{tscreen}
/home/larry\# {\em mkdir foo} \\
/home/larry\# {\em ls -F} \\
Mail/ \\
foo/ \\
letters/ \\
papers/ \\
/home/larry\# {\em cd foo} \\
/home/larry/foo\# {\em ls} \\
/home/larry/foo\# 
\end{tscreen}

Congrats!  You've just made a new directory and moved into it.  Since
there aren't any files in this new directory, let's learn how to 
copy files from one place to another. 

\subsection{Copying files}
\index{file!copy}
\index{copy files}
\index{cp@{\tt cp}}
Copying files is done with the command {\tt cp}:

\begin{tscreen}
/home/larry/foo\# {\em cp /etc/termcap\ \ .} \\
/home/larry/foo\# {\em cp /etc/shells\ \ .} \\
/home/larry/foo\# {\em ls\ --F} \\
shells\ \ \ \ \ termcap  \\
/home/larry/foo\# {\em cp shells bells} \\
/home/larry/foo\# {\em ls\ --F} \\
bells\ \ \ \ \ shells\ \ \ \ \ termcap \\
/home/larry/foo\# 
\end{tscreen}

The {\tt cp} command copies the files listed on the command line to the
file or directory given as the last argument. Notice how we use the
directory ``{\tt .}'' to refer to the current directory.

\subsection{Moving files}
\index{file!move}
\index{move files}
\index{mv@{\tt mv}}
A new command named {\tt mv} moves files, instead of copying them.
The syntax is very straightforward.

\begin{tscreen}
/home/larry/foo\# {\em mv termcap sells} \\
/home/larry/foo\# {\em ls -F} \\
bells\ \ \ \ \ sells\ \ \ \ \ shells \\
/home/larry/foo\# 
\end{tscreen}

Notice how {\tt termcap} no longer exists, but in its place is
the file {\tt sells}. This can be used to rename files, as we have
just done, but also to move a file to a completely new directory.

\blackdiamond {\bf Note:} {\tt mv} and {\tt cp} will overwrite 
the destination file (if it already exists) without asking you. Be careful 
when you move a 
file into another directory: there may already be a file with the same 
name in that directory, which you'll overwrite!


\subsection{Deleting files and directories}
\index{file!delete}
\index{delete!files}
\index{rm@{\tt rm}}
You now have an ugly rhyme developing with the use of the {\tt ls} command.
To delete a file, use the {\tt rm} command. (``{\tt rm}'' stands for
``remove''). 

\begin{tscreen}
/home/larry/foo\# {\em rm bells sells} \\
/home/larry/foo\# {\em ls -F} \\
shells \\
/home/larry/foo\#
\end{tscreen}

We're left with nothing but shells, but we won't complain. Note that
{\tt rm} by default won't prompt you before deleting a file---so be
careful.

\index{delete!directory}
\index{directory!delete}
\index{rmdir@{\tt rmdir}}
A related command to {\tt rm} is {\tt rmdir}. This command deletes
a directory, but only if the directory is empty. If the directory
contains any files or subdirectories, {\tt rmdir} will complain.

\subsection{Looking at files}
\index{files!viewing contents of}
\index{more@{\tt more}}
\index{cat@{\tt cat}!to view file contents}
The commands {\tt more} and {\tt cat} are used for viewing the
contents of files. {\tt more} displays a file, one screenful at a
time, while {\tt cat} displays the whole file at once.

To look at the file {\tt shells}, we can use the command
\begin{tscreen}
/home/larry/foo\# {\em more shells}
\end{tscreen}

In case you're interested what {\tt shells} contains, it's a list
of valid shell programs on your system. On most systems, this includes
{\tt /bin/sh}, {\tt /bin/bash}, and {\tt /bin/csh}. We'll talk about
these different types of shells later.

While using {\tt more}, press \key{Space} to display the next page
of text, and \key{b} to display the previous page. There are other
commands available in {\tt more} as well, these are just the basics.
Pressing \key{q} will quit {\tt more}.


Quit {\tt more} and try {\tt cat /etc/termcap}. The text will probably fly
by much too quickly for you to read it. 
The name ``{\tt cat}'' actually stands for ``concatenate'', which is the
real use of the program. The {\tt cat} command can be used to concatenate
the contents of several files and save the result to another file.
This will be discussed later.

\subsection{Getting online help}

\index{UNIX!manual pages for}
\index{help!online}
\index{manual pages}
Almost every UNIX system, Linux included, provides a facility known
as ``manual pages'', or ``man pages'' for short. These man pages
contain online documentation for all of the various system commands,
resources, configuration files, and so on. 

\index{man@{\tt man}}
The command used to access man pages is {\tt man}. For example, if
you're interested in finding out about the other options of the
{\tt ls} command, you can type
\begin{tscreen}
/home/larry\# {\em man ls}
\end{tscreen}
and the man page for {\tt ls} will be displayed.

Unfortunately, most of the man pages out there are written for those
who already have some idea of what the command or resource does. For
this reason, man pages usually only contain the hardcore technical 
details of the command, without a lot of tutorial. However, man pages 
can be an invaluable resource for jogging your memory if you forget
the syntax of a command. Man pages will also tell you a lot about
the commands which we won't tell you in this book. 

I suggest that you try {\tt man} for the commands we've already gone
over, and whenever I introduce a new command.  You'll notice some of
these commands won't have man pages.  This could be for several reasons.  
For one, the man pages haven't been written yet (the Linux Documentation
Project is responsible for man pages under Linux as well. We are gradually
accumulating most of the man pages available for the system). Secondly, the 
the command might be an internal shell command, or an alias (as discussed in 
Section~\ref{sec-shells-cmds}),
in which case it would not have a man page of its own. One example is
{\tt cd}, which is a shell internal command. The shell actually processes
the {\tt cd}---there is no separate program which contains this command.

