While you will be able to do subsequent labs on your VM, you must complete this lab in the Computer Science Instructional Lab (CSIL).


  1. Become familiar with the Linux environment
  2. Learn basic terminal commands and how to work with a text editor
  3. Learn to use git
  4. Learn to run a Python program from the command-line


Linux is an operating system much like OS X or Windows. It has windows, programs, web browsers etc. Files are stored in directories which are stored in other directories. You can access all of these features by using your mouse and double clicking on icons. As we perform more and more complex tasks we find that interacting with the computer graphically using the mouse is ineffective. Linux also allows us to interact with the computer entirely through text using a program called the terminal. (Macs provide a terminal application very similar to the Linux one, and there are ways to make it happen on Windows too. But, Linux provides the lowest barrier to entry.) In this lab you will learn how to use the terminal to perform some basic operations in Linux. You will need these skills for the rest of the course.


On your personal computer, you probably navigate your hard drive by double clicking on icons. While convenient for simple tasks, this approach is limited. For example, imagine you wish to delete all of the music files over 5 MB that you haven’t listened to in over a year. This task is very hard to do with the standard double-click interface but is relatively simple using the terminal.

Click the Application button (at the top left) and type “terminal” in the input box. Click the “terminal” icon to open the terminal window. Alternatively use the keyboard shortcut Ctrl-Alt-T.

A terminal window will open and you will see a string of the form:


where username has been replaced by your CNetID and computer is the name of the machine you happen to be using. This string is called the prompt. When you start typing, the characters you type will appear to the right of the $.

The procedure for completing this lab is as follows. For each section, read through the explanatory text and the examples. Then, try these ideas by doing the exercises listed at the bottom of the section.

Show Files

The terminal will start in your home directory, /home/username/, which is a special directory assigned to your user account. No matter which computer you will use in the CSIL it will automatically connect to your home directory and all files that you created or changed in a previous session will be available to you.

Two very useful commands are pwd and ls:

pwd Prints your current Working Directory - tells you where you are in your directory tree.
ls Lists all the files in the current directory.

The following is an example using these two commands in a terminal window:

username@computer:~$ pwd
username@computer:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

Try these commands yourself and verify that everything looks similar.

Notice that the directory path and list of files that you see if you open your home folder graphically are identical to those provided by pwd and ls, respectively. The only differences are the way you obtained the information and how it is displayed.

Change Directory

cd path-name     change to the directory path-name
cd ..            move up/back one directory
cd               move to your home directory

How can we move around in the file system? If we were using a graphical system we would double click on folders and occasionally click the “back” arrow. In order to change to a different directory in the terminal, we use cd (change directory) followed by the name of the directory that we want to move to. For example if we want to change to the Desktop directory, we type the following in the terminal:

cd Desktop

Here is an example of changing to the desktop directory in the terminal. We use pwd and ls to verify where we are and where we can go:

username@computer:~$ pwd
username@computer:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
username@computer:~$ cd Desktop
username@computer:~/Desktop$ pwd
username@computer:~/Desktop$ ls


Notice that after we cd into the Desktop the command pwd now prints out:


rather than:


In the beginning, there are no files in the Desktop directory, which is why the output of ls in this directory is empty.

We can move up one step in the directory tree (i.e. from /home/username/Desktop to /home/username or from /home/username to /home) by typing cd .. Here “up” is represented by “..” This command will move us up one level back to our home directory:

username@computer:~/Desktop$ pwd
username@computer:~/Desktop$ cd ..
username@computer:~$ pwd

Notice that the working directory is also shown in the prompt string.

The tilde (~) directory is the same as your home directory: that is, ~ is shorthand for /home/username.

When you use cd, you will specify what is called a “relative” path, that is, you are telling the computer to take you to a directory where the location of the directory is described relative to the current directory. The only reason that the computer knows that we can cd to Desktop is because Desktop is a folder within the /home/username directory. But, if we use a / at the beginning of our path, we can change to any directory we want, because we have begun at the “root” of our computer:

username@computer:~$ pwd
username@computer:~$ cd /home/username/Desktop
username@computer:~/Desktop$ pwd
username@computer:~/Desktop$ cd /home/username
username@computer:~$ pwd

These commands achieve the same thing as the ones above it: we cd into Desktop, a folder within our home directory, and then back to our home directory. Paths that start with a / are known as absolute paths.

Running cd without an argument will take you back to your home directory without regard to where are you are in the file system. For example:

username@computer:~/Desktop$ cd
username@computer:~$ pwd

Setting up your CS121 directory

Before we practice these commands we need a set of files to practice on. Unfortunately your home directories are mostly empty. In this section we’re going to download a set of files for you to work with. We will do this using Git, a version control system and code-sharing tool. Git will be described in more depth later in the lab. For now please execute the following steps:

  • Make sure that your departmental Git account is correctly set up. Go to and try logging in with your CNetID and password. When you type in your username, use only your CNetID username (without “”). If you are unable to log in, please speak with a TA.

  • Make sure that you are in your home directory /home/username using the pwd command. If you are not in that directory then use cd to change to it.

  • Run the following command in the terminal if you are on a CSIL computer:

    cs-setup-script cs121-aut-15


Copy-Paste: In Windows (Mac) you usually copy-paste with Ctrl-C (Command-C) and Ctrl-V (Command-V). These short-cuts are available in graphical programs in Linux but not in the Terminal. Instead you can copy text just by selecting it with your mouse. Select the line that starts with cs-setup-script... above to copy it. You can paste by middle clicking where you would like it to go. Middle click in the terminal. You should also be able to use Ctrl-Shift-C and Ctrl-Shift-V, but you may find that this method does not work.

  • The setup script will ask you first to enter your CNetID:

    Enter your CNetID [username]:

    Where your CNetID will appear in place of username. You can either type in your CNetID, or hit enter to accept the username in brackets. Next, you will be asked for your CNetID password:

    Enter your CNetID password:

    Your password will be handled in a secure manner by the script, which simply needs it to access your information on the CS department’s Git server.

    Note: When you type in your password in a web browser, you may be accustomed to seeing an asterisk character appear for each character you type. This is not the case when typing passwords into the terminal: the password is not “echoed” back in any way (not even with asterisks) so don’t be alarmed if it looks as if you’re password isn’t being typed in.

  • Next, the script will print this:

    You are a member of the following repositories.
    Please select the one you want to use:
    [1] username
    [X] Exit
    Choose one:

    Just choose 1. Later in the quarter, you will become a member of other repositories as you work in groups with other students.

  • If successful, the script will print out the following:

    Setting up your Git repository...
    Your git repository has been created in /home/student/cs121-aut-15-student
    Setting up chisubmit...
    chisubmit has been set up. You can use chisubmit commands inside /home/student/cs121-aut-15-student

    Where, once again, your CNetID will appear in place of username.

    This output indicates that your Git repository has been correctly set up. The script also configured that same directory so you’ll be able to use a tool called chisubmit that you will use to submit your programming assignments.

  • After running the setup script, list the files in your home directory. You should see a new directory cs121-aut-15-username. This directory will contain all of your work for this class. It contains a subdirectory, lab1, that has some files for us to play with. You will learn how to manipulate these files in the next section.

    Note that you will also see subdirectories named pa0, pa1 and lib. You can ignore these for now. Use pwd, ls, and cd to navigate yourself into the lab1 subdirectory.

Using an editor

List the files in the lab1 directory. You should see the following:  test.txt

How do we view and edit the contents of these files? There are many high quality text editors for Linux. Today we will use Sublime Text, which is good for writing code.

Open the file test.txt with sublime-text by typing the following into the terminal:

sublime-text test.txt

You should see a simple text file with the following text:

Lab 1 Test file

Author: Firstname Lastname

If the file is blank, quit sublime-text and ensure that the file test.txt exists in your local directory (use ls to list the files in your local directory). If it does not then use cd to navigate to the lab1 subdirectory inside the cs121-aut-15-username directory.

For now, we will use sublime-text in a very basic way. You can navigate to a particular place in a file using the arrow keys and then type standard characters and use the delete key as you would in a regular text editor. You can save your changes using the save option in the file menu or better use the keyboard shortcut Crtl-s.

Make sure that you are comfortable this level of usage by

  1. Adding your name after Author: in this file
  2. Saving
  3. Closing and reopening the file in sublime-text and ensuring that your name is still there.

Copy (cp) Move (mv) Remove (rm) and Make Directory (mkdir)

cp source destination -- copy the source file to the new destination
mv source destination -- move the source file to the new destination
rm file               -- remove or delete a file
mkdir directoryname   -- Make a new empty directory

Sometimes it is useful to copy a file. To copy a file use the command:


where SOURCE is the file you want to copy and DESTINATION is the name you want to copy it to. An example of copying the file test.txt to copy.txt is below:

username@computer:~$ cp test.txt copy.txt

Before starting these exercises please close sublime-text. Your terminal is currently busy running the sublime-text program and will not be responsive until this program is closed.


Try to accomplish the following tasks to practice and check your understanding of these terminal commands.

  1. Execute the above copy command and use ls to ensure that both files exist.

Move (mv) has exactly the same syntax but doesn’t keep the original file:

  1. Move the file copy.txt to the name copy2.txt. Use ls to verify that this command worked.

You can make a new directory with mkdir directoryname:

  1. Make a new directory named backups using the mkdir command.

Locations/paths can include directories:

  1. Copy the file copy2.txt to the location backups/copy.txt.

You can list the files in a specific directory with ls directoryname:

  1. Verify that step (4) was successful by listing the files in the backups directory.

You can remove a file with the command rm filename:

  1. Now that we have a copy of test.txt in the backups directory we no longer need copy2.txt. Remove the file copy2.txt in this directory.

If you want to copy or remove an entire directory with all the files in it the normal cp and rm commands will not work. Use cp -r instead of cp or rm -r (the r stands for “recursive”) instead of rm to copy or remove directories:

  1. Remove the backups directory entirely using rm -r backups.

Run a Python program

python3      # runs the python program

In this class you will learn Python. To run a Python program, you just specify the command python3 and the name of the file that contains your program.

Use ls to verify that there there is a file in your lab1 directory. Now run the program in the file typing in:


This program is a very simple. It just prints “Hello, World!” to the screen.


There are several variants of Python, including Python 2.7 and Python 3. We will be using Python 3 and the corresponding python3 interpreter. The CSIL machines have Python 2.7 installed as the default Python. As a result, the command python runs a version of Python 2.7. There are some differences between the two languages and your Python 3 program may not run properly using a Python 2.7 interpreter.

Edit and run a Python program

In this section you will modify, recompile and rerun the program in This change is very simple but goes through all the mechanical steps necessary when programming:

You can open the file with the command:


The file contains a single line of code:

print("Hello, World!")

Change this line so that it instead says “Hello ” and then your name. For example if your name was Barack Obama the line would read:

print("Hello, Barack!")

Do the following steps:

  1. Save the file in sublime-text (forgetting to save is a surprisingly common error)
  2. Rerun the program using python3

Is your terminal not responding? This is because it is busy running sublime-text so it can’t hear you typing python3 at it. You have three options:

  1. Close sublime-text, do terminal work, reopen sublime-text. This is annoying.

  2. Start a new terminal in the same location by clicking on your current terminal and pressing Ctrl-Shift-N. You can use this one to run Python programs while the other runs sublime-text.

  3. Close sublime-text and open it again but this time put a & symbol at the end of the line. This means “Run Sublime-Text and let us do something else”. You can then use the same terminal to run Python programs even while sublime-text is running:

    sublime-text &

Option 1 may be seem the most natural to you. Do not chose Option 1. We strongly recommend getting the habit of keeping both your editor and a terminal window open at the same time. We’ve seen students waste a lot of time opening and closing their editor and terminal windows for no good reason.

Let’s reinforce the steps to programming in Python with the terminal:

  1. Make a change to your .py file with an editor
  2. Save
  3. Run with python3


Git is a system used for developing software in a group. This system maintains files and all changes that are applied to them. You will each have a personal Git repository that is hosted on a central server. The server stores the project files and stores all changes to those files that have been uploaded to the repository.

We have created accounts and repositories for each of you on a CS department Git server. We will seed your repositories with templates and files that you need for labs and programming assignments. Also, we will be able to see any changes you upload to your repository, which allows us to provide help remotely, grade your programming assignments, and provide feedback.

Git tracks every version of a file or directory using commits. When you have made changes to one or more files, you can logically group those changes into a “commit” that gets added to your repository. You can think of commits as “checkpoints” in your work, representing the work you’ve done since the previous checkpoint. This mechanism makes it possible to look at and even revert to older versions of a file by going back to your code as it was when you “checkpointed” it with a commit.

When using Git, your basic working cycle will be:

  • Log into a CS machine
  • Change to your cs121-aut-15-username directory
  • Download updates from the Git server (we will add files to your repository throughout the quarter). In Git, this operation is called pulling from the server.
  • Work on your files
  • Create a commit with any changes you have made
  • Upload the commit to the Git server. In Git, this operation is called pushing to the server.

The course staff does not have access to any files stored in your home directory or files that you have not pushed to the Git server. All we can access are files that have been pushed to the Git server, so remember to always push your latest commits when you’re done or when you ask a question on Piazza that will require us to look at your code.

Creating a commit

Creating a commit is a two-step process. First, you have to indicate what files you want to include in your commit. Let’s say we want to create a commit that only includes the file that you modified earlier. We can specify this operation explicitly using the git add command:

git add

To actually create the commit, use the git commit command. This command will take all the files you added with git add and will bundle them into a commit:

git commit -m"Made some changes to"

The text after the -m is a short message that describes the changes you have made since your last commit. Common examples of commit messages might be “Finished part 1 of the homework” or “Finished lab 1”.


If you forget the -m"Comment" at the end then Git will think that you forgot to specify a commit message. It will graciously open up a default editor so that you can enter such a message. On the CS machines this editor is nano. To escape the nano view, press Ctrl-X. Now try git commit again and don’t forget the -m"Comment".

Once you run the above command, you will see something like the following output:

[master 99232df] Made some changes to
1 file changed, 1 insertion(+), 1 deletion(-)

You’ve created a commit, but you’re not done yet: you haven’t uploaded it to the server yet. Forgetting this step is actually a very common pitfall, so don’t forget to upload your changes. You need to use the git push command for your changes to actually be uploaded to the Git server. If you don’t, the graders will not be able to see your code. Simply run the following:

git push

You should see something like this:

Counting objects: 7, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 452 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
   c8432e4..99232df  master -> master

You can ignore most of those messages. The important thing is to not see any warnings or error messages.

You can verify that our Git server correctly received your commit by visiting the following page:

Where username should be substituted by your CNetID.

This URL takes you to the web frontend of our Git server (please note that you will have to log in using your CNetID and password). More specifically, the above URL will show you the contents of your repository, exactly as it appears on the Git server. You can click on “Files” to see your repository’s files, and on “Commits” to see the latest commits uploaded to the server. If you see a commit titled “Made some changes to Hello World”, then your commit was successfully uploaded.

In general, if you’re concerned about whether the graders are seeing the right version of your code, you can just go to the above URL. Whatever is shown on that page is what the graders will see. If you wrote some code, and it doesn’t show up in the above URL, make sure you didn’t forget to add your files, create a commit, and push the most recent commit to the server.

Pulling changes from “upstream”

When we distribute new homework assignments or lab materials, we will do so through Git. These files are located in a separate repository on our Git server, which we call the “upstream” repository. The setup script you ran earlier already configured your Git repository so you can easily download any new files we upload to the upstream repository). To download these changes, run this command from inside the cs121-aut-15-username directory:

git pull upstream master

If you run it now, nothing will actually happen, since we haven’t changed anything in “upstream” since the start of this lab. You should see something like this:

 * branch            master     -> FETCH_HEAD
Already up-to-date.

When you pull from “upstream”, Git automatically downloads any new files or changes that have been committed to “upstream” and updates the files in your repository. If you have made local changes to files that have changed upstream, Git will attempt to merge these changes.

After you’ve pulled from upstream, any new files or changes will only be downloaded to your local copy of cs121-aut-15-username. As with any other changes to your code, you need to run git push to upload them to the Git server (you don’t need to do a git commit to prepare a commit, though; git pull already takes care of this task).

  • Every time your work on your code, you should run git pull upstream master in your cs121-aut-15-username directory before you do anything else. Sometimes, the instructors notice typos or errors in the code provided for a programming assignment, and they’ll commit fixes to upstream. By running git pull upstream master, you can make sure that those fixes propagate to your code too.

Pulling your changes from the server

If you have done work and committed it to the server from a lab computer and now wish to work on your VM, you will need to pull these changes from the server to your VM. To download these changes, run this command from inside the cs121-aut-15-username directory:

git pull

It is important that you commit your changes after every session and that you pull from both upstream and cs121-aut-15-username before you start to do any work.

git add revisited and git status

So far, we’ve created a single commit with a single file that we had already supplied in the lab1 directory. If you create new files, Git will not consider them a part of the repository. You need to add them to your repository explicitly. For example, let’s create a copy of


Is part of your repository? You can use the following command to ask Git for a summary of the files it is tracking:

git status

This command should output something like this:

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#   modified:   test.txt
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
no changes added to commit (use "git add" and/or "git commit -a")

The exact output may vary depending on how far along you got in previous sections of the lab. However, the important thing is that there are two types of files listed here:

  • Changes not staged for commit: This is a list of files that Git knows about and have been modified since your last commit, but which have not been added (with git add).

  • Untracked files: This is a list of files that Git has found in the same directory as your repository, but which Git isn’t keeping track of.

    You may see some automatically generated files in your Untracked files section. Files that start with a pound sign (#) or end with a tilde, should not be added to your repository. These files are automatically generated. Files that end with a tilde are backup files created by some editors that are intended to help you restore your files if your computer crashes. In general, files that are automatically generated should not be committed to your repository. Other people should be able to generate their own versions, if necessary.

To add a previously untracked file to your repository, you can just use git add (unlike the previous commands, don’t actually run this just yet; you will be doing a similar exercise later on):

git add

If you re-ran git status you would see something like this:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#   new file:
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#   modified:   test.txt

Notice how there is now a new category of files: Changes to be committed. Adding not only added the file to your repository, it also staged it into the next commit (which, remember, won’t happen until you actually run git commit).

The git status command reports the status on the local copy of the full repository. If you wish to look at the status of a smaller part of the repository (the directory you are working in for example), you can add a path name to the status command. For example:

git status .

reports the status of the current directory (a single dot is the path used to refer to the current directory).


  1. You have already changed the test.txt file in your directory. Verify this by using the command git status. You should see it under Changes not staged for commit.
  2. Use git add and git commit to create a commit that includes only the test.txt file. A good commit message would be “Added my name as Author in test.txt”.
  3. Upload your work to the server using git push.
  4. Verify that this file was sent by again using the command git status. You should see that the file test.txt is no longer listed.
  5. Let’s revisit the file we created earlier. If you run git status, it should show up under Untracked files. Add it to the repository using git add.
  6. Run git status again. Is in a different category of files now?
  7. Although we have added this file, we have not yet created a commit. Create a commit and push it to the server.
  8. Run git status a final time to verify that was committed (if so, you should not see it in any category of files)
  9. Run git push to upload your changes to the server.

We strongly recommend you to check in and push changed files as often as possible, especially if you finished some work and are about to log off a computer. This way the latest files are accessible from any other computer that has Git installed.


You will be using a locally-developed system named chisubmit to submit your programming assignments. The set-up script that you ran earlier set you up to use chisubmit in addition to initializing your Git repository.

All chisubmit commands should be run from within your cs121-aut-15-username directory.

chisubmit has commands for managing assignments. Here are descriptions and sample runs of some of the more useful commands. Do not run these just yet.

chisubmit student assignment list: lists upcoming programming assignments and their deadlines.

chisubmit student assignment list

pa1     2015-10-08 17:00:00-05:00   Programming Assignment 1
pa2     2015-10-15 17:00:00-05:00   Programming Assignment 2
pa3     2015-10-22 17:00:00-05:00   Programming Assignment 3
pa4     2015-10-29 17:00:00-05:00   Programming Assignment 4
pa5     2015-11-12 17:00:00-06:00   Programming Assignment 5
pa6     2015-11-19 17:00:00-06:00   Programming Assignment 6
pa7g    2015-12-04 12:00:00-06:00   Programming Assignment 7 (graduating students)
pa7     2015-12-09 12:00:00-06:00   Programming Assignment 7

chisubmit student assignment show-deadline <assignment name>: lists deadline information for the specified programming assignment.

chisubmit student assignment show-deadline pa1

Programming Assignment 1

      Now: 2015-09-21 08:54:29-05:00
 Deadline: 2015-10-08 17:00:00-05:00

The deadline has not yet passed
You have 17 days, 8 hours, 5 minutes, 31 seconds left

chisubmit student assignment register <assignment name>: registers a student for a specific assignment. You will do this step once per assignment.

chisubmit student assignment register pa0
Your registration for pa0 (Programming Assignment 0) is complete.


Commits in git are identified by an SHA-1 hash, and look something like this:


You can see the list of commits in your repository by running the following:

git log

Or, you can simply save the SHA-1 hash of your latest commit in a Linux environment variable. The following command saves your most recent commit in an environment variable named COMMIT_SHA. Note the use of backticks (`) surrounding the git command. Those are NOT single quotes (‘)!:

export COMMIT_SHA=`git rev-parse master`

chisubmit student assignment submit <your CNetID> <assignment name> <commit sha>: submits the specified commit for the specified assignment:

export COMMIT_SHA=`git rev-parse master`
chisubmit student assignment submit amr pa0 $COMMIT_SHA

You are going to make a submission for pa0 (Programming Assignment 0).
The commit you are submitting is the following:

      Commit: 9fe25d4cef1d6b4de85ae7dfacb97a40d492f821
        Date: 2015-09-23 15:21:55-05:00
     Message: Ready for submission
      Author: Anne Rogers <>


Your team currently has 0 extensions

You are going to use 0 extensions on this submission.

You will have 2 extensions left after this submission.

Are you sure you want to continue? (y/n):  y
Your submission has been completed.

Final Exercise - Putting it all together

You have a programming assignment due next week. Let’s make sure that you can find, modify, and submit your programming assignments:

  1. Navigate to the pa0 subdirectory inside the cs121-aut-15-username directory.
  2. Register for pa0 using chisubmit.
  3. Replace the text that reads “YOUR NAME HERE” with your actual name in the test.txt file in your pa0 directory.
  4. Check in this change to your Git directory with the commit message “Test commit to pa0”. Don’t forget to push it to the Git server!
  5. Go to (replace username with your CNetID) and click on “Commits”. Does your commit message show up there? If not, maybe you forgot to push your commits to the server.
  6. Submit your “solution” to pa0 using chisubmit.

Advanced Text Editors

In addition to Sublime Text, Linux also has a number of text editors that run in the terminal. They do not have graphical user interfaces, which means all commands are typed with the keyboard and not with a mouse. While this may seem restrictive these editors are extraordinarily powerful and can automate many tedious tasks. vim and emacs are the most popular among these.

If you run emacs & in the terminal it will give you the option to do the “Emacs Tutorial”. You can also read an online tutorial.

If you want to know more about vim you can read a tutorial online or type vimtutor into the terminal.