Computer Science 1 -  Lab 1
                       Introduction to Unix


 Introduction
 ============
 This guided lab will introduce you to the Unix command line
 environment.  The objectives of this lab are:

   - Work with files in your account including copying files,
     renaming files, and removing files.

   - Use Manual Pages

   - Organize files including using commands to create directories,
     changing to a directory, viewing the contents of a directory,
     and copying files to a directory.


 This lab is a completely guided lab.  All you need to do is
 carefully read the instructions and execute the commands.  You are
 encouraged to take notes about the commands that you execute.

 In order to use this lab, you will need to be connected to a
 Unix-like machine.  This can be a local Linux machine, a mac os X
 machine, or it can be a shell account on a Unix server.  If you do
 not own a Unix machine, you can get shell access at sdf.org.  You
 will need an arpa level membership to do the later labs.

 Whatever your setup, I will leave it to you to figure out how to get
 into the shell.  You will either need to ssh or launch a terminal
 application to get the shell going.  I have no way of knowing your
 setup, but I'm sure with a little googling you'll be good to go.

 In this tutorial, I will assume you are logged onto sdf.org.  If you
 are not, some of the exact paths might be different.  More detailed
 treatments of Unix are available at http://sdf.org.  This lab will
 get you just far enough to be able to write a bit of code.

 Now, Let's get started!



 Working with files and the Manual
 =================================
 Ok, so now have a shell open.  Great!  Exciting stuff!  But what
 should you do with it?  Well, that's where we are heading next.  We
 will look at how to explore the file system, and how to use the UNIX
 manual to get help.  This will seem a little overwhelming at first,
 but with a little bit of practice, you'll be a CLI pro in no time!

 1. Logon / Start the shell.

 2. When you logon, you start out in something called your home
    directory.  Just like in windows, Unix divides its file system up
    into directories (folders) and files.  Your home folder is your
    little slice of the server.  In general, this is the only area
    you will be able to write data to, and no one else will be able
    to read or write to your folder (unless you grant them permission
    to do so).  So now, let's see where home is.  Type the following
    command and press enter:
     pwd
    pwd, which stands for "present working directory", simply displays
    the directory you are currently in.

 3. Another similar command is "hostname".  Go ahead and type it and
    press enter.  This shows us the name of the server you are
    currently connected to.

 4. Just like in the rest of the universe, you are not alone on this
    server!  Type "who" and press enter to see who else is logged in.

 5. The screen is getting very cluttered.  Type clear and press enter
    to clear it.

 6. At this stage, let's play around with some generic parts of the
    interface.  Press the up and down arrows.  Note that you can
    scroll through the history of the commands you have executed.  To
    repeat a command on this list, simply press enter.  You can also
    use the arrow keys and backspace to edit your present command.
    Play around with this a little bit now.

 7. Ok, now let's move on to some file operations.  The first step
    is to look at what is in your home directory.  Type "ls" and
    press enter.

 8. If this is a new account, there is not much to look at is it?
    Well it stands to reason, you haven't done anything yet, so you
    don't have any files yet!  Let's try another command.  Type "ls
    -al" and press enter.  Note that there is a space between the
    command name and the arguments.  This is important!  At the top
    of the listing, you should see something that looks like this:

       drwxr-xr-x 2 c1010a20 students 4096 May 27 20:17 .
       drwx--x--x 6 c1010a20 students 4096 May 27 16:19 ..


    It looks like you have something after all!  You have . which is
    your current directory, and .. which is the directory one level
    higher than where you presently are.  So why did these not appear
    when you typed "ls"?  The answer is that these special files
    begin with a "." which hides them from the ls command.  The -a
    causes ls to list all the files (including hidden files).  This
    isn't for security, this is to keep settings and things you don't
    want to worry about all the time hidden from view so they don't
    get in the way.

    The "l" part of the -al means "list details".  This shows a lot
    of information, including some strange alchemy on the left hand
    side.  Here's what that stuff means.  The first letter, which is
    a "d" in this case, indicates that these are directories.  This
    is followed by 9 characters representing the file's permissions.
    In Unix, the permissions are:
       r - read
       w - write
       x - execute
    There are three types of people we set this for.  There is the
    owner of the file, the group of the file, and the rest of the
    world.  These are the groups represented here.  We'll talk more
    about permissions in a later lab.  The good news is that these
    permissions will usually take care of themselves.  They are only
    important if you want to share things with other users.

 9. Speaking of other users, let's take a look at another user's home
    directory.  Namely, let's look at mine.  Type:
      ls ~pngwen
    This is a reference to my home directory.  ~<username> is an
    absolute path name for a user's home directory, and is short hand
    for my real directory (which is /arpa/ns/p/pngwen)

10. Alright, now we can list folders, let's look at some files.  Type
      ls ~pngwen/gopher/humor
    These are files!  You can verify that by typing
      ls -l ~pngwen/gopher/humor

    Note, however, that these files don't have extensions.  This is
    the case of a lot of Unix files.  Unlike windows, Unix doesn't
    depend on file extensions.  We sometimes use them, but we don't
    have to.  So how can we sort out what each file is?  Well we have
    to look inside it.  There is a special command for doing this,
    and that is the file command.  Type the following command:
      file ~pngwen/gopher/humor/geeks
    Now you know that this is a text file.  The file command
    determines this by using a large database of "magic numbers"
    which it matches against your files to tell you what they are.

11. So now you know that that's a text file.  Would you like to look
    inside?  Of course!  (Especially given that this is in the humor
    folder, it might be fun!)  So let's dump the file to your
    terminal.  To do this, type:
      cat ~pngwen/gopher/humor/geeks
    Enjoy!

12. Try a few of my other humor files out.  You probably have noticed
    that some are too large to fit on one terminal screen.  Another
    way to open them is using the "more" command.  Try:
      more ~pngwen/gopher/humor/threes
    Read this file. It reveals something of your future...  Note that
    more stops the screen after each page full.  Press space or enter
    to scroll down.

13. What if you wanted your own copy of threes?  Well, you could do
    that using the "cp" command.  Type
      cp ~pngwen/gopher/humor/threes .
    then list your folder.  You now have your own copy of threes!  So
    what's that "." at the end about?  Well cp has to have a
    destination.  You can specify either a file name or a directory.
    "." means your current directory, so that copies it with the same
    name to your current directory.  You can also specify a new file
    name.  For instance, try:
      cp threes threes2
    Now list your folder. Now you have 2 copies of this wonderfully
    depressing poem!

14. What good is two copies of a text file?  Not much!  Let's get rid
    of the second copy.  We do this with the remove command:
      rm -i threes2
    The -i tells it to question what you are doing.  Unix is
    remarkably obedient, so if told it to do something that was bad,
    it would.  The -i is for your safety, but if you are confident
    you can leave it off.

15. Phew!  There's a lot of commands!  They have lots of little
    switches and complex behaviors.  How do we keep it all straight?
    Well, in the beginning, you have to lean on the manual.  The
    manual has a complete reference of most of the commands that you
    will be using.  Often it tells more than you would ever need, but
    it is a great reference nonetheless.  Let's look at the manpage
    for "ls".  To do this, type:
      man ls
    This will tell you all about the ls command.  Try it for a few
    of the other commands we have used.  Oh, and you may ask
    yourself, is there a manpage for man?  Yup!  Try:
      man man
    How meta is that?

16. Man is great if you need to know all the switches to a command
    when you know what the command is, but how about when you haven't
    a clue about how to do something?  Not to worry, man's got your
    back!  You can do a keyword search of man.  Try:
      man -k directory
    This will show you all the commands/pages about directories.
    There's a bunch of them!  Try this:
      man -k directory | more
    That vertical bar is called the "pipe" symbol.  You make this by
    typing "Shift + \" which is right over the enter key.  What this
    does is take the output of man, and makes it the input of more.
    Thus you can use more to page through the large input.  Try a few
    other topics, like "permissions" or "compile" or "debug".  Enjoy
    poking around!

 Organizing Files
 ================
 Now you can copy files, you can list them, you can use the manual,
 and you are almost ready to move into harder labs.  First, however,
 we need to learn how to organize our files.

 As mentioned before, we organize files into directories.  This
 section will guide you through using directories and how to turn in
 your labs.

 1.  To make a directory, use the mkdir command.  Each of your labs
     will be in its own directory, so as to not make a big mess!  So,
     let's make one for the present lab:
       mkdir lab1

 2. To change directories, use the "cd" command.
       cd lab1
    You can also use cd to change to an absolute path.  For instance,
      cd ~pngwen
    will dump you into my home directory.  Try that now.  ACK!  How
    do you get back to your home?  Easy!  Just type:
      cd
    with no arguments, and you go home.  No slippers, no witches, no
    little dogs.  Just pure computer science.  Ok, let's go back into
    the lab1 folder.
      cd lab1
    Verify you are in this folder.  (Remember the command to do
    that?)

 3. Let's copy all of my humor files into your home folder.  You can
    do this by typing them one by one.  Or you can use a wild card:
      cp ~pngwen/gopher/humor/* .

 4. List your folder, read some of the files.  Pick your favorites.
    Welcome to the world of geek culture!  I pulled these files off
    the real Internet, back before the web took it all over.  Some
    even came off a BBS at 300 baud.  Ah, the good old days!  It was
    all text back then.  It was glorious!


 Further Reading
 ===============
 You've come a long way in a short time!  Of course, we have only
 scratched the surface of what you can do with the UNIX/Linux command
 line. For additional reading, please see:

   http://www.sdf.org
   http://tldp.org/
   http://tldp.org/LDP/intro-linux/html/index.html

 Or just gaze into the raw Internet.  It's largely powered by UNIX.
 You'll gain a lot from looking into it.  See sdf.org to see what the
 Internet really is.

 Copyright (C) 2013 Robert Lowe
 This work is distributed under a Creative Commons
 Attribution-ShareAlike 3.0 Unported License (CC BY-SA 3.0)
 For more information, see http://creativecommons.org