UNIX Lab Guide

NAC 7/105

In this document:

How to Log In

How to Read and Send E-Mail

Useful Unix resources/links

Beginning Unix: Summary

Basic file operations

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.

Directories

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.

Command line editing

^X, also written Ctrl-X, means type the letter X while holding down the Control key, and is pronounced "control-x."

File editing

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,

To use vi, type vi filename. The contents of the file are displayed. You can now:

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 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.

Compiling and running programs


Sam Fenster and George Wolberg, Jan. 26, 1999