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 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.
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').
Finally, you'll see a window that looks something like this:
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:
Neat, right? Turns out, these computers can do more than displaying the current calendar - crazy, right?
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.
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!
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
lab0
star [126] ~ #
Hey, there's our new directory! Awesome.
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 (sincethe 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.
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:
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 |
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
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:
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
lab0
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
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
shakespeare.txt
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
new_file
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
new_file
star [278] ~/lab0 # mv new_file ..
star [279] ~/lab0 # ls
star [279] ~/lab0 # cd
star [278] ~ # ls
new_file
We have just moved new_file back into our home directory,
which was a parent directory of the lab0 directory.
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
best_name_ever
We have just successfully renamed new_file to be the filename: “best_name_ever.”
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.
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 |
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
ls
cd
helloooo
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:
star [145] ~ # firefox &
star [146] ~ # ls
lab0
star [147] ~ # echo "Hooray, you're listening to me!"
Hooray, you're listening to me!
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!
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:
To help us keep track of what we're doing, I'm going to explicitly state the goals for this section:
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!
Remember to add the '&' when opening Emacs:
star [148] ~ # emacs &
A window something like this should open up:
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)
Now, let's create our new file - to do that, you can do any of the following 2 options:
Once you've done that, a prompt will appear on the bottom area (this is called the mini-buffer), asking for the name of the file you wish to create. Type in 'my_epiphany' as the file name, and hit enter. (See Figure 7 to see what the mini-buffer looks like).
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 semester is going to be a pretty good semester."
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.
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):
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.
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
3
>>> 7 * 8 - 9
47
>>> (1 + 2) * (3 - 4)
-3
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:
The Python intepreter 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 termainal 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:
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
'Eric'
>>> greet()
Greetings Eric, how are you today?
   -Python
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-C, and run python3 -i greet.py. Then, call the greet function again at the Pythohn 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!).
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:
Some hotkeys involve the Meta key, such as this hotkey that
opens up a Scheme interpreter:
     M-s
On the keyboards in the Soda labs, the Meta key is the key with the "diamond", usually next to the spacebar.
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 - so, 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
So, if you're using Emacs with a keyboard that doesn't have the Meta key, you can always use the Esc key.
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-x d | Open a directory |
C-_ | Undo. (The '_' is an underscore, so you'll probably have to use Shift also) |
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 |
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 |