CSC15 Assignment 1: Using Linux
Due one week from date assigned
This lab is intended for you to:
- Learn basic Linux "survival skills"
- Learn how to enter and execute a couple of simple Python programs.
- Take a glimpse at the joys and pains of programming by modifying some
simple program lines.
Like most operating systems, Linux has a graphical interface and many
aspects of it are self-explanatory. But to study computer science in
depth you must become fluent with a very different way of
communicating with the operating system. In fact, most server-class
machines don't even have graphical interfaces. There are several
differences between Linux and PCs and Macs that you need to
understand.
- Once you've logged in, explore the desktop environment. Try to
bring up the Firefox web browser. Go to and bookmark the class homepage:
www.cs.hofstra.edu/~cscccl/csc15p
- Bring up a "terminal" window under "Applications" -> "System Tools".
This is the main interface you will use. The first step in becoming an
advanced computer user or professional is to learn how to use the
computer without a graphical interface.
- The command prompt will read something like "-bash-4.2$". If not, Type "bash" first..
- Type "pwd" - this command shows you what directory you're currently
in. Directory names in Linux look like /home/username/subfolder/subsubfolder/... The directory (or folder) / is called the root directory. The /home/ directory contains folders for all users. When you log in, you are automatically placed in your own user directory. This is where you will organize all your class materials. Note that the slash "/" is in the opposite directory as the Windows directory slash "\".
- Type "ls" - this command shows the contents of the current
directory.
don't be surprised if there's nothing there - that's why we're here!
You can also use "dir" instead of "ls".
- Type "ls -a" - As with MS Windows, there are some hidden systems files
that are usually not visible. This command will display all files and
directories in the current directory.
- Type "ls -l" - This is the same as ls but gives you additional information
for each file and folder.
-
Basic Command Summary
- cd (directory name): change directory to named directory
cd .. : go to parent directory
cd /(path) : absolute directories begin with "/", the root
directory.
For example, if you are in directory /shared/csc/local,
typing "cd .." will take you to the /shared/csc directory.
- Typing "cd" by itself will take you to your home directory.
- pwd : tells you the current directory you're in.
- cp file1 file2 : copies the contents of file1 into a file called file2.
- ls : lists directory contents
- mkdir dirname : creates a new directory called dirname in the current directory
- mv file1 file2 : renames file1 into file2
- rm file1 : deletes file1
- rm -rf dirname : deletes the named directory and all its contents. Use with care!
- more filename : a quick way to view the contents of a text file.
Many of these operations can also be performed using the mouse in the
"file manager" window. However, you'll need to learn these commands if
you want to use the workstations remotely using telnet (I'll show
you how to do that later). Besides,
all serious computer users know them.
Let's now practice these commands (please do not deviate from instructions)
- Make sure you're in your home directory (type "cd")
- Type "mkdir csc15" - what does this do? You should creat a directory
for this class because you'll be using your account for other classes as well.
NOTE: if you're using one of the public accounts (adams01 - adams04), create a directory with your username instead, so you can keep your files separate.
- Type "cd csc15" - you're now in the directory you just created.
- Type "cd .." - you've moved up one directory level (to your home directory).
Please note: The "cd .." command does NOT always return you to the last directory you where in: it always moves you up one level in the directory hierarchy.
- Now use a command to back into your csc15 directory
- Using Python in interactive mode.
Type "Python2" to enter the interactive environment. Make sure that you're using Pyton version 2.7.x, and not Python 3: these versions of Python are not compatible (though similar). Most Linux/MacOs and various server code still rely on Python 2. Go to the "python
tutorial" link on the class homepage, and try to follow some of the
examples in section 3. You're encouraged to experiment. The interactive
mode allows you to quickly the range of valid syntax in Python.
When you're done, type Control-D to exit the Python interpreter.
Please note that while you're inside the python interpreter, you'll
see a prompt like ">>>". This is NOT where you can enter linux commands
like ls, cd, etc ... To do that, you need to be at the "shell" prompt
(like bash-3.00$).
- Time to write a program.
To do that you need to learn how to use an editor. A very powerful
(though less easy than MS word) editor used by programmers is "emacs"
- type "emacs &" - you'll see a window come up. The window may
be split into two halfs: click on the top half and type Cntrl-x 1 (control-x, then 1).
You should recognize a toolbar with
familiar buttons such as "file", "edit" and "help". Select "file" and
open a file called "firstprog.py". As a shortcut, you can also type
"emacs firstprog.py &" to open the file in one step.
- Type in the first program provided on the first attached sheet.
You do not have to type in all the comments marked with "#", but
put your name in comments at the top. Comments are lines that begin
with a "#".
- save it under the
name "firstprog.py" in your csc15 directory.
- Make sure you're in the
csc15 directory. Execute the program with "python firstprog.py"
Did you get any errors? If you did,
that means you had a typo while copying the program, so go back and
edit the program, save it, and try again.
- Change the program so that it prints your name 20 times instead of 10
- (harder). Change the 'for' loop in the program so that it prints your name 20 times BACKWARDS.
- Quit emacs (click the "file" menu option).
- You can also start emacs and open a file at the same time by typing
emacs (filename) &.
The "&" is important: without it, you'll lose the ability to use the
terminal window while emacs is running.
- Some additional emacs commands. You'd be surprised how much easier it
is to use keyboard commands instead of the mouse sometimes. Here are a few
you can use in emacs (Cntrl-xs means hold the control key and type xs):
- Cntrl-xs: save file
- Cntrl-xf: find/open file
- Cntrl-xb: switch between buffers
- Cntrl-x2: splits window (Cntrl-x1 makes it a single window again)
- Cntrl-g: reset (do this when something goes wrong)
- Cntrl-p: move cursor up
- Cntrl-n: move cursor down
- Cntrl-k: erase line of text, saves in buffer
- Cntrl-y: pastes text saved in buffer:
- Esc-x, then type 'goto-l' at the bottom to jump to a line number.
- Cntrl-xc: quit emacs
- Now type in the second Python program, "secondprog.py"
- Run the second program with 'python2 secondprog.py' or '/usr/bin/python secondprog.py'
Some additional Linux commands for program management. You may wish to terminate or suspend a running python program. Usually Cntrl-C (from the terminal
window) would work. If that doesn't work, tryp Cntrl-Z. This command will
suspend the running process, but does not kill it (it becomes a zombie).
The command "jobs" will the current running or suspended jobs. Each job
will have a number in [] brackets. To kill the job permanently, use the command kill -9 %n, where n is the number of the job.
You are to turn in the source code of the two programs you typed in and
modified. Make sure programs have your name in #comments. Submit to
hofstra.blackboard.com (csc15L)
Final Note: you are expected to remember what you learned today,
for these skills are required throughout the semester. Practice these
skills further on your own time. A quiz on basic linux commands will
be given shortly