CS 61A: Lab 0

Introduction to UNIX/Emacs

0. Introduction

Hello! The first thing you might have noticed about these computers is that they don't have Windows or MacOS installed. And you're right - they're running UNIX. But fear not! We'll get you familiar with this new system in no time - by the end of the semester, this stuff will feel like old hat.

By now, you should already have logged in, registered (make sure you use your berkeley.edu email address) and changed your password by running ssh update. If you made a typo (e.g. misspelled your name), don't worry; you can restart the registration program, first by completing the registration process, and then typing re-register at the prompt and hitting enter.

Note: Please do not forget your login information - especially your log-in name (e.g. cs61a-ba). Memorize your log-in name, e-mail your log-in name to yourself, etc. If you forget, you'll need to get another log-in form from your TA and start again. If you forget your password, you can either e-mail INST at inst@eecs.berkeley.edu, or go to 333 Soda.

0.1. Logging in from Home

If you don't have access to a school computer for this lab, you can still try it out: refer to the tutorials at the bottom of the 61A front page to setup the tools needed to log in remotely (they're under the section entitled 'Connecting to the Computing Labs from Home').

1. Meet the Terminal

Finally, you'll see a window that looks something like this:


Figure 2: The terminal window.

This window is called the terminal - this is where you'll be talking to the computer. You talk to the computer by entering in commands. Here's a neat command - need to look up a date for this month? Try the cal command by typing cal into the terminal, then hitting enter:

Calender output

Figure 3: Your first command, cal!

Neat, right? Turns out, these computers can do more than displaying the current calendar - crazy, right?

2. Getting used to the Filesystem

The most important thing to learn first is how to use the filesystem. Unlike in Windows/MacOS, there aren't folders you can click/drag/double-click. There's not even a 'My Computer' icon in sight!

That's okay - we're going to learn how to do everything via the command line (the command line is the terminal). Everything you did on a visual-based filesystem (i.e. like those found on a Windows/MacOS system), you can also do via the terminal.

2.1. Directories

First, I'll introduce you to our good friend, ls.

ls is a command that lists all the files in the current directory. Oh yes, and what's a directory? A directory is just like a folder, e.g. the "My Documents" folder. When you log in, you are automatically started off in the home directory, so if we run the ls command right now, it'll display all the files in our home directory:

Try the ls command now!

star [121] ~ # ls
star [122] ~ #

Hm - nothing really happened. That's because there's nothing in our home directory - we just made our account after all! Let's make some stuff!

2.1.1. Making Directories

This leads to another good command: the mkdir command.

mkdir is a command that makes a new directory (hey now, the command names make sense!). Unlike cal and ls, we don't just type mkdir and press enter - we need to specify the name of the folder we want to create! Since we're well-organized people, let's create a new directory for this lab, and call it lab0:

star [123] ~ # mkdir lab0
star [124] ~ #

When we supply extra 'stuff' to a command (like a folder name, for instance), we say that we're calling the mkdir command with parameter(s). Not all commands take arguments (recall cal). Some commands even have optional parameters (ls, for instance, has a bunch of different optional parameters).

Okay, now that we've made our directory, let's make sure it's actually there - use the ls command to make sure that the lab0 directory exists.

star [125] ~ # ls
star [126] ~ #

Hey, there's our new directory! Awesome.

2.1.2. Changing Directories

To get 'inside' the directory, we have a handy command called cd.

cd (short for change-directory) is a command that, when given a directory name as a parameter, takes you into that directory. Enter the lab0 directory by typing: cd lab0

star [126] ~ # cd lab0
star [126] ~/lab0 #

Note that the ~ turned into a ~/lab0. This tells you that you're currently in the lab0 directory - the ~ stands for the home directory.

So we're inside the lab0 directory, but there's not much here. You can ls to make sure that it's empty. Let's say we want to go back to the home directory: there are two ways to go back from here.

One way is to enter in the following: cd ..

star [126] ~/lab0 # cd ..
star [127] ~ #

The .. is shorthand in UNIX for "the parent directory". The home directory is the parent directory of the lab0 directory (since the lab0 directory lives in the home directory).

Alternately, you can type in just: cd

star [127] ~/lab0 # cd
star [128] ~ #

Running the cd command with no parameters is equivalent to returning to the home directory. This is handy when you're many directories deep, and you don't want to keep repeating cd .. to get back home.

2.1.3. Removing Directories

We've created them - now, we can destroy them! Er, remove them, rather. Often, you'll find yourself wanting to delete directories (say, to organize things). To delete a directory, we use rm -r command, short for remove recursively. This tells Unix to recursively go through a folder, deleting the folder and all of its contents (including empty folders).

Like mkdir, rm -r takes a directory name as a parameter. Try the following steps:

  1. Create a directory called my_folder
  2. Run ls to see that it's really there
  3. Remove the directory using rm -r my_folder
  4. Run ls again to see that it's not there anymore.

Summary: We've learned about the following commands:

Command Description
cal Displays the current month
ls Lists the current directory contents
mkdir Creates a new directory with a specified name
cd Moves into/out of directories
rm -r Removes the given directory

2.2. Files

We've done a lot of things so far, but only with directories - we probably want to be able to actually have stuff in our directories. So, let's make some files, and learn the commands to manipulate them.

Our first step is to create a file. Notice the distinction between files and directories. In UNIX, we tend to treat files and directories separately - for instance, it makes sense to cd into a directory, but it doesn't quite make sense to cd into a text file!

Let's create a simple file that has the sentence: 'This semester will be awesome!'

The command we'll use is called echo.
echo is a command that simply displays anything you type after the word 'echo':

star [136] ~ # echo hello
star [137] ~ # echo Stop repeating me!
Stop repeating me!
star [138] ~ # echo No, you stop!
No, you stop!

Some terminology - the words that the computer displays after we hit the enter button is the output of the echo command. It's sort of like this picture:

Echo visual

Figure 4: Visualization of input/output of the echo command

2.2.1. Making a file - redirecting output

UNIX has a very nice way to redirect output - with the > symbol. Let's say we want to redirect the output of echo into a new file called my_file . We can do this by doing:

star [139] ~ # echo This semester will be awesome! > my_file
star [140] ~ # ls
lab0 my_file

That was easy! We created a new file - to get a glimpse into what's inside, we can use another command, called cat.
cat is a command that displays the contents of a given file:

star [141] ~ # cat my_file
This semester will be awesome!

To remove files, we use the rm command - this time without the -r option. Use the rm command to delete the my_file file:

star [142] ~ # ls
lab0 my_file
star [143] ~ # rm my_file
star [144] ~ # ls

Warning: Use rm with utmost care! Unlike in Windows/MacOS, there is no friendly 'Recycle Bin' or 'Trash' where you can restore a deleted file. In UNIX (at least on these systems), when you rm a file, it's gone. Vanished. Caput. There's no 'undo-ing' a rm - so, think twice (and thrice!) before using the rm command!

With directories, we were able to make and remove them. However, for files, we can do even more!

Let’s go ahead and make a new file, because we have removed the one we made in the previous section.

star [139] ~ # echo This semester will be awesome! > my_file
star [140] ~ # ls
lab0 my_file

2.2.2. Copying a file

Let’s say we wanted to make a copy of this file. Well we can use the cp command.
cp takes two parameters, the first is the name of the file you want to make a copy of, and the second is the name of the new file you want to copy the first file into. For example, if we wanted to copy my_file into a new and different file called new_file, then we could do so as follows:

star [272] ~ # cp my_file new_file
star [273] ~ # ls
lab0 my_file new_file

If we were then to look at each file separately using the cat command, we can see that new_file is simply a copy of my_file. Exactly what we wanted.

star [275] ~ # cat new_file
This semester will be awesome!

Now a lot of times we will want you to copy a file from our cs61a account into your own. We can use the cp command to do so by specifying the filepath, which will almost always be given to you. (For example, something like “~cs61a/lib/shakespeare.txt” is the filepath for the text file from our 61a account which contains a Shakespearean sonnet).

star [276] ~ # cp ~cs61a/lib/shakespeare.txt shakespeare.txt
star [277] ~ # ls
lab0 my_file new_file shakespeare.txt

But here's a handy tip: if we put a period '.' as the second argument to cp, we get the same effect:

star [278] ~ # cp ~cs61a/lib/shakespeare.txt .
star [279] ~ # ls
lab0 my_file new_file shakespeare.txt

The '.' is a UNIX shorthand for 'current directory'. So, cp ~cs61a/lib/shakespeare.txt . means:
     Create a new copy of ~cs61a/lib/shakespeare.txt, and put it in the current directory.

Similarly, if we wanted to, we could copy shakespeare.txt to our lab0 directory by doing:

star [280] ~ # cp ~cs61a/lib/shakespeare.txt lab0
star [281] ~ # ls lab0

2.2.3. Moving a File

We can also move a file to a different directory by using the mv command.
mv takes in two parameters as well: the first is the filename that we want to move, and the second is the name of the directory that we want to move that file into.

star [275] ~ # mv new_file lab0
star [275] ~ # ls
lab0 my_file
star [276] ~ # cd lab0
star [277] ~/lab0 # ls

We just moved new_file into the lab0 directory. As you can see, the lab0 directory is in the home directory, which is where the new_file originally was. The name of the directory we are moving the file into needs to be in the current directory, or else the computer will not know what directory you are referring to, and will instead rename the file (more on that later).

However, what if we wanted to move the file back into the home directory; the home directory is not inside of lab0, so there is no way to reach it right? No! Just like we could change into a parent directory by calling cd with “..” we can also move a file into the parent directory by calling mv with a filename and “..” as follows:

star [276] ~/lab0 # ls
star [278] ~/lab0 # mv new_file ..
star [279] ~/lab0 # ls
star [279] ~/lab0 # cd
star [278] ~ # ls

We have just moved new_file back into our home directory, which was a parent directory of the lab0 directory.

2.2.4. Renaming a File

Lastly, we can rename a file. To rename a file, we can actually also use the mv command. In this case, the mv command still takes in two parameters: the first being the name of the file we want to rename; however, the second is the new name for the file.

star [277] ~/lab0 # mv new_file best_name_ever
star [278] ~/lab0 # ls

We have just successfully renamed new_file to be the filename: “best_name_ever.”

2.3. The Most Useful Unix Command: man

We've shown you a lot of commands and it might become a little hard to remember what everything does. If you ever forget (and can't be bothered to come back to this page), there is one useful that'll help: man (short for manual).

Just run man with some other Unix command to find out what it does (e.g. man cp). man will bring up a page inside of the terminal. The NAME field will give a brief description of what the command does, and the DESCRIPTION will have a host of extra options you can run the command with.

You can navigate forward through the man page with the Enter/ Return key and you can quit with q key.

2.4. Summary of Unix Commands

Command Description
cal Displays the current month
ls Lists the current directory contents
mkdir Creates a new directory with a specified name
cd Moves into/out of directories
rm -r Removes the given directory
echo Outputs user input.
cat Displays the contents of a specified file.
rm Removes the specified file.
mv Move a file to a new destination (can also be used to rename)
cp Copy a file to a new destination
man Brings up the manual page for a given command

3. Running programs: Firefox

These machines come pre-installed with a variety of programs. Two programs that you'll be frequently using over the semester are Firefox and Emacs.

Firefox is a free web browser (like Internet Explorer, Safari, Google Chrome, etc.). To open it, you can simply enter the program name at the terminal and hit enter:

star [145] ~ # firefox

After a few moments, Firefox will open up in its own window. Don't worry if it takes awhile - during the first week of school, the servers are usually very busy, so programs like Firefox may be slow at first.

One unfortunate side-effect of opening up Firefox like this is that our terminal is now unresponsive to new commands:

star [145] ~ # firefox
you're not working anymore :(

The terminal will only be responsive once you exit Firefox. To avoid this situation, if you add an ampersand '&' after firefox, the terminal will still be responsive (adding a & runs the program in the background):

star [145] ~ # firefox &
star [146] ~ # ls
star [147] ~ # echo "Hooray, you're listening to me! "
Hooray, you're listening to me!

4. A Recap

Whew! We've covered a lot so far, so let's recap what we've done so far.

This is fantastic - definitely all of the commands you'll need for the semester. However, we have yet to really create/edit/save text files. And no, Microsoft Word is not installed on these machines. But we have something better!

5. Our Text Editor: Emacs

Emacs is a very popular free text editor, with quite a bit of history behind it (it was created in 1976!). This is the text editor we'll primarily be using this semester. However, it's definitely not required - some other text editors include:

However, we'll only be talking about Emacs here. Now, Emacs may seem very intimidating and difficult at first, but don't worry, we'll get you situated in no time.

To help us keep track of what we're doing, I'm going to explicitly state the goals for this section:

  1. Using Emacs, create a new text file called 'my_epiphany' in the home directory, and type the sentence:
         "This is going to be a pretty good semester."
  2. Then, using Emacs, re-open 'my_epiphany', and edit it to instead say:
         "This semester is going to be a fantastic semester!"

So, let's start with opening up Emacs. It's important where you open Emacs, because the directory in which you open Emacs determines the directory that Emacs 'starts off' in. For instance, if I open up Emacs in the home directory, and I saved a file called 'my_file.txt', then my_file.txt will appear in the home directory. But more on that later!

Let's try opening emacs with the following:

star [145] ~ # emacs

One unfortunate side-effect of opening up emacs like this is that our terminal is now unresponsive to new commands:

star [145] ~ # emacs
you're not working anymore :(

The terminal will only be responsive once you exit emacs. To avoid this situation, if you add an ampersand '&' after emacs, the terminal will still be responsive:

star [145] ~ # emacs &
star [146] ~ # ls
star [147] ~ # echo "Hooray, you're listening to me"
Hooray, you're listening to me

A window something like this should open up:

Emacs Splash

Figure 5: The Emacs splash page. Note: you might not have the icons below the menu bar when you run Emacs.

This is sort of the 'splash page' for Emacs - later, if you're interested, you can check out the Emacs Tutorial, but let's not do that right now. (It is a valuable resource for learning to use Emacs, but it could take you years to complete! :p)

5.1. Creating a file in Emacs

Now, let's create our new file - to do that, you can do any of the following 2 options:

Now, the Emacs window should turn into a blank page - this is the newly created my_epiphany file. Go ahead and type the sentence: "This is going to be a pretty good semester."

Emacs saving

Figure 8: Our new file.

Now that we've added our sentence, let's save the file (either doing File -> Save or doing the hotkey C-x C-s). You'll know it's saved when the two stars after the file-name go away (see Figure 8 to see what I mean).

Now, exit Emacs (by doing File -> Quit , or C-x C-c). Congratulations! You've just created your first file in Emacs. We can confirm that it does in fact exist by cat-ing the file:

star [149] ~ # ls
lab_0 my_epiphany
star [150] ~ # cat my_epiphany
This is going to be a pretty good semester.

5.2. Editing a file in Emacs

But wait! We want to edit that file - we want to instead say:
“This semester is going to be a fantastic semester!”

So, let's edit the file to say this instead. One way we could do this is open up Emacs using emacs &, and use the File -> Open (hotkey: C-x C-f) to open up the file (typing in my_epiphany in the mini-buffer):

Emacs open

Figure 9: Opening a file in Emacs

Or, we can provide the name of the file as a parameter while opening up Emacs:

star [151] ~ # emacs my_epiphany &

This does two things at once:

Now, modify the file to instead say "This semester is going to be a fantastic semester!", save it, and exit Emacs. cat the file to make sure that it worked.

star [152] ~ # cat my_epiphany
This semester is going to be a fantastic semester!

Helpful Tip: If the mini-buffer ever has a prompt that you don't understand (say, you accidentally hit a command), and you're not sure what to do, click the mini-buffer and do the hotkey C-g. This will cancel the mini-buffer prompt, and also cancel the command that was expecting the prompt.

6. The Python Interpreter

In Computer Science parlance, an interpreter is a program that lets you interactively 'talk' to a programming language. A Python Interpreter is thus a program that lets you interactively talk to Python. The best way to see what I mean is to try it out yourself!

Just like Firefox and Emacs, we can enter the Python interpreter from the terminal. To do this, simply type python at the terminal:

star [153] ~ # python
Python 3.2.1 (v3.2.1:ac1f7e5c0510, Jul 9 2011, 01:03:53)
[GCC 4.4.6] on sunos5
Type "help", "copyright", "credits" or "license" for more information.

Now, you're talking to Python! The ">>>" signifies that the interpreter is waiting for user input. So, when you type something in and hit enter, Python will try to evaluate it. It's similar in spirit to the UNIX terminal prompt, but instead of talking to UNIX, you're talking to Python. Try typing in a few simple arithmetic expressions.

>>> 1 + 2
>>> 7 * 8 - 9
>>> (1 + 2) * (3 - 4)

Notice that you're actively talking to Python - hence, why it's an interactive program.

We'll play around in Python more a little later in lab, so let's get back to more Emacs fun - you can exit the Python interpreter by doing either of the following:

7. Emacs, Python, and the Terminal

The Python interpreter is definitely neat, and allows you to try and test out little bits of code relatively easily and quickly. But as our code gets more complex, typing everything into the interpreter again and again gets tedious. Emacs comes in handy here, as it allows our code some permanence.

Emacs does allow us to edit a file and then immediately run it in a built-in interpreter, but that can get a little messy on our instructional machines. To save Emacs (and ourselves) some trouble, let's couple it with the powerful Unix terminal. You'll hopefully be spending a lot of time in the terminal anyways, so using a text editor and a terminal simultaneously becomes an obvious combination.

Let's start by creating a Python source file, so navigate to the lab0 directory, either

Now, create a new file called greet.py - the .py file extension is important, because:

Let's write a very simple, sort-of-silly program that greets you by name. Don't worry if you don't understand the program (we'll learn what each of these pieces mean in more depth over the next few weeks):

print("Hello world!")
my_name = "Eric"

def greet():
    print("Greetings ", my_name, ", how are you today?")
    print("  - Python")

Now, your Emacs screen should look something like this:


Figure 11: Our simple greet.py program.

Let's go back to the terminal and run our little program.

star [154] ~/lab0 # python3 -i greet.py

You'll know you did it right if "Hello World" pops up and you're thrown into a Python interpreter: “>>>

When you run Python with the -i flag, Python acts as if you had typed every line in greet.py into the interpreter, line by line. That's why "Hello world!" appears, since the Python interpreter is evaluating the first line in greet.py: print("Hello world!")

greet.py also defines two things: a my_name variable (bound to the value "Eric"), and a function greet that, when called, greets a person (signed by Python, nonetheless!). To make sure it works, do the following in the Python interpreter:

If you did it right, your terminal should look something like this:

star [155] ~/lab0 # python3 -i greet.py
Hello World!

>>> my_name
>>> greet()
Greetings Eric, how are you today?

Great, it works! However, right now it's currently greeting me - we probably want it to greet you! Go edit the greet.py file, and change the value of the my_name variable to instead be your name.

For example, if your name is Stephanie, greet.py should look like:

print("Hello World!")
my_name = "Stephanie"

def greet():
    print("Greetings ", my_name, ", how are you today?")
    print("  - Python")

Save greet.py in Emacs, then go back to the terminal, kill the current interpreter session with Ctrl-D, and run python3 -i greet.py. Then, call the greet function again at the Python prompt ">>>" to make sure the name was changed.

Congrats! You've completed your first typical work-cycle: edit a file, run it, edit it again, run it again, etc. This will start feeling natural as the course progresses (and as you get further in your CS career!).

Appendix A: Hotkeys in Emacs

If you watch a pro Emacs user work in Emacs, you'll notice that he/she never uses the mouse to do anything - everything he/she does is via hotkeys.

A hotkey is just a combination/sequence of keys that, when performed, does some action. For instance, you're all probably familiar with the copy and paste hotkeys: Ctrl-c, and Ctrl-v respectively.

Emacs has a wide variety of hotkeys - pretty much any action can be done with some sort of hotkey. For instance, the hotkey C-x C-s will save the current buffer/file.

But let's see how to actually perform these hotkeys:

C-x C-s is two actions, one after another:

A.1. The Meta key

Some hotkeys involve the Meta key, such as this hotkey that opens up a Scheme interpreter:

On the keyboards in the Soda labs, the Meta key is the key with the "diamond", usually next to the spacebar.

Meta key

Figure 15: The Meta key is the key next to the space bar

So, when you're doing hotkeys involving the Meta key, use this "diamond" key just like any other key.

However, most keyboards don't have the Meta key! In these cases, the Alt key typically replaces the Meta key.

You can also use Esc as a "sort of" Meta key. The difference is, you first press the Esc key, then you hit the next key: for instance, to do M-s, you don't hold Esc while pressing s - you can just do:
     - First press the Esc key
     - Then press the s key

A.2. Some Useful Hotkeys

Hotkey Description of what it does
C-x C-s Save your file.
C-x C-f Open a file. If the filename you provide in the minibuffer doesn't exist, then Emacs will create a new file for you.
C-/ Undo.
C-w Cut the highlighted region of text.
C-y Paste text.
M-w Copy the highlighted region of text.
C-g Cancel a command (useful if you accidentally did a command, and the mini-buffer is prompting you for something).
C-x C-c Exit Emacs

Appendix B: Unix Commands Summary (incomplete list)

Command Description
cal Displays the current month
ls Lists the current directory contents
mkdir Creates a new directory with a specified name
cd Moves into/out of directories
rm -r Removes the given directory
echo Outputs user input.
cat Displays the contents of a specified file.
rm Removes the specified file.
mv Move a file to a new destination (can also be used to rename)
cp Copy a file to a new destination
man Brings up the manual page for a given command