Try Firefox!

Lab 1: Unix Familiarization


The most basic of Unix basics
The shell is a command-line interface between you and Unix. X-Windows is a program that provides those nice GUI windows, menus, mouse click responses, etc. Finally, a Window Manager provides a desktop - it manages the look and feel of those X-Windows GUI elements.

There are many Unixes out there. The Sun workstations in our labs run Opensolaris. When you log on at one of the workstations, you are presented with a (pretty much) blank desktop. This is the Gnome Desktop. You can do a lot by clicking on things like you would with Microsoft Windows, but you really need a shell terminal (command line) to do real work. Click on the Terminal icon at the top of the screen. This pops up a shell terminal (command line) for you to enter commands in.

Many people think of command-line interfaces to computers as primitive. Get over it. Some tasks are much easier to perform with a command line. GUIs are good enough for the average computer user, but none of you are average computer users. Once you've learned to use the shell right, you'll find it's very flexible, powerful and fast. You might not want to go back to that GUI world!

Part 1: Lab setup
If you remember from last class, in the shell you always have an idea of being in a directory. The command pwd (print working directory) tells you the absolute path of the directory you're currently "in". So if pwd gives you
/usr/include
it means that you're "in" the include directory, which is contained in the usr directory, which is contained in the /, which as we talked about last class, is the starting point of the file system.

The directory you'll always start off "in" when you log on is your home directory. If you execute pwd, you should see that you're "in" the directory /home/m12xxxx, which is your home diretory. The way your shell is set up, the current directory is listed in front of the prompt, but instead of listing /home/m12xxxx/ it simply lists the ~/ shortcut we talked about last class. Remember, in your home directory, you have the right to create, delete and move whatever files or directories you want to.

To get started on the lab, you're going to copy a file from my home directory to yours, then for the rest of the lab, we'll be playing around with that file. That brings us to our second command: cp

cp - Copy. cp takes two arguments, the first is the source file, and the second is the destination file. You can either provide an absolute path, or a relative path in either argument. For instance, the following command will copy the file passwd from the /etc directory into my home directory, regardless of what my working directory is:

cp /etc/passwd ~
Note that this would give the same result:
cp /etc/passwd /home/stahl
If, on the other hand, I'm in my home directory, I can take advantage of . to reference my current directory, so this would work:
cp /etc/passwd .
Oh, and this too:
cp ../../etc/passwd .
We want to copy the file unix_practice.tar.gz, located in directory: ~wcbrown/courses/IC221/labs/L01, to your home directory. Hmm, that seems like a lot of typing. Now might be a good time to show you some cool shell shortcuts:

What the heck is a .tar.gz file?
First things first...let's make sure you actually got the file. To do that, we'll use the command ls.

ls: List contents. If you use ls with no arguments, you get the contents of the current directory. If you provide a directory as an argument, you'll get the contents of that directory. Go ahead and type:

ls
You should see the file. If not, either you're not in your home directory, or you didn't copy the file correctly.

Okay, so now we can answer the subject question. A .tar.gz file (also known as a "tarball") is a zipped archive. It's like a .ZIP file in Windows, but a little bit different. To unzip the file, use this command (and remember to use tab completion!):

gunzip unix_practice.tar.gz
Use ls again. What happened? The .gz was removed from the end of the file, leaving us with unix_practice.tar To unpack the tar file, we'll use the tar command.

tar: Create or uncompress a tape archive.

tar -xvf unix_practice.tar
You should see something that looks like this:
> tar -xvf unix_practice.tar
unix_practice/
unix_practice/documents/
unix_practice/documents/mydocument.txt
unix_practice/programs/
unix_practice/programs/myprogram.c
unix_practice/programs/myprogram
unix_practice/doc.txt

What's the -xvf you ask? Ah, great question! We'll use another command to find out.

man: The Unix manual. Contains reference information for all commands on a Unix system. To get information about the tar command, execute:

man tar
Use the spacebar to move down a page, 'b' to move up a page, and 'q' to quit. Want some info about man? Just type man man! Notice that the tar command has tons of different command line options to choose from, and so does just about every other command on Unix. Whenever you have a question about a command, this is where you come.

 
Part 2: Moving around in the file hierarchy
After you untar the file you're left with a directory called unix_practice in your home directory. In order to actually navigate into that directory, we'll need another command:

cd: Change working directory. Provide a path, either relative or absolute, and cd will take you there. If you provide nothing, and just execute cd by itself, it will take you back to your home directory (which is a nice little feature). You'll use the .. reference quite a bit to move around as well since cd .. will move you up one level in the directory tree.

Using cd and ls, move through the unix_practice directory and take a look at what's there. After you're done, return to your home directory.

 
Part 3: Reading and moving files
Move to the unix_practice directory. There's a file in that directory called doc.txt. Let's see what's in it.

cat: Concatenate and print. Prints the contents of a file directly to standard output (the screen) without pausing.

To see the contents of doc.txt we'll execute this:

cat doc.txt

cat is great if you know the file is relatively small, but if it's large and you want to read it, then you'll have a hard time. Try this:

cat /etc/passwd

Yikes! cat doesn't pause when the screen is full to give you a chance to read everything. It actually does this for a very good reason, which we'll talk about later. If you want the shell to pause after a screenful, you need another command.

more: Display a screenful of text. Use man more to figure out how it works!

I really meant for doc.txt to be in the documents folder. We already know how we could copy it there, but instead of copying, let's go ahead and move the file.

mv: Move file. Takes two arguments: the source, and the destination. You can also use it to rename files. Take a look at the man page for mv, then move the doc.txt file into the documents folder. Here's how you can use mv to rename a file:

mv file.txt renamed_file.txt
This will rename file.txt to renamed_file.txt. Literally, you're moving file.txt to renamed_file.txt

 
Part 4: File permissions and ownership
Use cd to get back to your unix_practice folder. If you execute ls in that directory, you'll see that there's another directory in unix_practice called programs. Change into that directory.

Go ahead and examine the contents of myprogram.c if you'd like, using either cat or more. The first thing to notice is that this isn't C++, it's C. We'll talk about C in a couple of classes.

I've already compiled myprogram.c into a program called myprogram, in the same directory. To execute, just type:

myprogram
What happened? Get an error message that looks something like this?
bash: ./myprogram: Permission denied
But can't I do what I want in my home directory? You betcha. The problem is that you have insufficient permission to run myprogram, but not for the reason you think. To learn why, we'll use ls with the -l (that's an 'ell') option. Type this:
ls -l
This will give you a listing of your directory contents with a bunch of other important info. For example:
-rw------- 1 stahl scs 5848 2006-11-21 13:52 myprogram
-rw------- 1 stahl scs  167 2006-11-21 13:52 myprogram.c
Let's take a look at what each one of these columns means:
-rw------- 1 stahl scs 167 2006-11-21 13:52 myprogram.c Number of links to that file.
-rw------- 1 stahl scs 167 2006-11-21 13:52 myprogram.c The first item is the user name, the second is the group name. Yours will say something different. More on this later.
-rw------- 1 stahl scs 167 2006-11-21 13:52 myprogram.c The size of the file in bytes.
-rw------- 1 stahl scs 167 2006-11-21 13:52 myprogram.c The last time the file was modified.
-rw------- 1 stahl scs 167 2006-11-21 13:52 myprogram.c Duh.

The one we're really interested in to figure out why we got our error is this one:
-rw------- 1 stahl scs 167 2006-11-21 13:52 myprogram.c
This portion of the output gives the permissions associated with that file. We read it by breaking it up into three parts as follows:

-rw-------

The red section right at the start indicates the type of file. There are a bunch of options, but we're mainly interested in these: - for a regular file, d for a directory, and l for a link (which we'll talk about later).

The next three characters in green are the file owner's permissions, followed by the group's permissions in brown and finally everyone else's permissions in orange. From left to right, they mean "read, write, and execute" respectively. Although other options are possible, we're mainly concerned with these three. So, for permissions:

rwx
|||
|||
||+--->execute permissions active, - otherwise
|+---->write permissions active, - otherwise
+----->read permissions active, - otherwise

Let's look at our program's permissions again, they were:

-rw------- 1 stahl scs 5848 2006-11-21 13:52 myprogram
Which means that I can read and write the file (which happens to be a program), no one else can do anything with it, but I don't have the necessary file permissions to execute it! I can fix this with the next command:

chmod: Change permission modes. This command can only be executed on files that you own. Remember, Unix was designed to be a multi-user system, so I shouldn't be able to just go into another user's directory willy-nilly and give myself permissions for anything. Speaking of which, root can execute chmod on anything you own.

Here's how we'll use chmod most often:

chmod MODE FILE
FILE is just the file we want to change the permissions of, and MODE is where we specify exactly what permissions we want the file to have. Use u, g, and o to modify the user, group and other permissions respectively. Specify which permission you want to add with r, w and x. To add permissions for the user, we'd do something like u+r, which means "for the user (file owner), add read permissions". Similarly, we can do u+rw which means, "for the user, add read and write permissions. If we substitue a - for the +, we can remove permissions.

"We can modify more than one permission group at a time like this, for example: go+r. I use that one all the time. It means "for the group and all others, add read permissions." Why would I want to give everyone read access to something I own you ask? Well, you're reading such a reason now! That's how we set the permissions for web pages!

Now you have enough to correct the permissions of the file myprogram. Once you've successfully changed the permissions, go ahead and execute the file. Your screen will display some instructions for you!

 
Part 5: Clean-up
Use cd to get back to your home directory. Let's get rid of that unix_practice directory since you won't be needing it anymore. Try this:
rm unix_practice
What happened? Use man rm to figure out which option you need to give rm in order for it to delete the directory for you.

Part 6: Creating a text file
In your home directory, make a sub-dirctory called lab01 and cd to that directory, i.e. do
mkdir lab01
ls
cd lab01
You're going to create a text file called ans.txt which, eventually, will be your submission for this lab. Text files are created and modified with text editors. The text editor we'll be using is called "emacs". To launch emacs do
emacs &
Emacs looks a bit intimidating, but you'll learn to love it ... trust me! To create a new text file named "ans.txt", click on the "open icon"

the type ans.txt and hit Enter. A blank buffer comes up. Type your name and a few newlines, and click on the save icon. If you go back to your terminal window and ls, you should see that ans.txt has been created. Use cat, and you'll see the contents. Exit emacs (file / Exit Emacs), then relaunch it. When you go to file / Open file, you should see ans.txt listed in the box. Double-click on it to open it up.

Submission - Only when you have finished the above!
The answers to the questions below should appear in the textfile lab01/ans.txt. This file must have your name and alpha in the first line!

Copy and paste this text into ans.txt and add the answers into the file. Do not forget your name and alpha!

1. For each of the following utilites, give a one-line description of
   what they do:

   pwd
   cp
   ls
   tar
   man
   cd
   cat
   more
   mv
   chmod

2. Read the man page for chmod, then answer the following questions: 

   * what does it mean to have x permissions on a directory?

            		

   * What about r permissions?

            		

    You can do some experimentation to find out. Try looking at the
    IC221 course directory at this path:
    ~wcbrown/public_html/courses/IC221/ . You'll find some folders
    that you have x permissions for, but not r permissions. If you say
    "x lets you execute a directory and r lets you read it" you'll get
    a zero.


      	    

3. Copy the file ~wcbrown/courses/IC221/labs/L01/riddle.tar.gz. Unpack
   this and follow the clues to find "the answer". Write the answer in
   ans.txt. Hint: You'll need to use the ls/cp/cat/tar/mv/chmod commands
   described above.


	

You will submit this lab, like all your labs, via the class submit script. Change your current directory to the one containing lab01: not to lab01 itself, but to its parent. Then do:

/courses/wcbrown/etc/ic221/submit lab01 ← If I (Dr. Brown) get a chance to updating submit scripts, this might change!
... for Dr. Brown's students,
/courses/stahl/etc/ic221/submit
... for Dr. Stahl's students,
/courses/crabbe/etc/ic221/submit
... for Dr. Crabbe's students. For thorough instructions on the submit script see Submission.pdf.


Dave Stahl
Last modified: Mon Jan 9 11:08:19 EST 2008