This tutorial discusses:
Students in this course must get an account on our CSA network, which consists of machines that run the UNIX operating system. Students will also need to get an NT account to use the machines in our teaching lab, although we will often log into the CSA network from those NT machines.
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 CSA network form a file hierarchy, something like:
File Hierarchy (or File System) ------------------------------- / <-- root directory (a slash) / \ home ... / \ ugrad course / \ ... ... / | \ ... ... username <-- your home directory
So that those that have accounts for this course (except CS majors) will have an account in the location:
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 it,
course directory, under it, a
and under that is where your
Since this path starts at the root (/), it is called an absolute (or full) path.
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:
% mkdir unixlab
This added another directory to our picture:
Bottom of File Hierarchy ------------------------ / | \ ... ... username <-- your home directory / unixlab <-- new directory
Go into that new directory by typing:
% cd unixlab
which will change your present location in the file hierarchy:
Bottom of File Hierarchy ------------------------ / | \ ... ... username <-- your home directory / unixlab
To make sure you are there, you can type:
to list the path of your present working directory (also called the current directory). It will be something like:% pwd
Let's look at a file. We've written a letter about what we're learning here today that you can send to your friends or family. The letter just needs your signature.
The letter is available to download as
To download this file into your
unixlab directory, do
the following in :
To make sure the file got saved correctly, go to your UNIX window and type the ls command to list the files in your current directory. See that it is there.
letter.txt that contain only text
(i.e., letters, digits and punctuation) are called text files.
One way to display a text file is to use the cat command.
You need to type
"cat" followed by the name of the file
you want to view, as in:
% cat letter.txt
It will display the contents of the file on the screen. However, if the file is too long to fit in the window, you'll only see the end of the file on the screen.
Since long files won't fit in the window, you can use less to view such files instead of cat. Do...
% less letter.txt
Less will allow you to page forward through the file using
<Space> bar and page backward using the
b key. In that sense, it is a bit more useful than a
similar utility called more.
When you are done looking at the file, type
q to quit
If you needed a printout of this code, you could use lpr to send a copy to the printer, as in:
% lpr letter.txt
DON'T PRINT THAT FILE NOW, AS WE DON'T NEED A BUNCH OF COPIES GOING TO THE PRINTER!!!
Since we need to sign the letter, we'll want to edit the file
letter.txt. To edit or even create a text file,
we can use the editor emacs.
Type the following at the UNIX prompt:
% emacs letter.txt &
&) at the end of commands that bring up their own window, since doing so gives us another prompt back right away where we can type more UNIX commands.
Once Emacs is up and has loaded the source file, we can go directly to
the end of the letter. To do so, type the command
<Esc> key and then press and release the
> (greater than) key).
Emacs should place you at the end of the file. Go ahead and sign it by typing in your name.
letter.txt in Emacs, you have really only edited
Emacs' copy of that file in memory. We'll have to save the edits back
to disk in order for the file
letter.txt to be actually
To convince yourself that we have not changed the version on disk, go back to the UNIX prompt and display the file:
% cat letter.txt
Now, to save the edits to file, choose Save Buffer from the Emacs Files menu.
letter.txt to see that the changes were saved.
Finally, when you are done with Emacs, choose Exit Emacs from the Emacs Files menu. Note that Emacs creates backups (that end in ~) for files you edit.
Suppose we want 2 different versions of our letter, one for friends
and one for family. We can create those based on our initial letter
Let's start out by making a copy of the letter, named
friends.txt, as the version for our friends.
To make a copy of a file, we can use the cp command:
% cp letter.txt friends.txt
letter.txt still exists since we just
We can use the original
letter.txt as our letter to family,
but we'd like that file to have a more appropriate name. To rename a
file, we can use the mv command:
% mv letter.txt family.txt
letter.txt is no longer there since we gave it a
Finally, mv can also be used to move files, so let's put our letters in their own subdirectory. First, make a subdirectory:
and move the files there:% mkdir letters
% mv friends.txt family.txt letters
Note that the last thing listed to the command is the destination, with all the source files lists before.
You can make sure the files got moved by listing the files in directory
% ls letters
On your own time, edit the letter to your friends, adding more detail.
How can you go into the
letters directory to edit
friends.txt or how can you edit it while staying in the
You've already seen how to run UNIX commands by just typing their names, possibly with arguments:
% ls letter
Many commands produce their output on the screen. Sometimes, however,
you'll want the output to go to a file. For example, let's store a
calendar for the current month in a file named
We can do so by using output redirection with the cal
% cal > calendar
This makes UNIX send the output to the file
Display the file:
% cat calendar
Using ">" followed by a file name is what tells UNIX to redirect
the commands output. Be careful, though, it may overwrite a file
calendar if one already exists.
Sometimes, we just want to append some output to the end of a file--we can do so with ">>". Let's append the date to our letter to our family:
% date >> letters/family.txt
Note that we had to use a path to tell it where
lettersin the current directory, then, look for the file
friends.txt. Paths that start from the current directory are called relative paths.
Remember, paths that start from the root (/) directory, like
/home/course are call absolute (or full) paths.
To see that the date was added, display the file:
% cat letters/family.txt
Sometimes, you don't want to just capture the output of one command, but rather, both the input to and output from a bunch of commands.
You can do so with the utility script. To start recording, use:
Then, just run some commands:
% pwd your-dir/unixlab % calc Enter equations. Blank line to terminate. 2+2 4
When you are done using commands you want recorded, type
The recorded input and output can be found in a file named
% cat typescript
Since you have limited disk space for this course, 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, 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 utility 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 files
% rm calendar letters/family.txt
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
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.