CS 117: Introduction to the CS Lab Environment and Linux


The objectives of today's lab:

  1. Familiarize yourself with the CS lab environment
  2. Familiarize yourself with the Linux operating system

Introduction to the CS Lab Environment

In this class, you will be using the machines in CMC 306 for your programming assignments, projects, and labs.. These machines run both Windows and Linux. Most or all of you have used Windows already, but most likely you are not familiar with Linux. We'll be learning more about Linux in today's lab.

Windows and Linux are examples of operating systems. An operating system is the set of programs that tells the computer what to do when. It handles things like determining which program or process gets to use the computer at any given time, how data is stored in files, and so on.

In this class, you will be programming using the Java programming language in the Linux operating system. In particular, you will be using the following programs:

In the next part of today's lab, we will be covering the basics of the Linux operating system. You will learn some basic commands that you will need to use over and over throughout the course of the term.

Introduction to Linux

By now, you have logged in to the lab machines and started Mozilla. Take a few minutes to familiarize yourself with the computing environment. You've already found the web browser; see if you can also find examples of the following programs:

Now we will learn some of the basic Linux commands. You will be using these commands throughout the term. Before we dive in, though, we have to open up a terminal window. A terminal window, or xterm, is where you type in the commands to interact with the computer: create and delete files, compile and run programs, get help, etc. To open a terminal window, do either of the following:

Before the end of today's lab, both you and your lab partner should try logging in to your lab computer.

Note: Both Linux and Java are case-sensitive, so pay attention to what you are typing. Upper case letters vs. lower case letters are different.

Directories

Once you have the terminal window up, type pwd and then hit the Enter key. pwd stands for "print working directory"; it lists the name of the directory that you are currently in. Linux organizes everything according to directories (some other operating systems call these folders). Some of these directories are created by the system, but directories can also be created by the user. Each user has his or her own "home directory", where all the files created by that user are placed. This is a bit different from Windows, because on Windows a user can put a file anywhere and there is no concept of a "home directory" (although the "My Documents" folder is close). When you type pwd now, it should show that you are in your home directory. Make a mental note of what your home directory's full name is.

Now type ls (this is a lowercase "L" and not the number "1") and then hit the Enter key. ls is the command for "list files and directories". It will list all the files and directories that are currently in your home directory. There should be a couple of things there that Linux automatically creates. You can ignore them.

Let's create some directories. A good idea is to store groups of things in separate directories. Some people organize their home directories to contain directories named "classes", "programs", "data", "docs", and so on. You might want to create separate directories for each lab or homework assignment for this class. Directories can also have subdirectories; so, for instance, a "classes" directory can have subdirectories "fall2004", "winter2005", "spring2005", and each of these subdirectories can have directories for each class during that term.

Note: While in Windows it's perfectly acceptable to use spaces in your filenames and directory names, in Linux you want to avoid doing this. (You can do it, but it's a bit of a headache to deal with.) So, what happens if you have a file or directory name that's more than one word long, like "financial docs"? You can either use an underscore in between the two words ("financial_docs"), or use capitalization to delineate the words ("financialDocs" or "financialDocs"). Either one is acceptable, though the second approach will be consistent with the Java programs that we write.

Type mkdir labs in the terminal window and press Enter. mkdir stands for "make directory". Now if you type the command ls, you should see that directory listed in your home directory. It has been created.

Now let's create a subdirectory. Type cd labs and press Enter. cd stands for "change directory". Enter the commands ls and pwd and make a note of what you see. You are now in the "labs" directory that you just created. Now use mkdir to create another directory here, to store the files for this lab that you will create. Name the new directory "lab1".

If you ever want to remove a directory, use the command rmdir. For example, to remove the directory "foo", type:

rmdir foo

Try creating and deleting some directories now.

Shortcuts: At any time, to return to your home directory, you can just type cd, with no directory name afterwards. To go up to the parent directory of whatever directory you're in, type cd ..

Creating and editing files

Go back into the "lab1" directory that you created. We will now create some files to put in this directory. To create files, we will use the program NEdit. NEdit is a text editor that is very similar to Windows Notepad or other programs you may have used; in fact, it has similar menus and, in most cases, identical "shortcut" keys (such as [CTRL]s to save a file). Start NEdit by doing one of the following:

A blank window should appear. Write a short poem in this window, and save it in the "lab1" directory as "poem.txt". Then exit out of NEdit.

Did the poem save in the correct spot? To check, list the contents of the "lab1" directory. You should now see "poem.txt" listed. To view the contents of the file, type more poem.txt. The more command is useful for checking the contents of a file without having to open a text editor.

To remove a file, use the command rm. For example, to remove the file "foo.txt", type:

rm foo.txt

Note that rm will permanently remove a file, so use with care! There is no "recycle bin" or "trash can" in Linux.

Try creating and deleting some more files.

Relocating files and directories

Sometimes, we'd like to move and copy files around. For example, maybe you're afraid that you'll accidentally delete your beautiful poem, so you want to create a backup copy. Or, maybe you made a note to yourself to remind you of a meeting later, and you want to move this to your home directory (where you'll see it) rather than keeping it in your "lab1" directory (where you'll most likely miss it). Fortunately, Linux has commands for these too:

Practice moving, copying, creating, and deleting more files now.

Getting help

The instructor, prefector, and lab assistants are always a great source for assistance. As it turns out, though, there is also help built-in to Linux! You can get help on any Linux command or program by using the man command. man is short for manual. If you type in man followed by the name of any Linux command or program, you will get a help page for that program or command. Look at the man pages for all of the commands that we've used in today's lab. Is there a man page for Mozilla? For NEdit? Note: To exit out of man, type the letter q.

Notes, tips, and tricks

This page lists all of the commands that we've used in today's lab. Feel free to use it as a reference.


Authored originally by Amy Csizmar Dalal. Modified by Dave Musicant.