UMBC CMSC 202 Computer Science II
Lab1: Introduction to UNIX / Linux at UMBC
In this lab, you will learn the fundamentals of Linux that will enable you to
complete your projects and labs within this course.
You will learn about the following:
- Some essential linux commands
Essential Linux Commands
Opening a terminal
In the lab machines, simply run a terminal application. Your home directory and all your files are available without SSH. From a Windows machine at home, a good terminal application to use would be PuTTY. When connecting using PuTTY or another ssh client, connect to the server linux.gl.umbc.edu on port 22 with your UMBC username and password.
At this point, you should see a linux prompt, something like:
A sample terminal session
The linux prompt is a text-based interface for issuing commands to a Linux system. From here, you can change to a different directory, run a program, create a file, edit a file, or just about anything else.
This command lists all of the files and folders in the current directory.
- ls -l
Lists a long version of the directory contents including permissions,
owner, group, filesize, last editted date and filename. Directories at
the current level have a 'd' at the beginning of their permissions:
drwx------ 16 don1 rpc 2048 Jan 24 10:21 202
While files start with a '-':
-rw------- 1 don1 rpc 383 Jan 24 11:56 hello.cpp
- mkdir cmsc202
This command will make a directory named 'cmsc202'. If you already know
something about Linux and have created a 202 directory elsewhere, then you may
skip this command and simply go to that directory (i.e. make directory). You
may name your folder something other than 'cmsc202' if you like, but this lab
will assume you have named it 'cmsc202'.
- ls cmsc202
When given a directory name, ls will list the files in that directory (however,
no files should be in your cmsc202 directory yet).
- cd cmsc202
This command will change the current directory to be 'cmsc202'. In other words
we will move into the 'cmsc202' directory (i.e. change directory).
- mkdir lab01
This command will make a new directory named 'lab01' within the 'cmsc202'
directory (since that is where we are currently located). Most commands work
in the current directory only, unless you explicitly state otherwise. You
can think of the file and directory structure as a hierarchy - each directory
has "child" directories or files. This also means that most every directory
has a "parent" directory.
- cd lab01
Change into the 'lab01' directory.
- touch Lab01.cpp
This will create an empty file named 'Lab01.cpp' that you can edit later.
- mv Lab01.cpp lab01.cpp
Move the file 'Lab01.cpp' in the current directory to 'lab01.cpp' in the
current directory. This is essentially renaming the file, but you could also
use this command to move the file to a different folder.
- mv lab01.cpp ../
Move the file 'lab01.cpp' in the current directory into the parent directory.
The '../' indicates "one directory up the hierarchy". By setting the
destination as a directory, the file will retain its original name, but be
moved to the new location. An equivalent instruction would be:
mv lab01.cpp ../lab01.cpp
- cd ..
This makes the parent directory the current directory (i.e. changes the
directory to be one "up" in the hierarchy). If you were to use ls at
this point, you will see the lab01.cpp file located outside of the lab01
- cp lab01.cpp lab01/lab01.cpp
This will make a copy of our file from the current directory to the lab01
directory. There are now two identical versions of 'lab01.cpp'.
- cp -r lab01 lab01_new
Create an exact copy of the entire directory 'lab01' as a new directory
'lab01_new'. If you 'cd' into the new directory, you will find a file there
named 'lab01.cpp' - this is an exact copy. The '-r' means "recursively" which
means that any directories and files in 'lab01' will be copied over to the new
location. This can be particularly important within this class as you will
want to copy over all of your project files into a new folder before working
on the next project!
- rm lab01.cpp
Removes the file 'lab01.cpp'. You will be prompted to confirm the deletion of
this file. If you would prefer to NOT be prompted, you can use the following:
rm -f lab01.cpp
The '-f' will "force" the deletion of the entire folder and its contents.
- rm -r lab01_new
Removes (recursively) the entire folder and its contents lab01_new. You will
be prompted to confirm the deletion of each item. If you would prefer to NOT
be prompted, you can use the following:
rm -rf lab01_new
An alternative to using 'rm' to remove a directory is to use 'rmdir' (remove
directory), but the directory must be empty, 'rmdir' doesn't work with the '-r'
- cd ~
Change directory to your home (initial starting point). '~' always represents
your home directory. For example, touch '~/hello' will always create the file
hello in your home directory, no matter which directory you are currently in.
Use the above linux commands to do the following:
- Create a folder named 'temp'
- Make 'temp' the current folder
- Create a file named 'temp.cpp' in the temp folder
- Move into the parent folder of 'temp'
- Copy the temp folder and all of its subfolders and files into a new folder
- Completely remove the folder 'temp' and the folder 'temp2'
There are many additional resources available (i.e. google)
that describe the different
commands available to you under a Linux system. You are encouraged to read
through them and familiarize yourself with their use.
- less lab01.cpp
Types the contents of the 'lab01.cpp' file to the screen, one page at a time. Use your
arrow keys to navigate.
- cat lab01.cpp
Types the contents of the 'lab01.cpp' file to the screen all at once.
Lets you change your password by prompting you for your old and new passwords.
Displays the entire path to your current directory.
THIS IS VERY USEFUL!!!
Accesses the UNIX manual which describes commands and even C keywords and
functions. If you want to learn something about the 'cat' command, simply
type man cat and the associated manual page will appear. If you do
not remember the command, you can search for a keyword. For example, if you
do not know the 'cp' command, search for 'copy' by using man -k copy
and that should bring up some associated pages.
If you are still not familiar with the Linux commands here in this lab,
it is strongly suggested you give yourself plenty of extra time to start
your first project. The learning curve for this environment can be significant
for some students.
Emacs is a linux text editor.
A sample emacs window
First, check out an online emacs reference (Emacs Cheatsheet or the
Wikipedia: List of Emacs commands). There are plenty of different
online resources than the ones provided and you are encouraged to find one that
is the best for you.
You can see that there are quite a few short-cut keys to
learn to really master emacs. There is no need to learn all of these except
to speed-up your code development. The more you learn, the more quickly you
will be able to copy/paste/alter your code. However, let's start with just
the basics, you can learn more of these.
Use the cheatsheet to learn a few more instructions, there are commands for
searching a file, indenting regions of text, changing the case of a word,
spell checking a document, and many other actions. Familiarizing yourself
with these commands will make your time in the Linux environment much more
- Opening an existing file is easy, at the command prompt, type:
This will open the 'lab01.cpp' file that you created in step 1.
- Editing a file is even easier, simply start typing
- Saving a file requires typing:
- Exiting the editor requires typing:
If you attempt to exit after altering a file but before saving it, you will
be prompted to save your work.