This tutorial discusses:
Students in this course must get an account on our department network, which consists of machines that run the UNIX operating system. You should get an account which is not the one that you would have used for your CS1100 lab sessions. If you do not have an account, contact Mr. S. Ravichandran in the Software lab for getting your accounts created.
Like other common operating systems (the Mac operating system or MS Windows) information on our UNIX systems is stored in files. Besides files, UNIX allows the creation of directories, in which other files and directories can be placed (a directory is the same as a folder in Mac-speak).
All the files and directories on the department network form a file hierarchy, something like:
File Hierarchy (or File System) ------------------------------- / <-- root directory (a slash) / \ home ... / | \ username <-- your home directory![]()
So that those that have accounts for this course will have an account in the location:
/home/username
This is called the path of your home directory,
the directory you are
placed in when you log in.
A path just represents the location of a file or directory in
the file hierarchy. For example, the first path above says that under the
root (/) directory, there is a home
directory, under that is where
your username
directory is.
Since this path starts at the root (/), it is called an absolute (or full) path.
First, let us move to the Desktop. The UNIX command cd
shall help
us in changing the directory.
You should use separate directories to store files associated with a tutorial, lab or particular homework. Make a directory for this UNIX material by typing:cd Desktop
mkdir cs2110
which will add another directory to our picture:
Bottom of File Hierarchy ------------------------ / | \ ... ... username <-- your home directory / | \ Desktop <-- the Desktop directory /cs2110 <-- new directory
Remember, paths that start from the root (/) directory, like
/home/username
are called absolute (or full) paths.
Now go to the directory by typing:
cd cs2110
which will change your present location in the file hierarchy:
Bottom of File Hierarchy ------------------------ / | \ ... ... username <-- your home directory / | \ Desktop <-- the Desktop directory / cs2110![]()
To make sure you are there, you can type:
and it will report your present working directory, which should end inpwd
cs2110
.
/
.
ls
by itself lists
files in the current directory. If you want to access files
other than in the current directory, you have to use either a
relative pathname or a full
(absolute) pathname.
When you log in, your current directory is your home directory.
If you are in a CS course, your home directory will be:
/home/username
This just means that your home directory is under the home directory, which is under the
root directory, i.e., /
.
This, of course, is an example of a full pathname.
An example path would be:
cs2110/lab_0/p1.c
which refers to a file called p1.c
in the
directory lab_0, under the directory
cs2110 under whatever your current
directory is. Note the use of the slash (/
)
to separate the pieces.
Typically you'd use a path to tell a command what file to operate on. For example,
ls -l cs2110/lab_0/p1.c
uses ls to list information
about the file p1.c
. Since, in this case,
p1.c
is not in your current directory. you must give a
path with the file name.
Any example full path is:
/usr/bin/man
which is the location of the man
command.
Any example relative path is:
cs2110/test1
which refers to a file in the subdirectory cs2110 under your current directory.
ls -l cs2110
where the command name is ls
and the arguments are "-l"
(a special kind of argument
called a flag) and "cs2110"
.
ls -l
in which case the flag -l
(minus ell, not one)
gives a longer set of information. Flags alter the default
behavior of commands. For example, the command ls, by default, just lists the names of
files. With the -l
flag, it gives you more
information about each file (or directory).
Flags normally start with a minus sign (-
) and
consist of a single letter, like l (i.e., ell) or a whole
word, as in -debug. For commands that take only single letter
flags, more than one flags can often, but not always, be
combined. For example, -l and -F could be typed as
-l<SPACE>-F
or -lF
. In
some cases, flags will start with a plus sign (+
)
instead. It is more typically for flags with pluses to be part of a
pair of flags, e.g., -a
to turn some feature on and
+a
to turn it off.
Because some flags turn features on/off, flags are sometimes called switches. In addition, they may be called options.
Types of files that are not text files are executables and word processor documents and are called binary files. When you try to view binary files with text file commands, they look like garbage. Again, a text file only differs from a binary file in that information is stored in it using the typical symbols that you'd see on a typewriter.
.c
(and sometimes in
.h
). These files cannot be run on the
computer. In order to get an executable,
which can be run, you must compile and link the
source code.
ls
to view files, these
executables might have an asterisk (*
) after their
names (though the *
is not part of the file's
name).
To run an executable, you just type its name.
Compiling and linking are actually separate procedures, though often you'll use one program to both compile and link. Technically, compiling puts all the source code into an intermediate form and linking links all the intermediate pieces and special libraries into a single executable.
Often we'll use the term compiling for both compiling and linking.
We'll compile and link our C programs by using the gcc compiler or by using the make utility.
Our debugger is gdb
.
&
) at
the end of a UNIX command, so that we get the prompt back right away.
If a program is not running in the background, we say it is in the foreground.
UNIX allows you to use the less than
(<
) character to make the program take input
from a file instead of the keyboard.
UNIX allows you to use the greater
than (>
) character to send a program's output
to a file instead of to the screen.
UNIX allows you to use the pipe symbol (|
) to
send a program's output into another program.
Using the up and down arrow keys, you can scroll to a previous command, edit it (or leave it as is) and then press <RETURN> to perform it again.
Since you have limited disk space, you may want to remove unnecessary files. If you do not know what command is used to remove files, you can look it up on our system.
The UNIX Manual Pages let you look up information on UNIX commands (also C and C++ library functions), including how to use them. To view Manual Pages, we use the man command.
Let's first look for a command to remove files:
man -k 'remove files' ... rm rm (1) - remove files ...
The -k tells it to look up the key phrase 'remove files', which has to be in quotes because of the space between the 2 words. (In practice, you might have to try different key phrases or words before you find what you are looking for.)
It tells us that we want the rm command in Section 1 of the Manual Pages. Now, we can look at the documentation for rm by using:
man rm
Man displays the info just like the program less.
Reading the documentation given by man, you can see that all we
need to type to remove a file is "rm"
followed by the names of
files to be removed.
Remove the executables exec1
and exec2
by using:
rm exec1 exec2
Finally, since we are done with the files in the directory for now, let's go back up to our home directory. Since you are one directory below your home directory, you can just type:
cd ..
to go up one level. Dot dot (..
) is a way to refer to the
directory above.
We could also use:
since tilde (~) is shorthand for your home directory at the UNIX prompt (and in some applications, like Emacs). This works even if our home directory is not right above where we are.cd ~
Finally, we could also just use:
by itself, which also goes to your home directory.cd
You can confirm where you are by using pwd.