Introduction To The UNIX Operating System On IT Systems: Rice University, 2000 All Rights Reserved
Introduction To The UNIX Operating System On IT Systems: Rice University, 2000 All Rights Reserved
This document is intended to introduce you to the UNIX operating system. It will provide you with a
basic understanding of the UNIX operating system, its file and directory structure, basic UNIX com-
mands, and how to get on-line help. The information in this document is primarily designed for (but
not limited to) Sun workstations configured and maintained by Information Technology, such as
those used in the Owlnet network.
If you are using a workstation that is configured to run exclusively as an X display server, you may
wish to acquire the document, UNIX 2, Introduction to X Windows.
Introduction.............................................................................................. 4
Printing Files...........................................................................................................18
Directories...............................................................................................19
About UNIX Directories.........................................................................................19
Processes.................................................................................................27
Viewing Your Processes ..........................................................................................27
Troubleshooting......................................................................................29
Logging In...............................................................................................................29
Additional Resources..............................................................................30
Font Conventions
This document uses the true type font setting to represent any feedback echoed on the computer
screen after you type a command. When you are told to type a command, this command will be
printed in Times Roman font.
this is the true type font.
What is UNIX?
UNIX is a powerful computer operating system originally developed at AT&T Bell Laboratories. It
is very popular among the scientific, engineering, and academic communities due to its multi-user
and multi-tasking environment, flexibility and portability, electronic mail and networking capabili-
ties, and the numerous programming, text processing and scientific utilities available. It has also
gained widespread acceptance in government and business. Over the years, two major forms (with
several vendor’s variants of each) of UNIX have evolved: AT&T UNIX System V and the Univer-
sity of California at Berkeley’s Berkeley Software Distribution (BSD). This document will be based on
the SunOS 4.1.3_U1, Sun’s combination of BSD UNIX (BSD versions 4.2 and 4.3) and System V
because it is the primary version of UNIX available at Rice. Also available are Solaris, a System V-
based version, and IRIX, used by Silicon Graphics machines.
UNIX Layers
When you use UNIX, several layers of interaction are occurring between the computer hardware and
you. The first layer is the kernel, which runs on the actual machine hardware and manages all interac-
tion with the hardware. All applications and commands in UNIX interact with the kernel, rather than
the hardware directly, and they make up the second layer. On top of the applications and commands is
the command-interpreter program, the shell, which manages the interaction between you, your applica-
tions, and the available UNIX commands. Most UNIX commands are separate programs, distinct from
the kernel. A final layer, which may or may not be present on your system, is a windowing system such
as X. The windowing system usually interacts with the shell, but it can also interact directly with appli-
cations. The final “layer” is you, the user. You will interact with the entire operating system through just
the shell, or through a combination of the shell and the window system. The figure below gives a visual
representation of the layers of UNIX.
You
Windowing System
Shell
Applications Commands
Kernel
Hardware
TTY Terminal
If you are using a TTY terminal (a TTY is line-at-a-time oriented as opposed to page oriented) and
the screen is blank, you only need to press RETURN and a login prompt should appear on the
screen.
Workstation
If the display features a box in the center of the screen with text similar to that in the figure below,
then you are using a workstation that is configured to run a windowing system called the X Win-
dow system. These machines are called X terminals. (For more information on the X Window sys-
tem, see the Information Technology document, UNIX 2, Introduction to the X Window System.).
If the screen is entirely black, then a screen-saving program is running automatically to protect the
monitor from damage. Moving the mouse or pressing the RETURN key should “wake up” the dis-
play. (If you see the words “This screen has been locked...” then someone else is using the worksta-
tion, but they are temporarily away from their seat. Look for an unoccupied machine.) Move the
mouse until the cursor (a black ‘X’) is on top of the white box.
Logging Out
X Terminals
To log out of the X Window system from an X terminal, move the cursor into the console window
(it is labeled “console”), type the command exit, and press RETURN. If you try to use the logout
command in the console window, you will receive the message, “Not in login shell.”
UNIX Commands
Case Sensitivity
UNIX is a case sensitive operating system. It treats lower-case characters differently than upper-case
characters. For example, the files readme, Readme, and README would be treated as three different
files. Most command names and files are entirely in lower-case. Therefore, you should generally plan to
type in lower-case for most commands, command line arguments, and option letters.
holding down the key labeled CONTROL and pressing the “c” key (in the same manner that you
hold down the SHIFT key and press the “c” key to generate a capital C). The notation for control
characters is usually ^C or CTRL-C. Some standard special keys and control characters are sum-
marized below.
BACKSPACE This key is sometimes used as the rubout key instead of the
DELETE key. Otherwise, it is mapped as a backspace key, which
generates a ^H on the display.
CTRL-U ^U erases the entire command line. It is also called the line kill
character.
Editing Tools
Command Purpose
pico simple text editor
vi screen oriented (visual) display editor
diff show differences between the contents of files
grep search a file for a pattern
sort sort and collate lines of a file (only works on one file at a time)
wc count lines, words, and characters in a file
look look up specified words in the system dictionary
awk pattern scanning and processing language
gnuemacs advanced text editor
Creating Files
Many files are created using a text editor. A text editor is a program that allows you to enter and save
text. You can also use a text editor to manipulate saved text through corrections, deletions, or insertions.
The main text editors on Information Technology managed networks are vi, GNU Emacs, Pico, and
aXe. (Note: vi is included with every UNIX system, but GNU Emacs is commonly installed separately
by system managers. aXe is only available if you are using the X Window system.) You should learn
how to use at least one of these tools. Information Technology has tutorial documents on each of these
editors. Please see the section, Additional Resources, for information on the tutorials.
You can create a file without a text editor by using the cat command (short for concatenate) and the “>”
(redirect output) symbol. To create a file using the cat command, type:
cat > new-filename
where new-filename is the name you wish to give the file. The command cat generally reads in a
file and displays it to standard output. When there is no filename directly following the command,
cat treats standard input as a file. The “>” symbol will redirect the output from cat into the new file-
name you specify. cat will keep reading and writing each line you type until it encounters an end-
of-file character. By typing CTRL-D on a line by itself, you generate an end-of-file character. It will
stop when it sees this character. Try it, using this example as a guide:
cat > practice
When you reach the end of each line, press the RETURN key. You can only correct mistakes on the
line you are currently typing. Use the DELETE key to move the cursor back to the mistake and then
retype the rest of the line correctly. When you have completed the last line, press RETURN and
type CTRL-D.
Displaying Files
Now that you have created a file, you can display it one of several ways. You could use the cat
command. Just type cat followed by the name of the file that you want to see.
cat practice
Sometimes the files you want to view are very long. When using the cat command, the text will
scroll by very quickly. You can control the flow of text by using CTRL-S and CTRL-Q. CTRL-S
stops the flow of text and CTRL-Q restarts it. If you use CTRL-S, stopping the flow of text, and so
on, you must remember to type CTRL-Q or the computer will not display any output, including
anything that you type.
more is a program that displays only one screen of information at a time; it waits for you to tell it to
continue. Type more followed by a filename.
more practice
The computer will display one screen of text and then wait for you to press the space bar before it
displays the next page of text, until you reach the end of the file. Pressing the “?” character will
show help for more. A utility of greater power called less is available on many systems; it allows
reverse scrolling of files and other enhancements. It is invoked the same way as more.
Listing Files
The ls command will list the files in the current directory that do not begin with a period.
Below is a list of options you can tack on to ls:
ls -a lists all the contents of the current directory, including files with initial
periods, which are not usually listed.
ls -l lists the contents of the current directory in long format, including file
permissions, size, and date information.
ls -s lists contents and file sizes in kilobytes of the current directory.
If you have many files, your directory list might be longer than one screen. You can use the programs
more or most with the “|” (vertical bar or pipe) symbol to pipe the directory list generated as output by
the ls command into the more program. more or less will display the output from ls one page at a time.
ls | more
Copying Files
To make a copy of a file, use the cp (copy) command.
cp filename newfilename
where filename is the file you wish to copy and newfilename is the file you are creating.
cp practice sample (make a copy of “practice” called “sample”)
ls
practice sample
The example created a new file called sample that has the same contents as practice. If sample already
exists, the cp command will overwrite the previous contents. New accounts are often set up so that cp
will prompt for confirmation before it overwrites an existing file. If your account is not set up in this
manner, use the -i option (cp -i) to get the confirmation prompt, like so:
cp -i practice sample
Important: rm can be
very dangerous. Once
a file has been removed
you cannot get it back, Renaming Files
except, possibly, from
system backups (which To rename one of your files, use the mv (move) command.
may or may not
mv oldfilename newfilename
contain the file). It may
take the system where oldfilename is the original filename and newfilename is the new filename. For instance, to rename
administrators several sample as workfile type:
days to recover your mv sample workfile
deleted file, so use a
great deal of caution ls
when deleting files. practice workfile
New accounts are often
This moves the contents of sample into the new file workfile. (Note: Moving a file into an existing file
set up so that rm will
overwrites the data in the existing file.) New accounts are often set up so that mv will prompt for confir-
prompt for mation before doing this. If your account is not set up in this manner, use the -i option (mv -i) to get the
confirmation. If your confirmation prompt.
account is not set up in
this manner, use the -i
option to get the
confirmation prompt. Deleting Files
To delete files, use the rm (remove) command. For instance, to delete workfile, type:
rm workfile
ls
practice
lprloc
lprloc lists all of the printers that your system knows about, by name, along with their type and location.
To get some status information on the printers, use the command lpstat -p. Printer accounting informa-
tion is available by running the command pacinfo.
Directories
bin lib
file1 file2
At the top of the tree is the root directory. Its directory name is simply / (a slash character). Below the
root directory is a set of major subdirectories that usually include bin, dev, etc, lib, pub, tmp, and usr.
For example, the /bin directory is a subdirectory, or “child,” of / (the root directory). The root directory,
in this case, is also the parent directory of the bin directory. Each path leading down, away from the
root, ends in a file or directory. Other paths can branch out from directories, but not from files.
Many directories on a UNIX system have traditional names and traditional contents. For example,
directories named bin contain binary files, which are the executable command and application files. A
lib directory contains library files, which are often collections of routines that can be included in pro-
grams by a compiler. dev contains device files, which are the software components of terminals, print-
ers, disks, etc. tmp directories are for temporary storage, such as when a program creates a file for
something and then deletes it when it is done. The etc directory is used for miscellaneous administrative
files and commands. pub is for public files that anyone can use, and usr has traditionally been reserved
for user directories, but on large systems it usually contains other bin, tmp, and lib directories.
Your home directory is the directory that you start out from when you first login. It is the top level
directory of your account. Your home directory name is almost always the same as your userid.
Every directory and file on the system has a path by which it is accessed, starting from the root
directory. The path to the directory is called its pathname. You can refer to any point in the direc-
tory hierarchy in two different ways: using its full (or absolute) pathname or its relative pathname.
The full pathname traces the absolute position of a file or directory back to the root directory, using
slashes (/) to connect every point in the path. For example, in the figure above, the full pathname of
file2 would be /usr/bin/file2. Relative pathnames begin with the current directory (also called the
working directory, the one you are in). If /usr were your current directory, then the relative path-
name for file2 would be bin/file2.
If you are using C shell, TC shell, or the Bourne-Again shell, UNIX provides some abbreviations
for a few special directories. The character “~” (tilde) refers to your home directory. The home
directory of any user (including you, if you want) can be abbreviated from /parent-directories/use-
rid to ~userid. Likewise, you can abbreviate /parent-directories/youruserid/file to ~/file. The cur-
rent directory has the abbreviation . (period). The parent of the current directory uses .. (two
consecutive periods) as its abbreviation.
Displaying Directories
When you initially log in, the UNIX system places you in your home directory. The pwd command
will display the full pathname of the current directory you are in.
pwd
/home/userid
By typing the ls -a command, you can see every file and directory in the current directory, regard-
less of whether it is your home directory. To display the contents of your home directory when it is
not your current directory, enter the ls command followed by the full pathname of your home direc-
tory.
ls /home/userid
If you are using a shell other than the Bourne shell, instead of typing the full pathname for your
directory, you can also use the tilde symbol with the ls command to display the contents of your
home directory.
ls ~
To help you distinguish between files and directories in a listing, the ls command has a
-F option, which appends a distinguishing mark to the entry name showing the kind of data it con-
tains: no mark for regular files; “/” for directories; “@” for links; “*” for executable programs:
ls -F ~
Changing Directories
To change your current directory to another directory in the directory tree, use the cd command.
For example, to move from your home directory to your projects directory, type:
Renaming Directories
You can rename an existing directory with the mv command:
mv oldDirectory newDirectory
The new directory name must not exist before you use the command. The new directory need not
be in the current directory. You can move a directory anywhere within a file system.
Removing Directories
To remove a directory, first be sure that you are in the parent of that directory. Then use the com-
mand rmdir along with the directory’s name. You cannot remove a directory with rmdir unless all
the files and subdirectories contained in it have been erased. This prevents you from accidentally
erasing important subdirectories. You could erase all the files in a directory by first going to that
directory (use cd) and then using rm to remove all the files in that directory. The quickest way to
remove a directory and all of its files and subdirectories (and their contents) is to use the rm -r (for
recursive) command along with the directory’s name. For example, to empty and remove your
projects directory, move to that directory’s parent, then type:
rm -r projects (remove the directory and its contents)
For each file and directory, the read, write, and execute permissions may be set separately for each of
the following classes of users:
User ( u ) The user who owns the file or directory.
Group ( g ) Several users purposely lumped together so that they can share access to
each other's files.
Others ( o ) The remainder of the authorized users of the system.
The primary command that displays information about files and directories is ls. The -l option will dis-
play the information in a long format. You can get information about a single UNIX file by using ls -l
filename.
Each file or subdirectory entry in a directory listing obtained with the -l option consists of seven fields:
permission mode, link count, owner name, group name, file size in bytes, time of last modification, and
the filename (the group name appears only if the “g” flag is also specified, as in ls -lg).
The first 10 characters make up the mode field. If the first character is a “d” then the item listed is a
directory; if it is a “-” then the item is a file; if it is an “l” then it is a link to another file. Characters 2
through 4 refer to the owner’s permissions, characters 5 through 7 to the group’s permissions (groups
are defined by the system administrator), and the last three to the general public’s permissions. (You can
type id to verify your userid and group membership.) If a particular permission is set, the appropriate
letter appears in the corresponding position; otherwise, a dash indicates that the permission is not given.
The second field in the output from ls -l is the number of links to the file. In most cases it is one, but
other users may make links to your files, thus increasing the link count. A special warning to people
using links to other people’s files: your “copies” of their files can be counted against them by the file
quota system available on certain UNIX variants. That is why making links other than symbolic links to
other people’s files is strongly discouraged. The third field gives the userid of the owner of the file. The
group name follows in the fourth field (if the -g option is used in conjunction with -l). The next two
fields give the size of the file (in bytes) and the date and time at which the file was last modified. The
last field gives the name of the file.
ls -l myfile
-rw-r--r-- 1 owner 588 Jul 15 14:39 myfile
A file’s owner can change any or all of the permissions with the chmod (change mode) command. The
chmod command allows you to dictate the type of access permission that you want each file to have. In
the previous example the current permissions for myfile are read for everybody, write for the owner, and
execute by no one.
The arguments supplied to chmod are a symbolic specification of the changes required, followed by
one or more filenames. The specification consists of whose permissions are to be changed: u for user
(owner), g for group, o for others, or some combination thereof (a (all) has the same effect as ugo), how
they are to be changed (+ adds a permission, - removes a permission, and = sets the specified permis-
sions, removing the other ones) and which permission to add or remove (r for read, w for write, and x
for execute). For example, to remove all the permissions from myfile:
chmod a-rwx myfile
ls -l myfile
---------- 1 owner 588 Jul 15 14:41 myfile
(Note: chmod a= myfile achieves the same effect.)
--- 000 0
--x 001 1
-w- 010 2
-wx 011 3
r-- 100 4
r-x 101 5
rw- 110 6
rwx 111 7
Thus, if the setting you want is rw-r--r--, determine the octal number with the following
method:
symbolic r w - r - - r - -
\ / \ / \ /
binary 110 100 100
\ / \ / \ /
octal 6 4 4
This shows that the octal equivalent of rw-r--r-- is 644. The following example illustrates that the
permissions for myfile have been reset to the values with which we began.
chmod 644 myfile
ls -l myfile
-rw-r--r-- 1 owner 588 Jul 15 14:44 myfile
To change the permissions back to read only, you can execute chmod as follows:
chmod 444 myfile
ls -l myfile
-r--r--r-- 1 owner 588 Jul 15 14:45 myfile
As with files, directories may also have permissions assigned. When listing directories, you may use the
-d option to keep from descending into the directories you list. Otherwise, the contents of the directories
will be displayed as well as their names. Below is an example of permissions assigned to a directory:
ls -lgd home
drwxrwxr-x 1 owner caam223 588 Jul 15 9:45 home
The directory and the files and directories under it may be read and executed by anyone, but written to
only by the owner and users in the caam223 group. Assuming you are the owner of this directory, you
may decide to change the permission to allow only yourself and the caam223 group to read and execute
files in the home directory. You would set the permissions accordingly:
chmod o-rx home
ls -lgd home
drwxrwx--- 1 owner caam223 588Jul 15 9:46 home
You may decide that only you should be able to alter the contents of the directory. You must remove the
write permission for the group.
chmod 750 home
ls -lgd home
drwxr-x--- 1 owner caam223 588 Jul 15 9:48 home
An alternative to the previous command is chmod g-w.
When you create a file the system gives it a default set of permissions. These are controlled by the sys-
tem administrator and will vary from installation to installation. If you would like to change the default
which is in effect for you, choose your own with the umask command. Note that the permission
specified by the umask setting will be applied to the file, unlike that specified in the chmod com-
mand, which normally only adds or deletes (few people use the = operator to chmod).
First, issue the command without arguments to cause the current settings to be echoed as an octal
number:
umask
022
If you convert these digits to binary, you will obtain a bit pattern of 1’s and 0’s. A 1 indicates that
the corresponding permission is to be turned off, a 0, that it is to be turned on. (Notice that the bit
patterns for chmod and umask are reversed.) Hence, the mask output above is 000010010, which
produces a permission setting of rwr-r- (i.e., write permission is turned off for group and other).
Newly created files always have the execution bit turned off.
Suppose you decide that the default setting you prefer is rwxr-x---. This corresponds to the
masking bit pattern 000010111, so the required mask is 026:
umask 26
Now, if you create a new file during this session, the permissions assigned to the file will be the
ones allowed by the mask value.
Wildcard Characters
Using wildcard characters that allow you to copy, list, move, remove, etc. items with similar names
is a great help in manipulating files and directories.
1. The symbol ? will match any single character in that position in the file name.
2. The symbol * will match zero or more characters in the name.
3. Characters enclosed in brackets [and] will match any one of the given characters in the given
position in the name. A consecutive sequence of characters can be designated by [char char].
Examples of each follow:
1. ?ab2 would match a name that starts with any single character and ends with ab2. ?ab? would
match all names that begin and end with any character and have ab in between.
2. ab* would match all names that start with ab, including ab itself.
a*b would match all names that start with a and end with b, including ab.
3. s[aqz] would match sa, sq, and sz.
s[2-7] would match s2, s3, s4, s5, s6 and s7.
These wildcard symbols help in dealing with groups of files, but you should remember that the
instruction:
rm *
would erase all files in your current directory (although by default, you would be prompted to okay
each deletion). The wildcard * should be used carefully.
Processes
Every command or program running under UNIX is called a process. A sequence of related processes is
called a job. Your applications and even your shell itself are processes. The windowing system is also a
process, or a collection of processes. The UNIX kernel manages the processes on the system, usually
without distinguishing among them. UNIX is a multi-tasking system—it allows you to continue to work
in the foreground while running one or more jobs in the background. It also runs the processes of many
users simultaneously. You could even log off and come back later if the background jobs do not require
interaction with you.
[1] 5432
This particular response means that you have one job running in the background (and its job num-
ber is 1), and its process identification number (PID) is 5432. You will need to know the PID if you
want to abort the job. This is known as killing a job. To kill the job in the above example, you
would type:
kill 5432
You could also use
kill %1
or, if there’s only one job running called “jobname,”
kill %jobname
In the C shell, the job number can be used to control which jobs run in background or foreground.
The job number is used when switching a job that is processing in the foreground to the back-
ground, and one that is processing in the background to the foreground. To do the former, first press
CTRL-Z to suspend the job. Then type:
bg %jobNumber
To switch the job to the foreground, simply type:
fg %jobNumber
If you have forgotten the job number, type the command jobs to see a list of the jobs that are run-
ning in the background at the moment.
Note: The rules imposed by system administrators about where and how to run background jobs
varies from network to network and changes over time. It is important to stay current with the back-
ground job policy of your network.
For example, if you wanted to run a long non-interactive job, and you didn’t have to have the results of
this job right away, you should run it in the background and set a high nice value. Using the C shell, you
would type:
nice -19 jobname &
Remote Login
Sometimes, while you are logged into one workstation, you will find that you would like to be logged in
to another workstation, file server, or other UNIX system. The command rlogin allows you to do so
provided that you have an account on the other system. Type:
rlogin newSystem
You may then have to supply your password. You should also get the messages about logging in that are
used on newSystem. If your userid is different on newSystem you will have to use the form:
rlogin newSystem -l userid
Troubleshooting
Logging In
Problem: The system prints “login incorrect.”
Check to make sure that you are typing your userid and password exactly as
they appear on your information sheet (make sure you’re using the proper
case for all characters). If you still cannot log in, go to the Consulting Center
in 103 Mudd Lab and ask for help.
Problem: The computer types everything in upper-case with slashes in it.
SAMPLE OF/WHAT/PROBLEM LOOKS LIKE
Your userid was entered in uppercase letters, so the system thinks that your
terminal can only understand uppercase letters. You need to type logout and
then login again making sure that you enter your userid in lowercase letters
only. (If the screen displays uppercase as you type your userid, you may
have to turn the CAPS key “off” by pressing it once and try logging in once
more.)
Logging Out
If you experience difficulty logging out, check to see if you are having one of the following problems.
Do not turn off the workstation or terminal. This will not disconnect you from UNIX.
Problem: The system replies “There are stopped jobs” when you try to log out.
Type jobs to see what they are, then type kill %% to terminate all
stopped background jobs. After that, try logging out again.
Problem: The system replies, “Not in login shell.”
First, type exit, press RETURN, and then type logout at the prompt, if
necessary.
Problem: Nothing seems to work and you can’t log out.
You might be stuck in a program or shell other than the login shell.
Press RETURN to clear any previous commands; then try typing
CTRL-C, q, :q, :q!, exit, CTRL-D, CTRL-Q, or CTRL-Z to get back
into the login shell. Then try logout again. The following sequence
might also work if your terminal is NOT connected directly to your real
host machine. Press RETURN, then type “~.” followed by RETURN.
If you need additional help, go to the Consulting Center in 103 Mudd Lab during consulting hours
or call 713-527-4983. You can also send electronic mail to the Consulting Center by using the
address: [email protected], or better yet, use the web: http://problem.rice.edu/
Additional Resources
You now have enough information to get started using UNIX. To learn more about UNIX and spe-
cific applications, you can refer to a number of resources. You can use the man command to get on-
line help. This is the quickest and easiest way to get help during a UNIX session. Virtually every
UNIX operation and command has an entry. UNIX documentation written by the vendor for their
workstations is available in the document racks of Information Technology maintained labs.
Information Technology has a number of tutorial and reference documents available free of charge
outside the Consulting Center in 103 Mudd Lab. Below are some of the UNIX-related documents.
• Introduction to the X Window System
• Customizing the X Window System
• Introduction to the vi Editor
• Introduction to the aXe Editor
• Introduction to GNU Emacs
• Compilers, Make, and Debuggers
Fondren Library has a full set of UNIX reference documentation as well as a large checkout collec-
tion of books and manuals on UNIX and other related topics, including some very good introduc-
tory books.
The Consulting Center in 103 Mudd Lab has consultants available to troubleshoot problems and to
provide general assistance. A schedule of hours is posted outside the door of Room 103. You may
also send electronic mail to the userid [email protected] with questions about UNIX, or use the
WWW Problem Interface with Netscape, located at the URL
http://problem.rice.edu/
The Rice Campus Store has introductory and advanced books on UNIX, as does any bookstore with a
computer books section. Your professor may also have some course notes available at the Rice Campus
Store.
If you would prefer a hands-on tutorial introduction to UNIX, Information Technology offers free non-
credit, one to three hour sessions on various computing topics, including an introduction to UNIX.
Short Course calendars are available outside 103 Mudd Lab. For more information, call 713-348-4983
or on RiceInfo (URL: http://www.rice.edu).