UNIX Lab Guide
NAC 7/105
In this document:
You can log in to your Unix account from a PC lab using Telnet (Start ->
Programs ->
Accessories -> Telnet). In Telnet, connect to a random Unix machine (in
NAC 7/105) with Connect -> Remote System; enter host name csultra.
If you are connected to the internet from home, and have Telnet, use the host
name csultra.engr.ccny.cuny.edu.
Your login name consists of the first four letters of
your last name followed by the last four digits of your social security number.
If you last name consists of less than four letters, then take the remaining
letters from your first name (as read from a roster).
For example, the login name for David Smith (123-45-6789) is smit6789.
The login name for David Ho (987-65-4321) is hoda4321.
Your initial password is your social security number (with no dashes
or blanks).
As soon as you log in for the first time, change your password
using the passwd command. Use a password with letters and numbers
that is not based on a common word or name.
Your UNIX account will not be available until after the first week
of class. All registered CSc students automatically get a UNIX account.
- List your files: ls -sF
- Display a command's entry in the Unix manual: man command-name
- Rename (moving) a file: mv old-file new-file
- Copy a file: cp old-file new-file
- Remove a file: cp old-file new-file
- Display the working directory: pwd
- Change the working directory: cd new-directory
- Make a new directory: mkdir directory
- Remove an empty directory: rmdir directory
- To create or edit the contents of a file: vi file
or emacs.
When you log in, the commands you type will look for and create files
in your home directory. Each user has a different home directory,
but yours is the same every time you log in, and the files you created
last time will still be there now.
To see what files (and possibly directories) are in your directory, type
ls
which is the (typically short) Unix abbreviation for "list files".
ls will not show you names starting with a period (.)
unless you give it the "all names" option (also called a flag
or switch), as follows:
ls -a
To find out the size of each file in bytes, use the -s flag.
To have ls distinguish filenames from directory names by
following the latter with a slash, use the -F flag.
Flags can be combined or separate, as in ls -aF or ls -a
-F. Note that in Unix, uppercase and lowercase letters are not
interchangeable.
To list only certain files instead of all the files in a directory,
type their names, separated by spaces, after the flags (if any).
Typing an asterisk (*) in a filename is equivalent to typing the names
of all files which match the name and have any characters
(or none) in place of the asterisk. Thus, you can list all files
ending in ".c" with the command ls *.c. Such wildcard
expansion works for all commands.
To find out all about the ls command, you can read its
``page'' in the Unix ``manual'' (its man page) by typing
man ls
The explanation will be displayed one screen at a time, with the word
More appearing at the bottom. To keep reading, hit the space
bar. To quit, hit the `q' key. There are man pages for every Unix
command. Just ignore what you don't understand yet in the text.
To give a file a different name, use the mv command, short for
"move":
mv old-name new-name
To make a copy of a file, use cp:
cp old-file new-file
If a filename has spaces, returns, or certain punctuation characters,
it may be enclosed in apostrophes ('single quotes'), or each such
character may be preceded by a backslash (\). A filename may not
contain a slash (/), because that is used to specify the directory a
file is in.
To delete a file, use rm, for "remove." In Unix, one a file is
deleted, there is no way to recover it, so be careful! If you use the
-i option, rm will ask you to type "yes" or "no" before deleting
a file. The same option is available to prevent a new filename created by
cp or rm from accidentally overwriting an existing file.
When you type a command, there is always a current working
directory. When you log in, this is your home directory, so
commands look for and create files there. But you can change the
current directory with the cd ("change directory") command, and
find out what it is with the pwd ("print working directory")
command. In the below example, you don't type "$" -- the computer writes it
to prompt you to start typing.
$ pwd
/home/cslab/cslabs4.2/other/wolb9876
$ cd /home/cslab
$ pwd
/home/cslab
$ cd /home/cslab/cslabs4.2/other/wolb9876/my-subdir
$ pwd
/home/cslab/cslabs4.2/other/wolb9876/my-subdir
$ cd /
$ pwd
/
$ cd
$ pwd
/home/cslab/cslabs4.2/other/wolb9876
As you can see, cd with no arguments returns you to
your home directory.
Directories have names just like files, and usually exist within other
directories. The only exception is the outermost directory on the
machine, the root directory. The cd commands above
specified directories using complete pathnames. A complete
pathname starts with / (the root) and is followed by the names of
successive subdirectories separated by slashes, ending with the
desired file or directory name. Thus, in /a/b/c, a is a
subdirectory of the root directory, b is a subdirectory of
a, and c is a file or directory in b.
If it does not start with a slash, a pathname is relative to
the current working directory. Thus, for a/b, a is a
subdirectory of the current working directory and b is a
file or directory in a.
The special name ".." refers to the directory containing it.
Thus, .. is the parent of the current directory, and
/home/cslab/mary/../joe/foo/../bar is the same as
/home/cslab/joe/bar. Also, . is the current directory,
so ./x is the same as x. ls -a shows that
. and .. exist in every directory.
$ pwd
/home/cslab/cslabs4.2/other/wolb9876
$ cd ..
$ pwd
/home/cslab/cslabs4.2/other
$ cd wolb9876/my-subdir
$ pwd
/home/cslab/cslabs4.2/other/wolb9876/my-subdir
You can create a directory with the mkdir ("make directory")
command. If it is empty, you can remove it with the rmdir command.
If an argument to ls is the name of a directory rather than of
a file, the directory's contents, rather than its name, will be
printed. The -d flag inhibits this behavior.
^X, also written Ctrl-X, means type the letter X while holding down the
Control key, and is pronounced "control-x."
- If you type an incorrect character, you can delete it by hitting the
Back-Space or Delete key (see which works for you).
- You can delete the entire line you typed by typing a ^U character.
- You can abort a command by typing ^C.
To create or edit a file, you have a choice of three text editing
programs, pico, vi and emacs. Vi and Pico are meant to
be terminated each
time you finish editing a file; Emacs starts up slowly, and is meant
to be kept running between edits.
Pico is the simple text editor used by the mail program Pine. To
use it,
- Connect (cd) to the directory you want your file in.
- Type pico filename.
- The available keystroke commands, including
those to display help screens, will be displayed at the bottom of the screen.
To use vi, type vi filename. The contents of the
file are displayed. You can now:
- Move the cursor left, right, up and down with the four fingers of
your right hand, by typing h, l, k or j.
$ goes to the end of a line.
- Scroll forward or backward through a file a screenful at a time by typing
^F or ^B.
- Insert text by typing i, typing the text, and then hitting
the Esc key when you are done. To append text to the end of a line,
use a.
- Delete a character with x, or a line with dd.
- Join two lines with J.
- Search for text with / text Return. Repeat the
search with / Return.
- Type :w to save.
- Type :q to quit.
- Type :q! to quit, leaving work unsaved and the file unchanged.
Further details are in the man page.
To use Emacs instead of Vi, type emacs. In Emacs' notation, C-x
means ^X. M-x, pronounced "meta-x," means typing Esc followed by `x'.
- You can then get a tutorial by typing ^H then `t'
(also written "C-h t").
- You can then edit a file by typing C-x f, then the filename
(hitting space or tab completes incompletely typed filenames).
- Move the cursor backward, forward, to the previous line or next line with
C-b, C-f, C-p or C-n. M-b and M-f
move forward or backward by a word. C-a and C-e go to the
beginning or end of a line. M-< and M-> go to the start
or end of the file.
- Typing always inserts what is typed.
- Delete forward with C-d and backward with Delete. Delete an
entire line with C-k.
- Scroll up or down a screen with C-v or M-v.
- Search for text with C-s. Terminate the search with Esc. Repeat
it by typing C-s again.
- Get out of any command with C-g.
- C-x s saves.
- C-x C-c exits Emacs.
You can suspend Emacs (or any other Unix command) with ^Z, and get a
shell prompt. After you are finished typing shell commands, you may
resume editing where you left off by putting Emacs in the foreground
again with the shell command fg.
- A file containing C++ code ends in ".C". A
header file to be #included ends in ".h". Use your favorite text editor to
create whatever such files make up your program.
- You must compile each C or C++ file, creating a file of
object code, with the same name but ending in ".o", containing
signals that your computer's electronics can understand.
Two compilers are available, CC and g++. Both compile using the
-c option:
CC -c file.C
g++ -c file.C
- One or more object files must be linked to create an
executable program file. The -o name option
determines its name:
CC -o prog-name file1.o file2.o
...
- The program can now be run as many times as you like by typing its
filename, with its directory specified explicitly:
./prog-name arguments...
- In fact, almost all of the standard Unix commands are just program files!
Many of them are in the directory /bin. You don't need to type the
name of the directory they're in explicitly, because a standard series of
directories is searched when you type a command name without an explicit
directory.
- If your program is in just one .C file, you don't need to compile
and link in two separate steps. You can just say:
CC -o prog-name file.C
- C program files, by contrast with C++, end in ".c" (lowercase).
There are also two compilers for C: cc and gcc. They
are invoked the same way as the C++ compilers.
Sam Fenster and George Wolberg, Jan. 26, 1999