Linux

Objectives

  1. Become familiar with the Linux environment

  2. Learn basic terminal commands and how to work with a text editor

  3. Learn to use svn

  4. Learn to compile a Java program

Linux

Linux is an operating system much like Windows or Mac-OSX. It has windows, programs, web browsers etc.... Files are stored in directories which are stored in other directories. You can access all of this 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. In this lab we 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.

Terminal/Shell

At home you probably navigate your hard drive by double clicking on icons. While this is convenient for simple tasks this approach is limited. For example it is challenging to delete all of the music files over 5mb 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.

Go to Applications : Accessories : Terminal to open the terminal window. Alternatively use the keyboard shortcut Ctrl-Alt-T.

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.

Files in Linux are stored in directories/folders, just like in Windows/Mac-OSX. Directories can hold files or other sub-directories and there are special directories for your personal files, your Desktop, etc....

Name Linux Mac Windows
Root directory / / C:\
Home directory /home/username /Users/username C:\Documents and Settings\username

All of these computers are connected through a network file system. Effectively there is one very large shared hard drive. Your files are available from any computer and all of our directories live in the same space.

The layout to the right is how Linux organizes the file system. Your computer at home might have a slightly different organization (i.e. you might replace / with C:, but the idea is the same.

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 MacLab 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. It looks like what you might see in a terminal window after you type in pwd and ls.

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

Try this yourself and verify that everything looks similar.

Change Directory

cd path-name     change to the directory path-name
cd ..            move up/back one 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 
/home/username/
username@computer:~$ ls 
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
username@computer:~$ cd Desktop
username@computer:~$ pwd
/home/username/Desktop/
username@computer:~/Desktop$ ls

username@computer:~/Desktop$

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

/home/username/Desktop/

rather than

/home/username/

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:~$ pwd
/home/username/Desktop/
username@computer:~/Desktop$ cd ..
username@computer:~$ pwd
/home/username/

Downloading your CS121 directory with SVN

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 SVN, a code-sharing tool. SVN will be described in more depth later in the lab. For now please execute the following steps

Copy-Paste: In Windows/Mac you usually copy-paste with Ctrl-C and Ctrl-V. These 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 svn co ... above to copy it. You can paste by middle clicking where you would like it to go. Middle click in the terminal.

Error validating server certificate for 'https://phoenixforge.cs.uchicago.edu:443':
 - The certificate is not issued by a trusted authority. Use the
    fingerprint to validate the certificate manually!
    Certificate information:
 - Hostname: wooldridge.cs.uchicago.edu
 - Valid: from Fri, 28 Sep 2012 00:00:00 GMT until Mon, 28 Sep 2015 23:59:59 GMT
 - Issuer: InCommon, Internet2, US
 - Fingerprint: f6:7c:48:d0:fd:8b:28:45:59:c4:28:f4:18:87:b0:f9:14:a4:bf:c9
(R)eject, accept (t)emporarily or accept (p)ermanently? 

You should press p and then Enter.

After running the svn co command list the files in your home directory. You should see a new directory username-cs121-aut-12. This directory will contain all of your work for this class. It contains a sub-directory, lab1, that has some files for us to play with. We will learn how to manipulate these files in the next section. For now, use pwd, ls, and cd to navigate yourself into the lab1 sub-directory.

Using a text editor

List the files in the lab1 directory. You should see the following

configure.sh  HelloWorld.java  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 a very simple one, gedit. It is similar to Notepad for Windows or Textedit for Mac.

Open the file test.txt with gedit by typing the following into the terminal

gedit 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 gedit 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 username-cs121-aut-12 directory.

GEdit should be familiar to you from using other similar programs on your own computer. Make sure that you are comfortable with it by

  1. Adding your name after Author: in this file
  2. Saving
  3. Closing and reopening the file in GEdit 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, the original file is deleted
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:

cp SOURCE DESTINATION

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 gedit. Your terminal is currently busy running the gedit program and will not be responsive until this program is closed.

Exercises

  1. Do this copy now 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 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 instead of rm to copy or remove directories.

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

Compile and run a Java program

javac File.java     # produces an executable program File.class
java  File          # runs the executable program File.class

In this class you learn Java. In order to run a program that is written in Java it must first be compiled from human-readable .java code file to a computer executable .class file. Humans write and read .java files, computers read and run .class files.

To perform this transformation we use command javac FILENAME.java to compile the file FILENAME.java to the executable FILENAME.class To execute the just compiled program, you run it in Java with the command java FILENAME. (note: do not include .class at the end)

Use ls to verify that there there is a file HelloWorld.java in your lab1 directory. Compile it using the javac command

javac HelloWorld.java

Use ls to verify that this created a file HelloWorld.class.

Now run the HelloWorld program by typing in

java HelloWorld

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

Edit, compile, and run a Java program

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

    class HelloWorld 
{
public static void main(String[] args)
{
System.out.println("Hello, world");
}
}

Open the file HelloWorld.java with gedit. Find the line

    System.out.println("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

    System.out.println("Hello, Barack!");

Do the following three steps

  1. Save the file HelloWorld.java in gedit (this is a surprisingly common error)
  2. Recompile the .java file into a .class file using javac
  3. Rerun the program using java

Is your terminal not responding? This is because it's busy running gedit so it can't hear you typing javac and java at it. You have two options.

  1. Close gedit, do terminal work, reopen gedit. 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 compile and run Java programs while the other runs gedit.
  3. Close gedit and open it again but this time put a & symbol at the end of the line. This means "Run gedit and let us do something else". You can then use the same terminal to compile and run Java programs even while gedit is running.
    gedit HelloWorld.java &

Lets reinforce the four steps to programming Java in with the terminal.

  1. Make a change to your .java file with an editor
  2. Save
  3. Compile with javac
  4. Run with java

Automatically Configure your account for CMSC-121

In this section you will run a small program which tells your computer how to find some course materials. During the term we will supply data and programs for you to use in homeworks and labs. We need to configure your account so that it knows where to find these resources.

The third file in the lab1 directory is configure.sh. This is a very small program that configures your account. We don't expect you to understand how it works but we do need you to execute the following commands.

  1. cd into your lab1 directory if you're not already there.
  2. Type ./configure.sh to run the configure program
  3. Verify that "System configured" was printed to the screen. If it was not please contact your lab TA. This step is important.
  4. If you're interested view the contents of configure.sh using gedit. This is written in a language called BASH. It is not important that you understand it at this point.

SVN

SVN stands for subversion, which 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 svn 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 SVN server named PhoenixForge. We will seed your repositories with templates and files that you need for homeworks and labs. 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.

Subversion tracks every version of a file or directory using revision numbers. When you upload changes to a file, svn increments the revision number. This mechanism makes it possible to look at and even revert to older versions of a file.

The basic working cycle is: log in, change to your username-cs121-aut-12 directory, run an update, work on your files, and then when you are finished, commit any changes you have made.

The course staff does not have access to any files stored in your home directory or files that you have not uploaded to the svn server. All we can access are files that have been checked in to the SVN server, so remember to upload all changed files.

There is a copy of your directory on the SVN server located at the PhoenixForge website.

Commit

You will use the command:

svn ci -m"COMMENT"

to commit your changes to the server. This process is also referred to as checking-in your changes. You will replace COMMENT with a short message that describes the changes you have made since your last check-in. You must run this command from within your username-cs121-aut-12 directory. Common examples of commit messages might be. "Finished part 1 of the homework" or "Finished lab 1".

Warning! If you forget the -m"Comment" at the end then svn will think that you forgot to enter in a commit message. It will graciously open up emacs the default editor so that you can enter such a message. Unfortunately emacs is very challenging to start with and you will likely find yourself trapped inside it. To escape emacs press Ctrl-X followed by Ctrl-C then y and enter then a and enter. Now try svn ci again and don't forget the -m"Comment".

Update

When we distribute new homework assignments, we will do so through SVN. We will add new directories and files to your repository. To see these changes on your local computer you will need to run an update. To pick up these changes, run the command:

svn up

from within your username-cs121-aut-12 directory. When you run an update, Subversion automatically downloads any changes that have been committed since your last update and updates your files. If you have made local changes to files that have changed on the server, Subversion will attempt to merge these changes.

Add

When you create new files and directories, you must add them to your repository using the command:

svn add NAME

replacing NAME with the name of the file or directory that you wish to add to your repository. Note: running svn add merely tells Subversion about the file or directory. The file/directory will be not be uploaded to the server until the next time you run a commit. It is common to run svn ci directly after running svn add. You only need to add a file once (when you first start working on it). You may wish to commit many times as you continue to make changes.

Some files will be generated as you write and compile programs. These files, which start with a pound sign (#) or end with a tilde or the .class extension, should not be added to your repository.

Status

Running the command:

svn status

will show you the status of all the files in your current directory. Files that have been modified will be shown with an M in the first column. Files that have been added, but not yet committed will be shown with an A in the first column. Files that are unknown to the repository will be marked with a ?. It is a good idea to run svn status before you commit, just to make sure you have not forgotten any files.

M - Modified
A - Newly added but not yet uploaded
? - Unknown - In the directory but not part of the repository

SVN on your computer

If you want to work on your own computer, you can install SVN on it and check out your files as you did earlier in this lab. You will only need to do this step once. After that you can use the standard, update, edit, commit cycle. Linux machines and Macs should have SVN installed on them and you can use it in the terminal. Windows users need to download an SVN client, there are multiple graphical clients on the internet that are easy to use. For example, you could use the following one [http://www.shokhirev.com/nikolai/programs/SVN/svn.html]. A google search for subversion and windows will bring up a lot of other SVN programs that you can install in your computer.

Sample use

This is an example, not an exercise. You do not need to execute these commands now. You will execute very similar commands in the exercises in the next section.

username@computer:~$ cd username-cs121-aut-12
username@computer:~$ ls
admin  hw1  L2  pa1  lab1 
username@computer:~$ cd lab1 
username@computer:~$ ls
configure.sh  HelloWorld.class  HelloWorld.java  test.txt

username@computer:~$ gedit HelloWorld.java      * Note: at this point you make some change to the file
username@compute:~$: svn status
M       HelloWorld.java

username@computer:~$ svn ci -m "Added name to HelloWorld.java file"
Sending        lab1/HelloWorld.java
Transmitting file data .
Committed revision 2.

username@computer: svn status         * Note: no changes have been done since the last checkin, so
                                      * svn status does not generate any output
username@computer:~$ gedit test1.txt  * We make a new empty file with gedit and save it into this directory.
username@computer:~$ ls 
configure.sh  HelloWorld.java  test.txt  test1.txt

username@computer:~$ svn status
?       test1.txt                     * svn does not know about test1.txt
username@computer:~$ svn add test.txt
A       test1.txt
username@computer:~$ svn status
A       test1.txt                     * Note: test.txt has been added to the local repository, but has
                                      * not yet been committed.

username@computer:~$ svn ci -m "Added test1.txt"
Adding         test1.txt
Transmitting file data .
Committed revision 3.

username@computer:~$ svn status       * We receive no output. All is well.
username@computer:~$ 

Exercises

  1. You have already changed the HelloWorld.java file in your directory. Verify this by using the command svn status. You should see an M next to HelloWorld.java and test.txt. The M stands for modified.

  2. Commit your work to the server using svn ci. A good commit message would be "Changed name in HelloWorld program".

  3. Verify that this file was sent by again using the command svn status. You should see that the file HelloWorld.java is no longer listed.

  4. Copy the file HelloWorld.java to NewFile.java.

  5. Run svn status again. What is the status of NewFile.java?

  6. This file has not yet been added to the repository (it is new). Add it to the repository using svn add.

  7. Run svn status again. What do you think the letter next to NewFile.java means?

  8. Although we have added this file to our local directory we have not yet submitted it to the server. Commit your work to send NewFile.java.

  9. Run svn status a final time to verify that all of its changes are resolved (if this is the case you should not see it listed).

We strongly recommend you to check in 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 SVN installed.

Final Exercise - Putting it all together

You have a programming assignment due next week. Lets make sure that you can find, modify, and submit your homework.

  1. Add your name to the top of the Dollar.java file in your pa1 directory just under the line

  2. Check in this change to your svn directory with the commit message "Test commit to project 1".

Advanced Text Editors

Linux 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 want to know more about vim you can read a tutorial online or type vimtutor into the terminal.

If you run emacs & in the terminal it will give you a choice "Emacs Tutorial" or you can read an online tutorial.

A powerful text editor is not required in this class but can be quite handy. You are encouraged to look them up in your free time.