Beruflich Dokumente
Kultur Dokumente
UNIX
Version 1.0
August 1993
Acknowledgments
Authors: User Services staff, Scientific Computing Division of the National Center
for Atmospheric Research
Technical writers/editors: Nancy Dawson and Sara Van Dyke
Documentation production: Valeri Hart
Most of the material in this manual first appeared in other SCD documentation.
Thanks to authors Brian Bevirt and Peter Morreale for material from the NCAR
UNICOS Primer, to Nancy Dawson, Jeff Kuehn, Peter Morreale, and Tom Parker
for material from CMS-UNIX Conversion Guide and SCD UNIX Basic Usage Guide,
and to Adrianne Middleton-Link for material from “UNIX Mail Hints.” Thanks
also to the technical reviewers of this document: Sally Haerer, Peter Morreale, Tom
Parker, Susan Smith, and Greg Woods.
UserDoc categories
Published by: National Center for Atmospheric Research, Scientific Computing Division, P.O. Box 3000,
Boulder, CO 80307-3000. The National Center for Atmospheric Research is operated by the University
Corporation for Atmospheric Research and is sponsored by the National Science Foundation. Any opinions,
findings, and conclusions or recommendations expressed in this publication are those of the authors and do
not necessarily reflect the views of the National Science Foundation.
Trademarks: All brand and product names are trademarks or registered trademarks of their respective
holders. Reference to a company or product name does not imply approval or recommendation of that
company or product to the exclusion of others.
Printed on recycled paper (25% post-consumer waste).
UNIX
man ls
For more information about the ls command, see its man page.
• Variables where you provide the substitution are shown in bold italic letters.
Examples:
mv filename1 filename2
General
• Fortran programs, shell scripts, and screen displays are shown in Courier
(“typewriter”) font.
• You must press RETURN after each command, but the RETURN is not shown
in the document, except when required for clarity.
Inserting text 51
Deleting text 52
Searching text 60
Changing your vi editor environment 61
Filename expansion 83
Environment variables 96
Setting an environment variable 96
Removing an environment variable 96
Viewing environment variables 97
Index ........................................................................................................................................129
The UNIX
computing environment
Contents
Welcome to the UNIX operating system. This manual contains basic instructions
to get you up and running as a new UNIX user. Most of the information in this
book applies to any UNIX system at any location. However, since there are some
differences in versions of UNIX implemented at different sites, some of the details
described in this manual may not apply to the UNIX system you are using. In such
cases, consult the online documentation (access to it is described in Chapter 2),
locally written documentation, or your local system administrator for assistance.
The end of this chapter provides sources of additional UNIX information, and
Appendix A lists recommended UNIX books.
In the late 1960s, Bell Laboratories (a research section of the American Telephone
and Telegraph Company) developed a version of the UNIX operating system that
has evolved as a standard for interactive computing. The current version is called
System V. Then another version of UNIX was developed at the computer science
department at the University of California, Berkeley. This version of UNIX is
referred to as BSD (Berkeley Software Distribution).
As UNIX gained in popularity, many computer vendors developed UNIX
operating systems under various names. A few of these are UNICOS (Cray
Research, Inc.), AIX (International Business Machines, Inc.), and ULTRIX (Digital
Equipment Corporation).
At this writing, the Scientific Computing Division (SCD) UNIX front-end
computer runs a Berkeley Software Distribution (BSD) UNIX system, with System
V compatibility. The SCD Cray supercomputers run UNICOS, a version of AT&T
UNIX System V.
The UNIX operating system manages the resources of your computer system to
perform useful work on your behalf. It is composed of three major parts, as
described below and shown in Figure 1.
UNIX provides a wide variety of utilities and tools to help you get your work done.
These utilities include editors (such as vi), file manipulation commands (such as
the cp, mv, and rm), text formatting applications (troff), programming languages
(C, Fortran, Pascal, and others), communications (File Transfer Protocol), and
electronic mail.
As a user, you do not interact directly with the UNIX utilities or the UNIX kernel.
Instead, commands you type are interpreted by a UNIX shell, which sends requests
on to the operating system.
UNIX shells are really high-level programming languages. The shell lets you type
commands efficiently and develop shell script files containing commands and
programming constructs (such as conditional testing) that automate very complex
operations.
Three popular UNIX shells are the Bourne, C, and Korn shells. Your UNIX system
supports some or all of these. Chapters 4 and 5 of this manual introduce you to the
power of the shell and provide information to help you choose which shell to use.
Shells
Utilities
Kernel
Hardware
Classes
There are many UNIX books available in libraries and bookstores. Appendix A of
this manual lists some that we recommend.
Your local computing site has probably made some local modifications and
additions to the UNIX system you’re using, such as adding local printing
commands. Ask your system administrator for any site-specific documentation.
Online documentation
Every UNIX system has detailed online documentation, known as the man pages
(which stands for “manual pages”). Access to these is through the man command,
described in Chapter 2.
SCD offers introductory UNIX classes in Boulder for NCAR users. For information
about upcoming classes, send e-mail to scdinfo@ncar.ucar.edu or call SCD User
Information at (303) 497-1225.
For instructions on how to login from your location and for site-specific UNIX
documentation for SCD users, contact SCD User Information by sending e-mail to
scdinfo@ncar.ucar.edu or by calling (303) 497-1225.
SCD provides an online Daily Bulletin, which gives current information about the
computing environment. To read it when you are logged in to any UNIX computer
at NCAR, at the UNIX shell prompt, type: dailyb.
Information about SCD documentation and consulting services is on the last page
of this manual.
This chapter introduces you to the basic UNIX commands listed on Table 1. For
more detailed information on these and other UNIX commands, see the online
man pages and the books recommended in Appendix A.
Logging in
UNIX is case sensitive. Be sure CAPS LOCK is not on.
Note
1. After you connect to the UNIX system, your screen displays login:, which is
the system prompt for your login id. Enter your login id.
2. Your screen then displays password:, the system prompt for the password that
goes with your login id. Type your password. (It will not be displayed on the
screen.)
Logging out
At the end of your computing session, you need to log out. The exact command
you use will depend on how your computing environment has been set up. At
least one of the following commands should log you out:
In UNIX, there are usually several options available for commands; the options
change the way the command functions. This manual covers the most critical
options. See the man page for each command for a complete list of its options.
Here are some points to remember about UNIX command syntax:
• UNIX commands begin with the command name. For most cases, the
command may be followed by various options, which may then be followed by
one or more filenames, depending upon the command’s purpose:
Example 1:
Example 2:
The command line below is one that shows a different syntax. The pathname
/usr/local/bin follows the find command. The -name option is a word, not just a
single character, and it uses the string file1 as its value.
find /usr/local/bin -name file1 -print
The find command helps you locate files and directories on the UNIX system.
All UNIX systems have online documentation that provides complete information
about the UNIX commands available with that system. Online documentation is
accessed through the man command (which stands for “manual”). At the shell
prompt, type:
man command
where command is a command you want information about. Enough text to fill
your screen is displayed. If more text is available, press the SPACEBAR key after
you read the first page. (The RETURN key is the default pager for the man
command on some UNIX computers.)
The man command provides detailed information about the syntax, the function,
and the options available with the command.
A useful man option is:
-k string Locates all commands documented in the man pages that contain
string in the command name or in the NAME section.
The online manual provided by the man command is a complete system manual
written by programmers for programmers. Because of this, beginners usually need
to study the man pages carefully to use them effectively. The information in a man
page is organized into specific sections.
• The SYNOPSIS section shows the command line with all the associated
options. The [ ] (brackets) surround optional elements. You do not type the
brackets.
• The DESCRIPTION section details use of the command and describes any
options.
• A man page may contain several other sections such as CAUTIONS,
WARNINGS, ENVIRONMENTS, NOTES, and FILES.
% man man
MAN(1)
NAME
man - Displays on-line manual entries
SYNOPSIS
man -i keyword[,keyword]
man [-o] [-u] [-b] [-l] [-p] [-r] [-s] [-d] [-e] [-q] [section] entries
man [-o] [-u] [-b] [-l] [-p] [-r] -k pattern
DESCRIPTION
The man command has two functions: it displays on your
terminal screen (stdout) an entry (man page) from an on-line
Example 4:
A valuable option on the man command is the -k option; it lets you search the
online manual pages for a subject that you specify as a value following the -k
option. This is a good way to explore the contents of the online manual.
man -k directories
This lists all UNIX commands that have the word “directories” in the line under
the “NAME” heading.
You might see a list that looks similar to this:
If no entries are found for the topic, try shortening your search string. For example,
if man -k compilers finds nothing, try man -k compiler instead.
You should change your password the first time you log in to any UNIX system
and often thereafter for optimum security.
The passwd command allows you to change your password. When you type
passwd with no options, the utility prompts you to enter your old password, then
your new password, then your new password again (to make sure you didn’t
make a typing mistake the first time). Passwords never appear on the screen.
Example 5:
Here is an example of what you will see when you type the passwd command.
Note that you will not see any of the passwords you type.
% passwd
Changing password for jones.
Old password:
New password:
Retype new password:
%
Account security
The UNIX operating system has a hierarchical design for storing files. The file
system is arranged as an inverted tree. Commands that allow you to move around
in the tree are explained later in this chapter.
The top of this inverted tree structure is a directory called root; it contains files and
more directories. All directories and files in the tree have a unique name. The name
of the top directory in the UNIX hierarchy is pronounced “root” and is written as
a forward slash (/).
Directories for users are organized into subdirectories of root. On many UNIX
systems (including those at NCAR), these directories are named un where n is a
number such as 0, 1, or 2.
When you log in, you are automatically placed in a subdirectory referred to as your
home directory. By UNIX convention, your home directory has the same name as
your login id. Within your home directory, you may store files and create other
subdirectories to hold more files and more subdirectories. This hierarchical
structure allows you to organize your files into related groups.
/ root directory
...
u1 u0 bin
home directories
... ...
jones smith doc
Using pathnames
A filename that indicates branch nodes is called a pathname. Because two different
directories may contain files that have the same name, when you refer to a
filename in UNIX, you must specify the pathname to make clear which file and
which branch you mean. You may indicate a filename or directory in two ways:
1. An absolute pathname starts at the root directory.
2. A relative pathname starts from the directory in which you are currently
working (known as the current working directory).
Slashes within a pathname are delimiters between directories and filenames. No
spaces are allowed within a pathname. Wherever a filename is required in a UNIX
command, you may use a pathname and it may be either a relative or an absolute
pathname.
Absolute pathnames
An absolute pathname always starts at the root directory (/). Thus, whenever you
see a UNIX pathname that starts with a / (slash), it is an absolute pathname.
(When saying the full pathname of a file, the initial slash is pronounced “slash,”
rather than “root.”)
Here’s an example of an absolute pathname:
/u1/jones/project1/test.f
Here, test.f is a file in a directory called project1, which is in a directory called
jones, which is in a directory called u1, which is in root. This is the absolute
pathname of the file test.f.
Relative pathnames
If a pathname does not begin with a /, then it is a relative pathname. If you log in
as “jones”, you are placed in the directory called jones, your home directory. You
could then refer to the test.f file using its relative pathname:
project1/test.f
If you change your current directory to the project1 directory (we’ll show you how
later), then you can refer to the file by its name:
test.f
In every directory, there are two special directory files named . (pronounced “dot”)
and .. (“dot dot”) that you can use to help build relative pathnames for files.
The . directory file refers to the current directory. The .. directory file refers to the
next higher directory, known as the parent directory. For example, suppose you log
in as “jones,” and there are two subdirectories, project1 and jobs, in your home
directory. You could refer to the file test.f in the project1 directory as:
./project1/test.f
The dot is a way to say to UNIX to “start here” to search for a file.
If you were currently in the jobs subdirectory, you could refer to the file test.f in
the project1 directory as:
. ./project1/test.f
As you become more comfortable with UNIX, you will find that the concepts
behind pathnames are easy to visualize and, with a little practice, you’ll wonder
how you ever got along without the .. shortcut.
The C shell allows the use of the ~ (tilde) character as a substitution for the home
directory of any UNIX user, including yourself; for example, ~jones refers to the
home directory for the jones login id. Therefore, you could refer to the file test.f as:
~jones/project1/test.f
All files on UNIX-based computers are organized in the root directory and its
subdirectories. This section introduces you to the locations of some files you will
use when you are logged in.
Command directories
Commands you use during your computing session are commonly located in one
of the following directories:
/bin
/usr/bin
/usr/local/bin
The /bin and /usr/bin directories contain various system commands. For example,
various compiler commands are located in /bin. The /usr/local/bin directory
When you log in to a UNIX computer, you are placed in your home directory. For
example, if your login id is smith, your home directory path might be /u1/smith.
Library directories
Software libraries on UNIX systems are usually located in one of these directories:
/lib
/usr/lib
/usr/local/lib
The /lib and /usr/lib directories are usually reserved for vendor-supported system
libraries, and the /usr/local/lib directory usually contains libraries specific to the
local site.
System directories
There are other directories and files in the root directory that are mainly used by
the UNIX system. You generally do not use these other directories.
At times, we all wonder where we are in the UNIX directory structure (tree). The
command to print to the screen the pathname of the current working directory is:
pwd
The system responds with the absolute pathname up to and including the
directory where you currently are.
Example 6:
When you enter the pwd command to see the pathname for your current directory,
your screen displays something like:
% pwd
/u1/jones/project1
The ls command lists the names of the files and subdirectories in the current
directory. You may also list specific files in a directory by typing:
ls options filename1 filename2
The most common options to the ls command are:
-a Includes in its listing all files that begin with . (dot). By default, ls
ignores files that begin with a dot.
Example 7:
% ls
bin file1 project1 project2 rje
%
Example 8:
An example of the same listing, but using the -F option; shows files that are
directories or that are executable.
% ls -F
bin/ file1* project1/ project2/ rje/
%
% ls -a
. .login file1 rje
.. .ntwkparms project1
.cshrc bin project2
%
Example 10:
% ls -l
total 5
drwxr-xr-x 2 jones 512 Feb 28 12:04 bin
-rwxr--r-x 2 jones 2695 Mar 22 10:31 file1
drwxr-xr-x 7 jones 512 Feb 20 15:17 project1
drwxr-xr-x 2 jones 512 Feb 20 12:50 project2
drwxrwxrwx 2 jones 512 Mar 13 16:02 rje
%
The first field of each line in Example 10 shows the permissions set for that
particular file and whether the file is a directory. (File permissions are explained
later in this chapter.) You also see the owner of the file or directory (column 3), the
size of the file in bytes (column 4), and the date and time the file was created or last
modified (columns 5, 6, and 7). The last word on each line is the name of the file or
directory.
You can also combine options, such as asking for a long listing that includes the
dot files of the current directory:
ls -la
Example 11:
If jones is your home directory, to move into the project2 subdirectory, type:
cd project2
The current working directory is now project2.
jones
...
test.f text.c
Example 12:
To move from the project2 directory and into the project1 directory, type:
cd ../project1
To return to the jones home directory, type:
cd
The more command lets you scroll through the contents of a file, one screenful at
a time. The syntax for the more command is:
more filename
At the end of each page displayed, more displays the --More-- prompt. Press the
SPACEBAR to see the next page or view additional text by typing any of the more
subcommands. The following table lists some of the common more subcommands.
Command Description
SPACEBAR When viewing a file, displays the next screenful of the file
xRETURN When viewing a file, displays x more lines
h When viewing a file, displays help information
f Skips the next screenful of the file
/string Searches forward for the next occurrence of string and displays that
screen
q Allows you to leave more
The more command is not always part of System V UNIX systems, but it is on the
SCD UNIX computers. Other commands for viewing files are pg, less, and cat. The
pg command is documented on the next page. See the pg, less, and cat man pages
for more information.
Example 13:
The pg command, which stands for page, displays a file on your screen, a screenful
at a time. The syntax of the pg command is:
pg options filename
See the man page for the list of options.
The pg command has subcommands that allow you to move forward and
backward and to search among the pages of your file. After you invoke the pg
command, it displays a : (colon) prompt at the bottom of the screen where you
type the subcommands. Some of the most commonly used pg subcommands are
listed on the table below.
Table 3: pg subcommands
Command Description
RETURN Displays the next screenful of the file
n Moves to screen n
xl Begins the screenful at line x
h Displays help information
$ Goes to the last screenful of the file
f Skips the next screenful of the file
/string Searches forward for the next occurrence of string and displays that
screen
?string Searches backward for the last occurrence of string and displays that
screen
q Quits pg
The pg command is not always part of BSD UNIX systems, but it is on the SCD
UNIX computers. Other commands for viewing files are more, less, and cat. The
more command is documented on the previous page. See the more, less, and cat
man pages for more information.
Example 14:
To look at the contents of a file named file3 one page at a time, type:
pg file3
Example 15:
In the project1 directory, to make a copy of test.f and name it test1.f, type:
cp test.f test1.f
jones
To change the name of a file or directory in UNIX, you move it from one name to
another (that is, rename it) using the mv command:
mv old_pathname new_pathname
You can move a file from one directory to another this way.
Example 16:
Example 17:
Here is one way to move test.f from the project1 to the project2 directory. First,
change directories to project1, then use a relative pathname for old_pathname and
an absolute pathname for new_pathname:
cd project1
mv test.f /u1/jones/project2/test.f
jones
test.c test.f
Or, after changing to project1 directory, use fewer keystrokes by using .., the
system file that refers to the next higher directory, and specify only the destination
directory without the filename; mv uses the same filename it already has. The mv
command is:
mv test.f ../project2
To remove (delete) files from a UNIX directory, use the rm command with this
syntax:
rm options filename(s)
The rm command permanently deletes a file from the UNIX system. Three options
you can use with rm are:
-f Tells rm not to send you any error messages (ignore any error
conditions) and to remove the file if possible. You might use this
option when you are deleting a list of files and you aren’t sure
whether all of the listed files exist.
Example 18:
To remove the files named file3 and data from your current directory:
rm file3 data
Example 19:
You want rm to check with you before removing the file named data:
rm -i data
If you do not answer the prompt with a lowercase y (for “yes”), the rm command
does not delete the file. Your screen display would look like this:
% rm -i data
data: ? y
%
Each file has a permissions structure that allows read, write, and execute
permission for three different categories of users. UNIX treats directories as special
files that have the same permissions structure.
File permissions can be displayed using the ls -l command. File permissions are
displayed in a 10-character string such as:
drwxrwxrwx
or—
-rwxr-xr--
The first character is either a d or a - (hyphen). The d denotes that this file is a
directory; - (hyphen) denotes that it is a file other than a directory. Next, the three
characters, r, w, and x, refer to the read, write, and execute permissions of the user
who owns the file (see Figure 6, below). The next set of three characters indicates
the access permissions of the group of users associated with the file, and the last
three characters show the access permissions for all other UNIX users. In these
three sets of three characters, a hyphen denotes that the type of permission
associated with that position in the display is not granted.
d r w x r - x r - x
Read, write, and execute permissions mean different things for files and for
directories, as detailed in the following table.
Default permissions
The default file creation mode is set with the umask command in your shell’s login
script file. You’ll learn about this file in Chapters 4 and 5. For UNIX computers in
SCD at NCAR, the initial default permissions are set to read, write, and execute
permission for the owner and read and execute permission for the group and
others (umask 022).
Only the owner of a file or directory can modify the permissions of that file or
directory. The chmod command (which stands for “change mode”) is used to
change permissions for a given file or directory. The syntax for the chmod
command is:
chmod mode filename
where mode has the format:
who_operator_permission
The who part of the mode can be one or any combination of:
u User permission
g Group permission
o Others permission
The operator part of the mode can be either:
+ Adds permission
- Subtracts permission
The permission part of the mode can be one or any combination of:
r Read permission
w Write permission
x Execute permission
Multiple modes can be specified if they are separated by commas and no spaces
are included.
For files to which other users have linked (using the ln command), permissions are
set by the owner of the file and users linked to it may perform functions according
to those file permissions.
Example 20:
Remove write permission for group and others from a file called data:
chmod go-w data
Example 21:
Add group write permission to the file data, and remove all permissions for others:
chmod g+w,o-rwx data
To create a subdirectory, use the mkdir command, which stands for “make
directory.” The syntax is:
mkdir pathname
The pathname may be either an absolute pathname or a relative pathname.
Example 22:
To create a new subdirectory named project3 in the current working directory, the
command to type is:
mkdir project3
jones
test.f text.c
To remove (delete) a directory, use the rmdir command. The syntax is:
rmdir pathname
This removes the directory indicated by pathname. The directory to be removed
must be empty. To make sure a directory is empty, you can list the files in it with
the ls command, using the -a option. If the directory contains files, you need to
move them or delete them before you can use the rmdir command. (However, you
do not need to remove the files named . and .. from the directory.)
We recommend that you change to the parent directory of the directory you want
to remove, before using the rmdir command. This method helps you keep track of
exactly which directory you are removing.
Example 23:
To remove the project2 directory, change to the directory just above it (the parent
directory) and type:
rmdir project2
jones
test.f text.c
The grep command (global search for a regular expression) examines the contents
of a file (or files) for lines that contain strings that match a certain pattern you
specify. The syntax is:
grep options "pattern" filename1 filename2
Here are two useful options:
Example 24:
The simplest use of grep is to look for a pattern within one file. To look for the
pattern “error listing” in the file called output, you would type:
grep "error listing" output
Any lines containing “error listing” will then be displayed on the screen. If no
matches are found, your screen will show the shell prompt, ready for your next
command.
Example 25:
This example uses grep as a filter, so that only those lines of the input files
containing the “pattern” will be sent to your screen. (Chapter 5 provides details on
the use of filters and pipes.) To search for the pattern “Aug” in the output from a
long listing (ls -l) of a directory, you would type:
ls -l | grep "Aug"
The ls -l part of the command line is executed first. The output from the ls -l
command is then piped (|) to grep, which searches the output for any lines that
contains “Aug”. The output from this command line might look like this:
The ln command allows many users to access a single copy of a file. As well as
conserving disk space, the ln function provides an easy way to update information
in a file that is accessed by many users. A good example of this is a data file,
updated regularly, and accessed by many users within one work group.
With the ln (link) command, a directory entry is added for each user who links to
a file. The command syntax is similar to the cp command, and you may use the
same or a different name for the linked file.
When creating a link to a file, the new reference to the file retains the original
ownership and the original date/time stamp. Even if the file owner removes the
file, the file and the links to it will continue to exist until all links to it in the system
have been removed.
Read the section on the chmod command in this chapter to learn how to designate
the permissions other users, linked to one of your files, have for that file.
Example 26:
In the project2 directory of jones, to create a directory entry named my_data that
links to the file data in the smith subdirectory projectb, type:
ln ~smith/projectb/data my_data
Figure 8. The file structure showing the link to smith’s file named data
jones smith
You can use the UNIX rcp command (remote copy) to copy files between UNIX
computers. The rcp command is one of the TCP/IP applications.
To use rcp, the computers involved in the transfer must meet these requirements:
(1) they must both support rcp, (2) they must be connected to the Internet, (3) the
computer that is the target of the rcp command must have a file in its home
directory named .rhosts. This file must contain a line that references the computer
from which you are issuing the rcp command and your login id on that computer,
and (4) the .cshrc file on the local computer must not have any input/output in it,
such as an echo command. See your system administrator if you need more
information on these requirements.
For security, file permission for the .rhosts file should be set to read and
Note write for the owner only. Execute permission should not be set at all. For
more information about setting file permissions, see the chmod
command in this chapter or the chmod man page.
When you use rcp, the file you are copying is specified first, and the new
destination and new name of the file is specified second. The filename on the
remote computer must always include the name of the computer (hostname)
followed by a colon (:) preceding the pathname of the file itself.
To copy a file from a remote computer to your local computer and place the file in
your current working directory, the syntax is:
rcp hostname:pathname newfile
To copy a file from your local computer to a remote computer, the syntax is:
rcp pathname hostname:newfile
The rcp command assumes a path relative to your home directory on the remote
computer unless you specify an absolute pathname.
If your login id for the remote computer is different from your login id for the local
computer, you need to include the login id along with the name of the computer
to which you are copying the file with the rcp command. See the examples on the
next page. If the syntax in the examples don’t work on the UNIX computers you
are using, consult the system administrator. Some rcp packages use different
syntax.
Example 27:
User “jones” is logged on to a local computer, and is in the directory where the file
named proposal resides. To copy proposal to the home directory on a computer
named thunder.uiuc.edu and rename it prop.0621:
rcp proposal thunder.uiuc.edu:prop.0621
If “jones” must use the login id “jjj” on thunder, the syntax would be:
rcp proposal jjj@thunder.uiuc.edu:prop.0621
Example 28:
User “jones” is logged on to a local computer. To copy the file named cloud from
the remote computer thunder to the local computer and call it cloud9 in the current
directory, jones would type:
rcp thunder.uiuc.edu:cloud cloud9
If “jones” must use the login id “jjj” on thunder, the syntax would be:
rcp jjj@thunder.uiuc.edu:cloud cloud9
The ftp command is the user interface to the Internet standard File Transfer
Protocol (FTP). You can use the ftp command to transfer files between your local
computer and a remote computer if both computers have access to the Internet,
support the FTP application, and have TCP/IP communications software.
There are some small differences in the version of ftp on different computers. If the
instructions outlined here don’t work on your particular computer, see the ftp
instructions for that computer.
1. Establish a connection with the remote computer using an ftp command that
follows this format:
ftp remote_computer
The remote_computer may be either the Internet name (such as ftp.ucar.edu)
or the Internet Protocol (IP) node number (such as 128.117.64.4) of the
computer with which you want to connect.
2. After establishing the connection, ftp prompts you to enter a login id and a
password.
3. After the login and password are accepted, you will get an FTP prompt, which
is usually ftp>. You can then start the file transfer process.
4. To end the FTP session, use either the quit or bye command.
Transferring files
When transferring a binary file (such as a graphics metafile), you must first set the
file transfer type to binary within the FTP session, using the binary command. To
reset the transfer type to ascii (text files) after the binary file has been transferred,
use the ascii command. For example, to retrieve a binary file and then retrieve an
ascii file, the command sequence would follow this format:
binary
get binary_filename
ascii
get ascii_filename
Other useful FTP commands include mget (multiple “gets” with one command),
mput (multiple “puts”), help, cd (change directory), and ls (list files). For more
information on the ftp command, see its man page. For site-specific information on
FTP at NCAR, see the SCD UserDoc, “Using FTP for File Transfer at NCAR.”
Documentation ordering information is on the last page of this manual.
FTP is also used to share files with other users on the Internet through anonymous
FTP accounts. These accounts allow anyone on the Internet who can access the host
computer via FTP to download files without requiring that the user have an
account, a password, or operator assistance. SCD uses an anonymous FTP account
to make some user documentation available. Directions for using this account are
on the last page of this manual.
A user with the login id of “jones” wants to FTP a copy of a program named
my.prog from the current directory on the local computer to the jones home
directory on meeker.ucar.edu and give it the name jones.prog. Then, jones wishes
to retrieve the prog8912 file from meeker and place it in the current directory on
the local computer.
The system prompt for the local computer is % (percent sign). The FTP prompt is
ftp>. This is how the FTP session would appear:
% ftp meeker.ucar.edu
Connected to meeker.ucar.edu
220 meeker FTP server (Version 4.162 Mon Jun 18 16:39:52 MST 1990) ready.
Name (meeker:jones) : jones
331 Password required for jones.
Password:
230 User jones logged in, access restrictions apply.
ftp> put my.prog jones.prog
200 PORT command successful.
150 Opening data connection for jones.prog (ascii mode) (3318 bytes).
3384 bytes transferred in .22 seconds (15 Kbytes/s)
ftp> get prog8912
200 PORT command successful.
150 Opening data connection for prog8912 (ascii mode) (31256 bytes).
32001 bytes transferred in .54 seconds (58 Kbytes/s)
ftp> quit
221 Goodbye.
%
The messages you see on your screen may vary from this example,
Note depending on the FTP package installed on your local system.
The table below introduces a few more UNIX commands that you may find useful.
To get more information on any UNIX command, see its man page. Also,
Appendix A lists several UNIX books.
Command Description
ps Allows you to monitor the status of UNIX processes; provides the PID
(process identifier) for every process currently running and owned by you.
kill Sends a signal to a UNIX process. To terminate a currently running process,
type:
kill -9 PID
where PID is the process number you obtained from the ps command.
You cannot kill a process that you do not own.
file Allows you to see the type of file; type file followed by the filename:
file filename
This command is useful for determining whether a particular file is editable;
that is, whether it contains ASCII text or binary data.
find Allows you to search a directory hierarchy for a file by name or other
characteristic, such as type, owner, permission, and so forth. Use the
-print option to list what is found.
diff Allows you to determine whether two files are different from one another and
how they differ. Reading the output from the diff command is a little tricky and
you may want to redirect it to a file so you can print and study the results.
comm Allows you to find lines in common between two files (opposite of diff).
head Allows you to view the beginning of a file. By default, head displays 10 lines;
you can specify the number of lines to display.
tail Allows you to display the end of a file (the opposite of head). By default, tail
displays 10 lines; you can specify the number of lines to display.
who Lists all current users on the system.
wc Counts characters, words, and lines in a file.
Inserting text 51
Deleting text 52
Searching text 60
This chapter introduces you to the basic vi editor commands listed on Table 1. For
information on additional vi commands, see the recommended books in Appendix
A.
Command Description
Creating and editing a file
vi filename Invokes the vi editor for filename
Moving around in the file
CONTROL-f Moves forward a screenful
CONTROL-b Moves backward a screenful
CONTROL-u Moves up one-half screenful
CONTROL-d Moves down one-half screenful
nG Moves cursor to nth line of file
G Moves cursor to the last line of file
h Moves cursor one character to the left
j Moves cursor one line down
k Moves cursor one line up
l Moves cursor one character to the right
The h, j, k, and l commands can be preceded by a number that specifies
how many times to repeat the command.
Inserting and deleting text
i Inserts text before the cursor
I Inserts text at beginning of current line
a Appends text after the cursor
A Appends text at end of current line
o Opens a line after the line cursor is on and begins insert mode
O Opens a line before the line cursor is on and begins insert mode
The standard UNIX screen editor is called vi, which stands for “visual editor.”
While there are other editors available on some UNIX systems, we recommend
that you learn the vi editor because it is a part of all UNIX operating systems and
usable on any type of terminal. The vi editor is efficient, powerful, and flexible. It
also has some ability to recover your file if there is a computer crash.
Unlike many screen editors, the vi editor does not rely on function keys specific to
a particular terminal. Instead, it supports a set of keys to simulate function keys
found on some terminal keyboards.
This chapter starts you off with a set of essential vi editing commands. Several
books devoted to the vi editor are listed in Appendix A.
Basic vi rules
• All work is done in a buffer and only written to disk when you save the file.
• If the file already exists, a copy is brought up on your screen for editing when
you invoke vi. If the file doesn’t exist, a blank screen is brought up for editing.
• There are two modes of operation within vi: command mode and insert mode.
These modes are explained in detail later in this chapter.
• When in doubt about which mode you are in, press the ESCAPE key. This
returns you to command mode.
• All vi commands are case sensitive, so make sure your CAPS LOCK key is not
set.
Example 1:
~
~
"junk" 3 lines, 70 characters
This is the contents of the file named junk. The cursor position is indicated by an
_ (underscore). A ~ (tilde) character at the beginning of a line indicates that the line
doesn’t exist.
A new file looks similar, but since it doesn’t contain anything, there are tildes at the
beginning of every line you see on your screen. As with the above file, the cursor
is positioned at the beginning of the file.
To exit your file, first you need to decide whether to save or discard the changes
you made to the file. Saving the changes writes the temporary buffer copy of the
file to a disk file. If you don’t want to save the changes, you can exit the editor and
discard the temporary buffer.
There are three ways to save the changes made to a file:
:wq Writes the buffer into the filename you gave when you started the
editor and exits the editor.
ZZ Same as :wq. Writes the buffer into the filename you gave when
you started the editor and exits the editor.
When you type the colon commands, the colon and your command appear in the
lower left of your screen. When you press RETURN, the command is executed.
To exit the editor without saving any changes, use the command:
:q! Discards the buffer and exits the editor. The text file is still just
like it was when you entered the editor (or since the last time you
entered :w).
Example 2:
Here’s an example of how the screen appears when you are in the editor and you
type in the command to write the file and quit the editor.
Notice that :wq is entered as a command at the bottom of the screen. Then, when
you press RETURN, the buffer contents are written to the file you named when
you invoked vi (in this example the file is named junk) and you are returned to the
shell prompt.
Commands for scrolling forward and backward in your file are done by holding
down the CONTROL key and pressing a letter key at the same time. Some of the
most commonly used ones are:
To move the cursor around within the text, the arrow keys are usually used, but
these are actually function keys specific to your terminal. These arrow keys may
do the job with vi, but they also may not, depending upon your terminal type. An
alternate form of cursor movement uses character keys as follows:
h Moves cursor to the left one character for each time h is pressed
RETURN Moves the cursor down one line for each time RETURN is
pressed, and positions the cursor at the beginning of each line
If you precede any one of these cursor-moving commands by a number, it affects
the number of lines (for up and down movement) or columns (for left and right
movement) that the cursor moves. For example:
You need to tell the editor when you want to enter insert mode before you can
insert text. There are several different ways to do this.
Deleting text can be done by line, word, or character. Here are the commonly used
deletion commands.
dw Deletes the word the cursor is on; starts from the cursor and
deletes to the end of the word
It is easy to delete multiple lines, words, or characters. The dd, dw, and x
commands may be preceded by a number that specifies how many times the
command is to be executed. See the examples below.
To replace or change text in vi, use the change commands listed below.
cw Changes the current word (from the cursor position to the end of
the word) to whatever sequence of characters you type—until
you press ESCAPE
Example 4:
To change the word “Junk” to “Trash,” position the cursor at the beginning of
“Junk” (the cursor position is indicated by the underline). When you enter the
command to change the word, the last character of the word is displayed as a
$ (dollar sign). From that point, everything you type will be the new word, until
you press ESCAPE. So in this example, after positioning the cursor and typing cw,
a $ replaces the k in “Junk” to mark the end of the string being replaced.
Now, you type the new text (Trash) and press ESCAPE. The result is:
Notice that the cursor is positioned at the end of the changed word.
Changing an entire line is similar, except that when you give the command cc, the
entire line is erased, leaving a blank line, and you are in insert mode. Again, the
underline indicates where the cursor is during each part of the command.
After you type the new text and press ESCAPE, the change appears as shown:
The yank and put commands (yy or Y and p or P) provide one way to copy and
move text within your file. The delete command, dd, can also be used to delete text
from one location and put it (with the p or P command) in another location.
dd Deletes (in reality, moves text to the buffer) one line of text
ndd Deletes (in reality, moves text to the buffer) n lines of text
The yank method (yy or Y) copies the text, but does not remove it from its original
location in the file. The delete method (dd) removes the text from its original
location in the file. You may then place the copied or deleted text at a new location
using put (p or P) at a new location in the text.
If you want to copy text from one file to another or insert the same text at multiple
locations in the file, you may yank it into a special buffer that remains unchanged
during your current editing session. The vi editor maintains a set of lettered
buffers for that purpose. These special buffers are explained in the next section,
“Copying and moving text between files.”
You want to yank (copy) the first six lines in the following file and move them
elsewhere in the same file. Move your cursor to anywhere in the first line to be
yanked, and type the editing command:
6yy
After a yank command, your screen will look like this:
(On most UNIX systems, if you yank fewer than five lines, the number of lines does
not display at the bottom of your screen, even though they have been yanked into
the buffer.)
Move your cursor to anywhere in the line you want the yanked text to follow. Type
a lowercase p:
p
and the resulting file will look like this:
This is the spot in the file after which the yanked text is inserted.
Here is a test file.
It has several
lines of text that
can be moved around as needed.
One way to move text is
to use the yy (yank) command,
~
~
~
6 more lines
If you don’t want the yanked text to be repeated both places in the file, go back to
the original location of the yanked text and type:
6dd
to delete those lines.
This section tells you how to copy and move text between files by using the lettered
(a-z) buffers to store the text to be moved. You can use any letter for the designated
buffer, and you can be using more than one lettered buffer during any editing
session to store different pieces of text. The text remains in the buffer you assigned
it to until you end the editing session.
The first character in the commands listed here is " (double quotation mark).
"ap Puts text currently in buffer a below the cursor line; a copy of the
text also remains in buffer a
"aP Puts text currently in buffer a above the cursor line; a copy of the
text also remains in buffer a
You can also use this method to copy and move text within a file.
Here are the additional editing commands you need to copy and move text
between files.
:e Returns to the other currently open file for editing; used to toggle
back and forth between files
You want to yank (copy) six lines from the file addendum and move them to the
file manuscript1. You have already opened the file addendum with vi and made
some editing changes. Type:
:w
to write any changes that are currently in the buffer to the file.
To place the first six lines into buffer a move the cursor to anywhere in the first line
to be moved and type:
"a6yy
To open the file manuscript1 (the file where you want to put the text), type:
:e manuscript1
Move your cursor to anywhere in the line you want the text that is stored in buffer
a to follow. Type:
"ap
The text from buffer a is placed below the line the cursor is on. A copy of the text
also still remains in buffer a, so if you need to insert the same text again later in the
file, you can do so by using another "ap command.
Before you return to the other file, type:
:w
to write the changes you made to the file.
To toggle back to the other file, type:
:e
If you have made any changes to the file, you must always enter a :w before you
return to the other file.
You can search a file for a string of text using one of these commands:
Example 8:
To search a file for the string of characters “line 9,” type the search command and
it appears in the lower left corner of the screen. Type the search string, then press
RETURN; the command is executed and the search is on.
:
Junk file - line 8
Junk file - line 9 <-(cursor is here after search)
Junk file - line 10
~
~
~
~
~
~
~
~
~
/line 9 <-(search command typed in)
When the string is found, the cursor is at the beginning of the string that was being
searched for.
You can modify your vi editing environment to suit your own needs in two ways:
1. You can use :set commands within the vi editor to modify the editing
environment for the current session only.
2. You can use the .exrc file or setenv EXINIT variables to modify the editing
environment for all vi editing.
See the vi man page for details about customizing your vi environment with these
tools.
What is a shell? 65
What the shell does for you 65
What is a subshell? 68
Table 1: Features of the different shells 69
What is a shell?
The shell is your user interface to the UNIX operating system. The primary function
of a UNIX shell is that of a command interpreter. When you type a UNIX command
and press RETURN, the shell accepts that command, interprets it, and then makes
a request to the UNIX operating system to execute the command. For you as the
user, all this happens transparently. However, the shell actually interprets the
shell-specific characters you type before passing your command on to the UNIX
operating system. The special shell characters allow you to streamline the use of
the standard UNIX commands and work more efficiently in the UNIX
environment.
After you log in to a UNIX system, the shell is the program the operating system
first executes. When you receive the prompt, you are operating in what is referred
to as your login shell; the prompt displayed on your screen is the shell prompt.
The UNIX shell is a complete programming language, containing a rich set of
control structures (including loops and conditional statements). Some shells also
contain data types, arrays, and user-definable functions or procedures. These
constructs may be used either interactively or from within a shell program (called
a shell script).
This chapter introduces you to three commonly used UNIX shells and their basic
use. The following chapter, “Using the power of the UNIX shell,” discusses some
of the more advanced features of shells, which will help you become more efficient
in your use of UNIX.
UNIX shells do many things for you in the process of executing commands. For
instance, the shell can direct the output of a command to a file rather than display
it on your terminal screen. The shell can also cause the output from one command
to become the input for another command. These features are known as
I/O redirection and are discussed in more detail in the next chapter.
UNIX shells provide a wildcard capability when you type file and directory names.
The UNIX shell interprets special wildcard characters and automatically expands
names according to filename expansion rules. This gives you flexibility in
searching for files and directories by certain patterns or when you are unsure of
some characters in the name.
Some UNIX shells maintain a list (or history) of commands you have recently
executed. History mechanisms are useful features for an interactive command
interpreter; they allow you to easily re-execute long instructions to the shell.
There are currently three UNIX shells that are in widespread use in the UNIX
community: the Bourne, C, and Korn shells. Check with your system administrator
to find out what shells are available on the UNIX system you are using, and which
shell is the system’s default shell. For example, in the Scientific Computing
Division at NCAR, the Bourne, C, and Korn shells are available on all UNIX
systems and on the Cray supercomputers running under the UNICOS operating
system. The C shell is the default shell for all these systems.
All UNIX shells are command interpreters, but each has different characteristics,
strengths, and weaknesses. You need to choose which shell you want to use. Your
choice will affect how commands are interpreted. Your choice of shell is subjective;
your own preferences and the shell characteristics you need will be determining
factors. You can alter the shell interface of any of the shells to suit your particular
needs. To help you choose a shell, the three shells are described briefly in this
chapter and their features are compared on Table 1. More detailed shell
information is in the books listed in Appendix A.
The Bourne shell is the original shell. It was named for Stephen Bourne, who wrote
it while at Bell Laboratories.
The Bourne shell has several features that lead to its popularity with users; one
example is the Bourne shell’s independent handling of program output and error
message output. It also has functions that allow you to define shortcuts for
complex commands, including all of the Bourne shell’s powerful control
constructs (such as if statements and loops). Some users prefer the Bourne shell as
a scripting language over the C shell because of its efficiency, compactness, and
signal-handling capability. One advantage of the Bourne shell is that it is the only
UNIX shell found on every UNIX computer.
One primary drawback of the Bourne shell is its lack of a mechanism that allows
easy recall of previously-typed commands (known as a history mechanism and
available in both the C and Korn shells). Also, the scheme for arithmetic expression
evaluation is somewhat awkward.
The C shell
The C shell was the second UNIX shell to be developed. It was written by Bill Joy
while he was a computer science student at University of California at Berkeley. It
was named for its syntactic similarity with the C programming language.
The Korn shell is the newest of the three UNIX shells discussed here. It was named
after its author, David Korn of AT&T Bell Laboratories. The Korn shell’s
programming language is a backward-compatible subset of the Bourne shell’s
language, including shell functions and many other advanced features. The Korn
shell has a history mechanism and a command aliasing capability. Command
aliasing combined with other shell features allows great flexibility in customizing
your environment. Some users feel that the Korn shell contains the best features of
both the Bourne and C shells, and may someday grow to be the most popular shell
in use.
The disadvantage of the Korn shell is that until recently it has not been widely
available. Currently, it can run on everything from a Sun workstation to a Cray
supercomputer, but it must be licensed and installed separately from the UNIX
operating system.
You can change your shell for the current login session or you can change your
default shell for all subsequent sessions.
During a single login session, you may temporarily change to another shell and
then revert back to the original shell you were using. For example, if you are using
the C shell when you first log in, you may temporarily change to the Bourne shell
for your current computing session by typing:
sh
after logging in. You will be placed in a Bourne subshell and have access to the
Bourne shell commands and features.
In order to log out of the computer, you must first log out of the Bourne subshell,
then log out of the computer.
You may permanently change your default shell for all subsequent login sessions.
For example, to change your default login shell to the Korn shell, type:
chsh your_login /bin/ksh
at the shell prompt. Log out of the system and log in again. From this point on, you
will be working in the Korn shell, your new default login shell.
What is a subshell?
You just learned that at the command line you may type the name of another shell
if you wish to use it temporarily. For example, if you normally work in the C shell,
you may type ksh at the shell prompt to change to the Korn shell.
How does UNIX switch you from one shell to another without interrupting
operation or logging you out of the system? What actually happens is that UNIX
places you into a subshell. This is pretty simple for you: you just see a different
prompt that tells you that you’re now operating in a different shell. You have
access to all the features and commands of the new subshell.
Another example of using a subshell is when you execute a shell script. A shell
script is a file containing UNIX commands that you can execute from the command
line just by typing the name of the shell script file. A subshell is created whenever
you run a shell script.
The next chapter contains additional information about using shell scripts and
executing commands within a subshell.
All UNIX shells have initialization files that are used to configure the environment
when you log in. An initialization file is nothing more than a file containing UNIX
and shell commands. Initialization files are sometimes referred to as dot files, since
the first character in their filename is a dot (or period).
Some of the operations typically performed by initialization files include:
• Telling the shell which directories to search for commands and in what order
to search them.
• Configuring the environment to accurately reflect what type of terminal you
are using, so that the editors and other programs that perform screen
manipulation can do so correctly.
• Defining shell functions and/or aliases as shortcuts for some of the commands
you use frequently.
• Defining shell variables that can (1) serve to define default option lists for some
commands, or (2) serve as shortcuts for commonly used pieces of commands
that do not change often. For example, this could be a list of libraries to be
searched when compiling and linking Fortran or C programs.
• Performing any task that you would like done each time you log in to the UNIX
system.
The Bourne and Korn shells have two initialization files: the system file /etc/profile
and a file named .profile in your home directory. When you log in, /etc/profile is
read first and .profile is read second. The file /etc/profile is set up by the system
administrator to perform generic operations and to prepare a default user
environment; you cannot alter this file. The .profile file resides in your home
directory and is owned by you; you may make changes in your .profile file to
customize your own computing environment.
A very simple example of .profile file, which works for both the Bourne and Korn
shells, might look like this:
PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin
TERM=vt100
export PATH TERM
ls() {
/bin/ls -xF $*
}
The first line defines your search path—directories to search for commands and
order in which to search them. The second line defines your terminal type. The
third line makes this information available for all other UNIX processes started
The C shell has two initialization files: .login and .cshrc, both residing in your
home directory. On some UNIX systems there is a third system file: /etc/cshrc. If
your system has /etc/cshrc, it is set up by the system administrator to perform
generic operations and to prepare a default user environment for all users.
If /etc/cshrc exists, it executes first when you log in, followed by .cshrc in your
home directory. Finally, your .login file is executed after .cshrc. The .login file
executes only once, at login time; however, every time you execute a C shell
command, the shell searches the .cshrc file, and behaves according to instructions
found in that file. This file is where you should put your shell aliases, so that they
are interpreted by the C shell.
A very simple example of .cshrc might look like this:
#!/bin/csh
set path = (/usr/ucb /bin /usr/local/bin .)
alias ls '/bin/ls -al \!*'
In the C shell, the first line must be #!/bin/csh or simply a # (pound) sign with
nothing else on the line. The second line defines your search path. The third line is
an alias command that adds the -al options to the ls command every time it is
executed. Recall that the -a option displays the hidden dot files as well as regular
files and the -l option gives a long directory listing. The alias in your .cshrc file
overrides the system ls command. The \!* means that any other arguments you
enter for the ls command will be placed here.
The .cshrc file should not contain any commands that read from or
Caution write to a terminal. Also, it should not contain commands that
configure a terminal (for example, the stty, tset, or resize
commands). These operations should be in the .login file.
#!/bin/csh
umask 022
setenv TERM vt100
In the C shell, the first line must be #!/bin/csh or simply a # (pound) sign with
nothing else on the line. The second line in the above example defines your default
file permissions. The third line defines your terminal type. This example illustrates
the use of UNIX environment variables. You may change the characteristics of
your working environment by setting environment variables in your .login file. To
learn more about environment variables, see the section “Environment variables,”
in the next chapter.
Your .login and .cshrc files are ASCII text files that you may view, edit, or print.
You may change the .login and .cshrc files to customize your own computing
environment. After you modify these files, you must type source .login or
source .cshrc for the changes to take immediate effect.
Filename expansion 83
Environment variables 96
Setting an environment variable 96
Removing an environment variable 96
Viewing environment variables 97
If you do not specify the C shell or the Korn shell by the appropriate
Caution first shell script line shown above, the script is interpreted by the
Bourne shell. This may cause execution errors for scripts containing
C or Korn shell commands.
Here is an example of a simple shell script. The comment lines (any line that starts
with a # sign after the first line) indicate what the script does. Comment lines are
not part of the executable shell script.
#!/bin/csh
# anything following a pound sign is a comment
# remove ls.file
rm ls.file
#!/bin/csh
set dir1 = /usr/tmp/jones
mkdir $dir1
cd $dir1
ls > ls.file
mail jones@ncar < ls.file
rm ls.file
For simple scripts such as this, there is no difference among the shells (C, Bourne,
or Korn), except for the first line. Differences only show up in more complex scripts
that use features unique to one or another of the shells.
Shell scripts may contain constructs that are specific to the shell in which it
executes; these include looping, conditional, and branching constructs. While all
shells have these constructs, the syntax differs among them. You should decide
which shell you want to use for your scripts before you get too involved in the
details of construct syntax. Refer to the man pages for sh (the Bourne shell), csh
(the C shell), and ksh (the Korn shell) for more information.
The basic if construct for the C shell is:
if (expression) then
command_sequence_1
else
command_sequence_2
endif
In this construct, if expression is true, then the C shell executes
command_sequence_1; if expression is false, then the C shell executes
command_sequence_2.
Example 2:
This is an example of a simple C shell script that uses the if construct. Notice that
the first line of the script designates that this is a C shell script. Also, notice the way
that comments are used both as lines by themselves and within a line of text.
#!/bin/csh
#
# This is a C shell script that shows a simple use of the if construct.
# Compile source code on the Cray supercomputer.
cft77 source.f
• The shell variable status is a predefined C shell variable that always contains
the exit status of the last command executed. (Notice that you must type a $
before the variable name when referring to its value.) In this example, status
tests whether there were errors in the compile process, and we’ve based the
conditional processing on the resulting value of status.
There is a set of predefined shell variables that you may use in your scripts (status
is one of these). You may also define your own variables within a shell script (using
the set command). For more information on shell variables, refer to the
recommended shell books listed in Appendix A.
All three shells have filename expansion features that allow you to specify a file’s
name without typing in all the characters of the name. To do this, you type
characters that are called metacharacters or wildcard characters. In addition, there
are several constructs you can use to help specify a group of files with similar
names.
This section introduces the most commonly used filename expansion techniques
in UNIX. Other techniques are also available; as your knowledge of UNIX
increases, you may want to investigate them by consulting the shell man pages (sh
for the Bourne shell, csh for the C shell, ksh for the Korn shell). There are three
main types of metacharacter:
[abc] Matches any single character in the list. The square brackets
enclosing a string tells the shell to match any single character
listed in the string. As shown here, a, b, and c stand for single
characters to be matched.
The shell interprets these metacharacters, not the UNIX operating system; this is
an example of why we say that the UNIX shell is a command interpreter. The shell
actually performs the filename expansion and passes the resulting list of filenames
along to the command.
Example 3:
% ls c*
cAb cAd ccd cbd
%
Example 4:
% ls a?
aA aB ac
%
Example 5:
% ls c[Ac]?
cAb cAd ccd
%
The UNIX shell interprets all characters you type on the command line before
passing the command along to the UNIX operating system. There are many
metacharacters that have special meaning to the shell; a few are described on the
previous two pages. There may be times when you want the shell to simply pass
along something you have typed without interpreting it.
UNIX shells use the \ (backslash) character to allow you to hide, or escape,
characters from the shell. The backslash escape character causes the UNIX shell to
ignore the character immediately following it. The examples below show you
some uses for the UNIX escape character.
Example 6:
Suppose you are creating a script that contains a long command line. You can
make the command easier to read if you have it span two or more lines in the
script. You may escape the newline character (RETURN) by preceding it by the
\ (backslash).
% cp /u1/jones/project2/july93/sampleruns/file /u1/jones/project13\
/august93/goodruns/file7
There must be no spaces after the \ (backslash) and the following character that is
to be escaped (a RETURN in this example).
Example 7:
Suppose you wish to use one of the UNIX metacharacters as a literal. For example,
you wish to echo back some text that contains a question mark. You will need to
escape the ? (question mark) so that the shell will not attempt to interpret it. Notice
the difference in the two command lines shown below:
In the first case, the UNIX shell attempted to perform a substitution on the
question mark, looking for a five-letter filename starting with the letters “file.” It
could find no such filename in the current directory and returned an error
message.
Example 8:
You may also escape a string of characters using a pair of single quotation marks.
When you use quotation marks, you do not need to type a \ (backslash) to escape
each space that occurs in a long series of words.
You will see examples of this technique in the section “Aliasing commands,” later
in this chapter.
This section briefly defines the standard terminology you need to know to use
UNIX Input/Output (I/O) redirection. Usage examples are on the following
pages.
There are three I/O devices that are an integral part of all UNIX-based operating
systems. Every program that runs under UNIX may use these three devices. The
input device allows a UNIX program to receive input from the user or from
another program. In addition, UNIX has two types of output devices. The first type
receives the output from a program. The second type receives any error messages
that occur during the execution of a program.
UNIX has special names for these I/O devices:
stderr Standard error includes all error messages resulting from the
program. Error messages are directed to the screen by default
(Fortran unit 0).
You can also use >> (two greater-than signs) to have the shell append stdout to the
end of an existing file.
command >> output_filename
The file, output_filename may already exist; if it does not, the shell creates it.
Append output redirection creates a new file if one does not exist.
Caution If you set a shell variable called noclobber in your .login file,
append redirection only adds to an existing file. To learn more
about this, consult the man page for the C shell (csh).
Example 9:
To have the ls command redirect its output to a file named out.file, type:
ls > out.file
Example 10:
To have the ls command append its output to the same output file named above,
you would type:
ls -l >> out.file
You can tell UNIX to read the standard input (stdin) from a file instead of the
keyboard. You use the < (left angle bracket, also called the less-than sign) to
redirect stdin. Basically, input redirection looks like this:
command < input_filename
If you have a code that reads from Fortran unit 5 (the keyboard or stdin), you can
avoid re-typing the input from the terminal every time you run this code. To do
this, you create a file that contains this input and use < (less-than sign) to have your
program read from the file.
Example 11:
Suppose you create a file to contain your input and call it my.in.data. To run prgm
using the contents of this file as input instead of the keyboard, you would type:
prgm < my.in.data
Another useful form of I/O redirection is called the here document. A here
document is a form of input redirection that is generally used to redirect stdin from
within a shell script, although you may also use it in interactive mode.
The here document says to the shell “here is the input” and its general format is:
We suggest that you enclose the terminating_string in single quotes to prevent the
C shell from expanding any special characters that may be in command_input. The
command_input may contain any number of command lines.
The terminating string ends the here document input. It must begin in
Note the first column of a line and be the only characters on that line.
You can use a here document in combination with the cat command to create
separate files from within a shell script, as shown in the example that follows:
'EOF'
After you finish entering the command shown above, you will have a new file in
the current directory containing the four lines you typed in. (The blank lines are
also included.) The new file will be named new.file and it may now be used just
as any other UNIX file.
Example 13:
From within a script file, you might use a here document to set various parameters
for the computing job you are running. Doing this allows you to change the set of
parameter values by editing the script file, making the changes, and then
rerunning the job. This example assumes that a.out is reading from Fortran Unit 5:
.
.
.
a.out << 'EOF'
param1
param2
param3
'EOF'
.
.
.
To run the program a.out again using different parameter values, change and save
the shell script file and then rerun the job. This approach leaves no extra files
behind in your directory for you to clean up at the end of your computing session.
Standard error is different from standard output (even though they both default to
the screen), but you may redirect error messages (stderr) much as you redirect
stdout. However, there are some important differences in the way this operation is
handled by the three shells. The Bourne and Korn shells have similar mechanisms,
while the C shell uses a different methodology and different syntax.
C shell
In the C shell, >& (greater-than sign, ampersand) redirects both stderr and stdout
to a file.
Use the following syntax to redirect stdout and stderr to separate files:
(command > program_output) >& program_errors
This construct redirects the stdout from command to the file program_output and
redirects the stderr from command to the file program_errors.
For both the Bourne and Korn shells, use 2> (two, greater-than sign) to redirect
stderr (2) to a file.
Use the following syntax to redirect stdout and stderr to separate files:
command > program_output 2> program_errors
This construct redirects the stdout from command to the file program_output and
redirects the stderr from command to the file program_errors.
Example 14:
To redirect stdout to a file named out.data and redirect stderr to a file named
error.file:
C shell: (prgm > out.data) >& error.file
Bourne and Korn shells: prgm > out.data 2> error.file
You can use any combination of redirection techniques in any order on any
command line. Some commonly used redirection techniques are shown in this
section; note how the redirection symbols are used. In all of the following cases,
command can be any UNIX command, input_file stands for the name of the file
from which stdin is taken, output_file is the name of the file to which stdout is
directed, and error_file is the name of the file to which stderr is directed.
To make your program read its input from a file, then write its output to a file,
without requiring you to change any of the code in your program:
command < input_file > output_file
To separate stdout and stderr into two separate files:
(command > output_file) >& error_file
To redirect all three of the standard devices from or to files:
(command < input_file > output_file) >& error_file
To make your program read its input from a file, then write its output to a file,
without requiring you to change any of the code in your program:
command < input_file > output_file
To separate stdout and stderr into two separate files:
command > output_file 2> error_file
To redirect all three of the standard devices from or to files:
command < input_file > output_file 2> error_file
Example 17:
Reading stdin from a file named in.data, redirecting stderr to a file named
error.file, and redirecting stdout to a file named out.data:
C shell: (prgm < in.data > out.data) >& error.file
Bourne and Korn shells: prgm < in.data > out.data 2> error.file
You can connect two commands so that the output from the first becomes the input
of the next. Two or more commands connected in this way form a pipe. A pipe is
designated by a | (vertical bar) between the two commands.
command1 | command2
Almost all UNIX commands that use stdin and stdout can be used to form pipes.
You may combine several UNIX commands into a pipe where the final output is a
subset or manipulated version of the output from the first command.
Commands that perform some kind of manipulation on the output of the previous
command in the pipe are called filters. One of the most common uses of a filter is
to modify its input by filtering out unwanted items so that only the desired lines
are contained in the final output.
Example 18:
You want to view the many files listed in the current directory a screenful at a time.
ls -l | more
By piping the output of the ls -l command into more, you are able to see the output
of the ls -l command a single screenful at a time instead of having the output scroll
off your screen.
Example 19:
You want to list all the files that contain the string august in the filename. You want
the list to appear on your screen, a screenful at a time.
ls | grep august | more
The result of the first pipe is a list of files in the current working directory that
contain the pattern august in their filenames. (For more information on the grep
command, see its man page.) The second pipe will display the list one screenful at
a time.
The shell maintains information about you as a computer user and the resources
you use, such as directories, printers, mailbox, terminal, and so on. This
information is stored in what are known as environment variables. These variables,
in turn, are used by many of the UNIX utilities, such as e-mail, the print utilities,
and UNIX editors. In many cases, you can set an environment variable to hold
common options to a command you use frequently. This saves you from typing in
the options every time you use that particular command.
Environment variables are always available to UNIX commands. Whether a
particular command uses environment variables is determined by the developers
of the command. Check the man page for the command to see if environment
variables are used and what the valid values are.
If a UNIX utility (or you, yourself) starts another shell, environment variables are
available to that subshell, as well. However, any environment variable set in a
subshell or by a shell script affects only that current shell or future shells spawned
by the utility or the script. Changes do not affect the shell that started the subshell.
In the C shell, to view all the environment variables defined for your login session,
type:
env
To view the value of a single environment variable, use the echo command. Notice
that you must type a $ before the variable name when referring to its value.
echo $VAR
where VAR is the name of the variable.
Example 20:
Set the value of the VISUAL environment variable to be the path to the vi editor:
setenv VISUAL /usr/bin/vi
The VISUAL environment variable is used by several commands, such as more.
Example 21:
Example 22:
Command history
The C shell provides a feature known as command history, which allows you to re-
execute a command using just a few keystrokes. To use the history mechanism,
you must first complete the following steps:
set history
to your .cshrc file. By default, the C shell lists the last 24 commands you’ve
executed. If you’d like to change that number, add it to the set command:
set history = n
where n is the number of commands you want. For example, set history = 40
would mean your last 40 commands will be listed.
2. After you exit from the .cshrc file, type
source .cshrc
to make the change take effect.
There are several ways you can access the commands in your history list. First, you
can see the contents of your history list by typing:
history
The contents of your history list then appear on your screen; each command has a
number.
!str Re-executes the last command that begins with str, where str is a
string of characters that uniquely identifies the command
You want to use your history list to re-execute a command you have recently
typed. To see the history list, type:
history
Your screen may appear as shown below. (This, of course, depends on the
commands you have previously typed.)
% history
1 ls
2 mkdir test
3 cd test
4 mv $HOME/.login Login
5 more Login
6 history
%
Example 24:
You want to re-execute the more Login command line shown in the preceding
example. There are two ways to do this. You could type:
!mo
This causes the shell to search backwards through the history list until it finds the
most recent command that begins with the string mo. The shell then executes the
entire command line.
You could also re-execute this command by typing:
!5
In either case, the history list is updated to reflect the new execution of the more
command.
Example 25:
The C shell allows you to create short character strings that stand for long
command lines. This is commonly called aliasing, and it is performed with the alias
command. This is a very useful command because it lets you reduce the number
of keystrokes you use during your computing session.
To set an alias, use the following syntax:
alias string 'long command line'
The long command line usually contains blanks, so it is good practice to always
enclose long command line in single quotation marks; this ensures that the shell
interprets the blanks properly. Be sure to define string so it is both unique and easy
to relate to long command line.
To see the aliases already set in your shell, type alias without any arguments:
alias
You can pass command line options into the aliased command by using the
notation \!* (backslash, exclamation mark, asterisk). Here is the proper syntax:
alias string 'command options \!*'
To remove an alias from the shell, use the unalias command with the alias string
you want to remove:
unalias string
Be aware that the alias and unalias commands are only known to the C shell
during your login session, just like setenv and unsetenv. If you want to establish
aliases that work for all your UNIX sessions, include them in the .cshrc file in your
home directory.
You added the following command line in your .cshrc file to give you a long listing
for files in the current directory whenever you use the ls command:
alias ls 'ls -l'
To remove the alias and list only the filenames when you use ls, you would type:
unalias ls
Example 27:
It is useful to create an alias for the cd command that sets the shell prompt to be
the current directory. That way, you can always see on the screen where you are
currently working. The syntax is:
alias cd 'cd \!*; set prompt = "${cwd}: " '
Now when you execute the cd command, the C shell prompt is set to the current
working directory. If you want this alias available every time you log in to the
UNIX computer, place the above command in your .cshrc file.
Example 28:
You want to alias the cd command to set the shell prompt to display the current
working directory along with a command history counter. The command history
counter automatically increments with every command you enter. In addition, you
want the number to be enclosed in brackets:
alias cd 'cd \!*; set prompt = "${cwd}[\\!]: " '
If you want this alias available every time you log in to the UNIX computer, place
the above command in your .cshrc file.
When you execute a UNIX command or run a program interactively, you execute
in foreground mode. Your terminal waits for execution to complete, possibly
displaying the output of the command (and error messages if something went
wrong) and then displays the shell prompt. When the prompt returns to the screen,
it is your signal that the command has completed and that the shell can now accept
more input from your keyboard.
Sometimes, commands and programs take a long time to execute and you would
like to perform some other operation before the current task is completed. For
example, you might wish to read your mail while you also run a program that sorts
and prints a large data file. UNIX supports multitasking capabilities that allow a
single user to start up many tasks simultaneously. Because of this, UNIX supports
the concept of executing commands in background mode. That is, UNIX
concurrently completes the process you initiated “behind the scenes” while you
perform another operation at your terminal.
Background execution
When you type a UNIX command or invoke a program by typing its name on the
command line, you can tell UNIX to execute it in background mode by typing &
(ampersand) at the end of the command, before pressing RETURN:
command filename &
The & (ampersand) signals UNIX to run the command in background and return
the shell prompt to the terminal, thus allowing you to type another command.
When you put a job into background, UNIX displays some information about the
UNIX process you have initiated. You see a message on the screen like this:
[1] 818
%
This shows the number of processes you have in background (the number in
brackets) and the process identifier (PID—818 in this example) for your
background job.
Suppose you wish to bring a background job back into foreground mode. You
might wish to do this if the job is taking too long or an error message has displayed
that indicates a problem.
At the shell prompt, type:
fg
to bring a background job into foreground mode. This places you into interactive
foreground execution again, and you may proceed as if the job had never been
placed in background mode.
Suppose you started a process in foreground and decide that you needed to put
the job into background mode so you could continue to do work at the terminal.
To do so: (1) temporarily suspend execution of the process, and (2) type the UNIX
command to put the job into background mode.
In the C shell on most UNIX systems, do this by typing the following two lines:
CONTROL-z
bg
The CONTROL-z sequence suspends (but does not terminate) the execution of a
job. (If CONTROL-z does not suspend execution, check with your system
administrator for the correct key sequence.) The UNIX bg (for background)
command starts execution of the job again but places it into background mode and
then displays the shell prompt on the terminal screen. Your screen might look
something like this:
UNIX mail programs have some differences on various UNIX computers. This
chapter describes the Berkeley mail program. If you find that some things don’t
work as described here, consult the mail man page or check with your system
administrator.
1. To send an e-mail message to another user who has a login id on the same
UNIX computer as yours, at the shell prompt, type:
mail loginid
replacing loginid with the login id of the person to whom you are sending
mail. Press RETURN.
You can send the same mail to several users, all on your UNIX computer, by
listing their login ids on the command line. Typing:
mail user1_login user2_login user3_login
sends the same mail to all three users.
2. At the prompt, type the subject of your message; press RETURN and type your
mail message.
You may backspace to correct errors in the current line. To make corrections in
previous lines, you must invoke an editor from within the mail facility. You
will learn how to do this later in this chapter.
3. To send your message, press RETURN to position the cursor at the beginning
of a blank line after the last line of text and press CONTROL-d. Once you have
pressed CONTROL-d, your message has been sent and there is no recalling it.
Your only choice is to send another message clarifying the first one.
The recipient of your mail message does not have to be logged in at the time
you send the mail.
4. If you want to cancel a message you have started typing, press CONTROL-c
twice. Your message is canceled and you are returned to the shell prompt.
UNIX automatically saves what you typed in a file in your home directory
called dead.letter. To use this file as the basis of another mail message, you can
edit it and then use UNIX mail to send the file. (See the section “Sending a file
as a mail message,” later in this chapter.)
Example 1:
This example illustrates the keystrokes used to compose and send an e-mail
message:
% mail smith
Subject: Missing book
Have you seen my copy of "UNIX for People" anywhere?
Jane Jones
^d
%
% mail jones
Subject: no book
I haven’t seen your book and I ^c
(Interrupt -- one more to kill letter)
^c
%
Example 3:
If the person you’re sending mail to is not on the same computer where you are
logged in, you need to include a more detailed mail address. Under UNIX, the
general syntax for an e-mail address is
loginid@host.domain
where
Generally, the name after the @ sign and to the left of the dot is the name of your
recipient’s host computer; everything located to the right of the . (dot) is the domain
name, which is the location of that computer. For example, at NCAR, the name of
the host computer that runs the NCAR central post office is ncar, and NCAR’s
domain name is ucar.edu. The basic address syntax for sending e-mail to any
NCAR user, regardless of where you are logged in, is
loginid@ncar.ucar.edu
Example 4:
This example illustrates the correct syntax for sending mail to “jones”, a UCAR or
NCAR computer user:
mail jones@ncar.ucar.edu
E-mail etiquette
• E-mail is great for sending short messages and short files. However, it is not
designed for the transfer of long files and it cannot be used to transfer binary
files. NCAR currently imposes a size limit of 64,000 bytes on e-mail messages
going out or coming in, and other systems you use may also have a size limit.
• You should use file transfer protocol (FTP) rather than electronic mail to send
large files or noncharacter (binary) files. FTP excels at transferring data files
and large documents. It also allows the transfer of binary files.
• Add information in the Subject: field of your messages to others; it helps those
receiving your mail as they view the header information in their mailbox. And,
cover only one subject in a message; it’s easier to forward and sort mail
messages that way.
• Remember that mail is not secure. Don’t send a message or document through
e-mail that is private or sensitive.
If you type mail but you have no mail messages, mail displays the
Note message “No mail for loginid,”and returns the shell prompt.
Example 5:
To read the message from smith, type 2 at the & prompt and press RETURN. The
message displays on your screen, with the & prompt again at the bottom, ready for
your next mail command:
% mail
>U 1 green@niwot.ucar.EDU Fri Aug 1 08:43 12/480 solution to pr
N 2 smith Fri Aug 1 1216 13/483 missing book
& 2
I think I took your book home. When do you need it back? Steve Smith
&
For messages that are longer than one screen, press the SPACE BAR to display the
next screenful. (There is no way to move back up through a mail message unless
you invoke the editor. See the v command on the mail man page if you want to
learn how to do this.)
To send a reply to a mail message without having to enter the person’s address and
the subject, on most systems with the Berkeley UNIX mailer, at the & prompt,
type r. The mail program automatically completes the header information for you.
Then type your message and use CONTROL-d to send it as usual.
Example 6:
This message will be sent to the user who mailed the original message.
& r
Subject: missing book
I’d like it back on Mon. Thanks. JJ
^d
%
You can access a limited help screen while you are using mail to read your
messages. At the & prompt, type:
help
or
?
The mail program displays a list of mail subcommands and a brief description of
each.
When you are using mail to send a message, you may access help for using the
~ (tilde) commands by typing:
~?
at the beginning of a line in the message area. The complete list of tilde commands
displays with a brief explanation of each. Even though the tilde commands list is
still displayed on your screen, it will not appear in the mail you’re sending. Just
continue entering your message as usual.
There is also a man page for mail. Be sure you are back at the shell prompt, then
type:
man mail
To leave the mail program and return to the shell prompt, at the & prompt, type:
q
which stands for “quit.” The shell prompt is then displayed.
Example 7:
Deleting messages
If you want to delete some e-mail messages, you can do so at the & prompt by
typing:
d message_number message_number . . .
Be sure to separate the message numbers by spaces. If you type d and no number,
the message marked by the > in the message headers list is marked for deletion.
The messages are actually deleted when you quit the mail program by typing q.
Undeleting messages
If you are still in the mail program (you haven’t yet quit mail with the q command)
there are two ways to undelete messages:
Once you have deleted mail and quit the mail program using the
Caution q command, there is no way to recover deleted mail.
1. You can forward a single message in your mailbox to another person or group
of people.
2. You can automatically forward your own mail from one UNIX computer to
another.
Interpolating: message_number
(continue)
You may forward all your mail automatically from one computer to another by
creating a file called .forward in your home directory. (Be sure to include the dot
as the first character of the filename.) This file must contain only your e-mail
address for the computer where you want to receive your mail. All your UNIX
accounts, except the account where you want to receive mail, should have a line
like this in their .forward files:
yourlogin@host.domain
To verify the existence of your .forward file, use the -a option of the ls command
to list the dot files.
Use .forward files with care! Be careful to forward all your mail to
Caution only one computer. If you accidently forward you mail in circles
among two or more computers, it will probably cause an infinite
loop and possible loss of all your incoming mail.
Example 8:
The following .forward file is in the “jones” home directory on the computer
named meeker.ucar.edu:
jones@ncar.ucar.edu
All mail addressed to jones@meeker.ucar.edu will be forwarded to
jones@ncar.ucar.edu.
You can use UNIX e-mail to send someone an existing file, provided it meets the
size limits that may be imposed by any of the computers that must pass the file to
its final destination. For example, currently at NCAR, files larger than 64,000 bytes
will be rejected by the mail program. You may send only ASCII files in e-mail.
Also, UNIX e-mail does not have the capability of handling binary file format.
There are two ways to mail someone a text file.
1. At the shell prompt, you can redirect a file to be the input for mail. This
method has the advantage of being quick, but it does not allow you to type an
additional message. To use the redirection method, you should use the -s
option to add a Subject: header that tells the recipient what it is that you are
sending:
mail -s 'subject in single quotes' loginid@host.domain < filename
2. The second method allows you to include your own message along with the
file. In this case, enter your personal message as you normally do. Then, to
include the file, press RETURN and, on the new line, type:
~r filename
where the ~ (tilde) is the first character on the line. This inserts your file at this
point in your mail message. You can then either enter some concluding lines, or
you can press CONTROL-d at the beginning of a new line to send the mail.
Example 9:
Here is an example of writing your own message, including a file, then adding a
concluding message before sending the mail:
% mail smith
Subject: Missing book
Have you seen my copy of "UNIX for People" anywhere? And here is a list of
other UNIX books you might like to see:
~r unix.books
"unix.books" 3/20
Let me know ASAP if you find my book
Jane Jones
^d
%
If you want to edit your mail message before sending it, you can use the UNIX vi
editor while you are still writing your message. This is particularly handy to check
that you did the right thing with ~r to include a file in your mail. Just press
RETURN and type:
~v
where the ~ (tilde) is the first character on the line. This puts you into the vi editor
so you can now use all the standard vi editing commands. After you are finished
with your message, leave vi by typing :wq. Press CONTROL-d at the beginning of
a line to send your message.
To assure that the ~v option invokes the vi editor in mail, we recommend that two
environment variables be set in your .login file:
setenv EDITOR /usr/ucb/vi
setenv VISUAL /usr/ucb/vi
You may want to edit a mail header before you send your mail; for example, you
might wish to add a name to the list of recipients or alter the wording in the
Subject: line. The following command can be particularly handy used in
combination with the r or R command to reply to a message. Like other ~ (tilde)
commands, you must first be in the e-mail program and in the process of sending
a message. Then, to change the Subject: line or add another address to the list of
users receiving your message, just enter:
~h
at the beginning of a new line. A mail prompt displays:
To:
1. To add people to the list, press the SPACE BAR and then type the additional
e-mail addresses. Use the DELETE key to erase any addresses you don’t want.
Be sure to put a space between every e-mail address you type.
2. Press RETURN to display the Subject: header. To alter the subject header, use
the DELETE key to erase the original header. Type in the new subject.
3. Press RETURN to display the cc: (carbon copy) header. Use the DELETE key
to erase addresses you don’t want and the type in new addresses, leaving a
space between each address.
4. Press RETURN to display the Bcc: (blind carbon copy) header. Use the
DELETE key to erase addresses you don’t want and the type in new addresses,
leaving a space between each address. (Recipients of “blind copies” receive the
mail, but the mail header that goes to others on the list won’t include anyone
you mailed to using Bcc:.) Press RETURN when you are done.
5. To leave any header field unchanged, press RETURN to bypass it.
6. After the header fields have been edited, the screen displays this message:
(continue)
7. Type your message and press CONTROL-d at the beginning of a new line to
send it.
Many UNIX systems provide the vacation command, which provides a way to
inform senders of incoming mail that you are away for an extended period and
will not be reading their message immediately. To see if the UNIX system you are
using supports this feature, type:
man vacation
at the shell prompt. To use vacation, follow the instructions on the man page.
To make addressing mail easier, you can create alias lines in your .mailrc file in
your home directory. These lines contain a short name (known as an alias) and the
complete address of the recipient of your mail. The basic structure of an alias
command is:
alias short_name long_e-mail_address
You can also create a single alias to send mail to a list of people.
It is also possible to put a group alias in the system alias file, which is located in the
/usr/lib directory, so that more than one person can send messages to the whole
group. This can be very useful to a group of people working on a project together.
The system alias file /usr/lib/aliases can be changed only by your system
postmaster or by your system administrator; contact them for procedures.
Example 10:
The \ (backslashes) at the end of lines are escape characters so that the list may be
typed on more than one line.
Example 11:
If you were to create the above aliases in your .mailrc file, you could send mail to
your colleague Phil in Toronto on BITNET by typing:
mail phil
and proceed as if Phil were a user on your home computer.
Example 12:
If you wanted to send a message to all the people on list1, it would be like sending
mail to one person:
mail list1
The .mailrc mail initialization file sets options that are used every time you type
the mail command. The .mailrc file must be located in your home directory.
Listed below are some of the popular set commands you may want to include in
your .mailrc file:
set crt=22 Breaks incoming messages into 22 lines so that long messages
will not scroll off the screen before you can read them
set ask Provides the Subject: line if it is not already a system default
set hold Saves incoming mail in your system mailbox so that all messages
are accessible by the mail command until you delete or save them
set metoo Allows mail to be sent to “you, too” when your own login is
included in a list of users to whom you are mailing
set askcc Provides the cc: line (“carbon copy”) at the end of the mail
message
set askbc Provides the Bcc: line (“blind carbon copy”) at the end of the
mail message
There are several other set commands available; these are listed in the mail man
page as well as in the “Mail Reference Manual,” which is included in UNIX User’s
Manual: Supplementary Documents (4.2 Berkeley Software Distribution).
from: MAILER-DAEMON@hostname
The returned mail message includes the original e-mail message you tried to send.
Any mail system along the route (the hostname) may have generated the error
message. Therefore, the error message may be somewhat different from the ones
shown on the table below, depending on which system returned it.
If you encounter problems you cannot solve, contact your system administrator or
the designated e-mail specialist at your site.
Cannot send for xx days No connection could be made to Contact system admin. or
remote host. Poor connection or e-mail specialist.
host is down.
Bad file number Remote host closed connection Contact system admin. or
before message transferred. Poor e-mail specialist.
connection or host went down dur-
ing transaction.
Part of this page was adapted from “Unix Mail Utility,” with permission from Woods Hole Oceanographic Institution.
June 1992, Woods Hole Oceanographic Institution.
Recommended
UNIX books
Recommended UNIX books
Beginner
Learning the UNIX Operating System (A Nutshell Handbook), Todino, Grace, and
John Strang. O’Reilly & Associates, Inc., 1989. ISBN 0-937175-16-1 ($9.00).
NCAR Library call no. QA76.8 U65T4. This short handbook teaches the basic
system utility commands to get you started with UNIX (AT&T’s System V ver-
sion). Topics have been carefully chosen so the new user is not overwhelmed
with unnecessary details. The handbook concentrates on the most basic and
useful features of a command so that the user can feel comfortable as soon as
possible in the UNIX environment.
O’Reilly and Associates produces a series of Nutshell Handbooks
Note
covering numerous UNIX topics. Each handbook is an extended essay
on a particular area with a lot of examples, help, and reference material.
Life with UNIX: A Guide for Everyone, Libes, Don, and Sandy Ressler. Prentice-Hall,
Inc., 1989. ISBN 0-13-5366577 ($30.95 paper). Comprehensive treatise of what
UNIX is all about, from technical, market, and historical orientations. Provides
analysis of UNIX from three different viewpoints—user, programmer, and ad-
ministrator.
UNIX for People: A Modular Guide to the UNIX Operating System: Visual Editing, Doc-
ument Preparation, and Other Resources. Birns, Peter, Patrick Brown, and John
Muster. Prentice Hall, Inc., 1985. ISBN 0-139374-42-6 ($37.95 paper). A nonin-
timidating “hands-on” guide to the UNIX operating system that assumes no
previous experience with UNIX or computing in general.
UNIX Primer Plus, Waite, Mitchell, Donald Martin, and Stephen Prata. Howard W.
Sams and Company, 1983. ISBN 0-672-22028-8 ($22.95). NCAR Library call no.
QA76.6.W3185. An easy-to-read tutorial introduction to UNIX.
The UNIX Programming Environment, Kernighan, Brian W., and Robert Pike. Pren-
tice-Hall, Inc., 1984. ISBN 0-13-937699-2 ($40.00 hardcover). NCAR Library call
no. QA76.6.K495. A nicely done book on the UNIX program development en-
vironment and philosophy that ties together the fundamental concepts of
UNIX programming. Talks about UNIX from the user’s perspective and intro-
duces how-to tools effectively. Recommended for developers or users who
want to know a lot about UNIX and how to use it effectively. An excellent book
for the beginning UNIX system programmer.
Advanced
Shell programming
The UNIX C Shell Field Guide, Anderson, Gail, and Paul Anderson. Prentice-Hall,
Inc., 1986. ISBN 0-13-937468-X ($34.95). NCAR Library call no.
QA76.76.063A53. Entirely devoted to the C shell, this book tells you everything
you ever wanted to know about C shell programming.
UNIX Shell Programming, Kochan, Stephen G., and Patrick Wood. Hayden Books,
1985. ISBN 0-8104-6309-1 ($24.95). NCAR Library call no. QA76.76.063K64.
This book is all about shell programming. It concentrates on the Bourne shell,
includes chapters on the Korn shell, and briefly covers the C shell.
The vi editor
vi—The UNIX Screen Editor, Hansen, August. Prentice Hall Press, 1986.
ISBN 0-89303-928-4, ($21.95). NCAR Library call no. QA76.76.T49H36. A well-
organized description of vi, the interactive text editor on UNIX systems. Con-
tains a complete vi reference guide.
Visual Editing on UNIX, Srinivasan, B., and K. Ranai. World Scientific Publishing
Co., Inc., 1989. ISBN 9-971507-70-6, ($44.00). NCAR Library call no.
QA76.76.T49S68. A complete description of vi, the interactive text editor on
UNIX systems. Contains numerous screen examples and a quick reference
guide.
A Guide to vi: Visual Editing on the UNIX System, Sonnenschein, Dan. Prentice-Hall,
1987. ISBN 0-13-371311-3 025, ($21.95). NCAR Library call no. QA76.76.T49966.
A complete description of vi, the interactive text editor on UNIX systems. Con-
tains a vi reference guide.
Learning the vi Editor, Lamb, Linda. O’Reilly & Associates, Inc., 1990. ISBN
0-937175-67-6 ($21.95). NCAR Library call no. QA76.76.063L355. A get-started-
quickly handbook that graduates to more advanced editing skills.
These manuals by the AT&T staff are still the best place to find out what UNIX
commands do, but you must first master how to read the AT&T manual page
(known as the “man page”).
UNIX Programmer’s Manual, Vol. 1, Holt, Rinehart, and Winston, 1986. ISBN 0-03-
009317-1 ($28.95 paper).
UNIX Programmer’s Manual, Vol. 2, Holt, Rinehart, and Winston, 1986. ISBN 0-03-
009314-7 ($28.95 paper).
UNIX Programmer’s Manual, Vol. 3, Holt, Rinehart, and Winston, 1986. ISBN 0-03-
009313-9 ($19.95 paper).
UNIX Programmer’s Manual, Vol. 4, Holt, Rinehart, and Winston, 1986. ISBN 0-317-
47172-4 ($28.95 paper).
UNIX Programmer’s Manual, Vol. 5, Holt, Rinehart, and Winston, 1986. ISBN 0-317-
47175-9 ($37.95 paper).
F I
file command, 42 I/O (Input/Output)
Files redirection of, 89–94
copying, 27 terms defined, 87
deleting, 29 Insert mode, in vi, 51
dot, 19
filename expansion, 83
linking, 36
listing, 22
K
moving, 28 Kernel, 4
naming conventions, 17 kill command, 42
organization of, 17–20 Korn shell, 67, 77–103
pathnames, 18
permissions, 30–32
protecting, 16, 30–32
remote copying, 37 L
removing, 29
less command, 25
renaming, 28
ln command, 32, 36
searching, 35
Logging in, 12
transferring, 37–41
Logging out, 12
viewing, 25–26
Login shell, 65
Filters, 95
ls command, 22
find command, 42
finger command, 16
T
tail command, 42
Tilde (~), in C shell, 19
U
umask command, 31
UNICOS, 3
UNIX commands. See Commands
UNIX directories. See Directories
UNIX files. See Files
unsetenv command, in C shell, 96
Utilities, 4
V
vacation command, in e-mail, 119
vi editor commands, 45–61
books about, 127
command mode, 48
insert mode, 48, 51
quick reference, 45–46
W
wc command, 42
who command, 42
Wildcard characters, 83
The SCD Documentation Catalog and order form are available online via
anonymous FTP on the computer named ftp.ucar.edu. To obtain a copy of the files,
follow the steps below. Press RETURN after typing each line:
ftp ftp.ucar.edu
or
ftp 128.117.64.4
anonymous
Note: If your local computer is a Digital Equipment VAX running VMS, you may
need to type:
"anonymous"
Enter your last name at the password prompt, wait for the ftp> prompt, then type:
(Replace local_filename in the command above with a name that is unique to your
current working directory to ensure that you do not overwrite an existing file.)
N
S
NCAR users, information for, 6
Security, of accounts, 16
setenv command, in C shell, 96
O Shells, 4, 65–103
aliasing commands in C shell, 100
Online help, 14 changing for single session, 67
Operating system changing the default, 68
diagram of, 5 compared, 69–70
overview of, 3–5 filters, 95
history in C shell, 98
login, 65
P pipes, 95
prompt, 65
Parent directory, 19, 34 quick reference, 77
passwd command, 16 redirecting stderr (standard error), 92
Passwords, of files, 16 redirecting stdin (standard
Pathnames, 18 output), 90
pg command, 26 redirecting stdout (standard
Pipes, 95 output), 89
Processes, 4 redirection combined, 93
ps command, 42 scripts, 79–82
put command, in FTP, 39 subshells, 68
pwd command, 21 Shells, books about, 126
Shells. See also C shell; Bourne shell; Korn
shell<$nopage>, 65
Q Start-up files. See Files: initialization
status variable, in C shell, 82
Quick reference stderr (standard error), 87
C shell, 78 redirection of, 92
commands, 11, 42 stdin (standard input), 87
e-mail, 107 redirection of, 90
shells, 77 stdout (standard output), 87
vi editor, 45–46 redirection of, 89
Subshells, 68
T
tail command, 42
Tilde (~), in C shell, 19
U
umask command, 31
UNICOS, 3
UNIX commands. See Commands
UNIX directories. See Directories
UNIX files. See Files
unsetenv command, in C shell, 96
Utilities, 4
V
vacationcommand, in e-mail, 119
vi editor commands, 45–61
books about, 127
command mode, 48
insert mode, 48, 51
quick reference, 45–46
W
wc command, 42
who command, 42
Wildcard characters, 83