The objectives of this lab:
You should complete this lab before class on Wednesday, 2 April 2008. (There's nothing to turn in on paper—the purpose of this lab is just to give you practice with some of the tools that you'll need throughout the term. You'll submit your work on this lab to get practice with the homework submission program ("hsp") that you'll use throughout the term, but it's not for a grade.)
You will be using the machines in CMC 306 for your programming assignments throughout the term. 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. Today's lab will help you get started with using Linux.
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 CS 111, you will be programming using the Python programming language in the Linux operating system. In particular, at some point you will need to use the following programs:
nedit
, which is very similar to
Windows Notepad;xterm
(short for
x-terminal), in which you will run various commands to accomplish
various tasks;hsp
, a simple program that you will use throughout
the term to hand in your homework assignments electronically.If you're reading this assignment from a computer in the 306 lab, then you've already logged in to a computer and started up a web browser. If you're reading this from elsewhere, well, go to the lab! Pick an unoccupied computer, log in, and start up Firefox. (If the screen doesn't say "Red Hat" or "Linux" on it, then you'll have to reboot—the computer is currently set to load Windows instead of Linux. To start Firefox, there's an icon on the top of the screen that looks sort of a like a globe; click on it to get started.)
The computer where you're sitting is named after a famous computer scientist, specifically one who has won a Turing Award (essentially the Nobel Prize of computer science). If you're interested, check out the Wikipedia entry for "Turing Award" and read a brief biography of the person for whom your machine is named.
In this lab, we will be covering the basics of the Linux operating
system, including nedit
and xterm
. (Firefox
should be fairly natural, and you'll deal with hsp
at the
end of this lab.) You will learn some basic commands that you will
need to use over and over throughout the course of the term.
Take a few minutes to familiarize yourself with the computing environment. You've already found the web browser; feel free to poke around to find other applications: drawing or graphics programs, word processors, email programs, media players, etc. You can also fiddle with the display settings (try right clicking on the desktop ...) if you'd like.
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:
Note: Both Linux and Python are case-sensitive, so pay attention to what you are typing. Upper-case letter and lower-case letters are different.
Once you have the terminal window up, type pwd
and
then hit the Enter key. The command 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
(as in lower-case "ell ess") and then hit
the Enter key. The command ls
is short 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. It's a good idea 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 "fall07", "winter08", "spring08", 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 want to have a file or directory name that's more than one word long, like "grad school applications"? You can either use an underscore in between the two words ("grad_school_applications"), or use capitalization to delineate the words ("GradSchoolApplications" or "gradSchoolApplications"). Either underscores or capitalization works fine, though the latter will be consistent with the Python programs that we write.
Type mkdir cs111
in the terminal window and press
Enter. The command 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 cs111
and
press Enter. The command cd
stands for "change
directory." Enter the commands ls
and pwd
and make a mental note of what you see. You are now in the "cs111"
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 "ps0".
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 ..
Go back into the "ps0" 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:
nedit
in the terminal window.A blank window should appear. Write a short poem (musical lyrics or the result of gentle random pounding on the keyboard is also okay) in this window, and save it in the "ps0" directory as "poem.txt". Then exit out of NEdit.
Did the poem save in the correct spot? To check, list the contents
of the "ps0" 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.
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 "ps0" directory (where you'll most likely miss it). Fortunately, Linux has commands for these too:
cp
(short for "copy") copies a file from
one place to another. Example: To make a backup copy of your poem,
type:
cp poem.txt poem.txt.bak
This creates a copy of the original file, "poem.txt", in a new file
named "poem.txt.bak". Do this and then type ls
. You
should see both files listed. Use more
to verify that
the contents of the two files are the same.
mv
(short for "move") moves a file from
one place to another. Let's say you want to move the backup copy of
your poem to your home directory. Type:
mv poem.txt.bak
[insert the name of your home
directory here]/poem.txt.bak
Now, if you do ls
in "ps0",
you should not see "poem.txt.bak" anymore. But, if you
cd
to your home directory, you should see a new file
there. Verify that its contents are the same as the old file.
Shortcut: Type mv poem.txt.bak ../..
(be
sure you get all the periods in there). What does this do? Well,
we've already learned that cd ..
will take us up to the
parent directory. If we're in the directory "ps0", that means we're
two levels below the home directory. So ../..
says "go up
two levels."
Practice moving, copying, creating, and deleting more files now.
Submit your poem and the other work that you've done here via
hsp
. (Note: you can submit entire directories using
hsp
, so you should just submit your entire "ps0"
directory.)
Instructions for using hsp
are here.
The instructor, prefector, and lab assistants are always great
sources for assistance. But you can also get help from Linux itself!
You can get help on any Linux command or program by using the
man
command (short for "manual"). If you type
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 Firefox? For NEdit? For man
itself?
Note: To exit out of man, type the letter
q
.
[CTRL]z
. You should see a message that says "suspended" and see the command prompt again. At the prompt, type bg
.
This puts NEdit into "background" mode, which essentially means that
Linux will switch back and forth between running NEdit and running
whatever other commands you type in to the terminal window.&
after the command. So, for instance, if you type nedit &
,
NEdit will start, but you will also see the command prompt right away
in the terminal window. Now you can run NEdit and type in commands as
well.nedit foo.txt
will open NEdit and that file simultaneously.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 and David Liben-Nowell.