Sie sind auf Seite 1von 148

The UserDoc Collection

UNIX

Basic UNIX Guide

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

SCD provides documents covering specific topics in these categories

• Applications software • Networking and data


communications
• Cray computers
• Output services (Text and
• Graphics Graphics System—TAGS)

• Introduction to the SCD • UNIX


computing environment
• Additional topics
• Mass Storage System

Ordering information appears on the last page of this document.

© Copyright 1993 University Corporation for Atmospheric Research (UCAR)


All Rights Reserved

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

ii Basic UNIX Guide


Conventions used in this document

UNIX

• Command names, options, filenames, pathnames, directories, and other items


that must be typed as shown are printed in bold letters. Examples:

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

At the prompt, type your login_id.

• The \ (backslash) followed by pressing RETURN when you are entering a


command allows you to continue the command on the next line. In UNIX, a \
escapes the next character.

General

• Fortran programs, shell scripts, and screen displays are shown in Courier
(“typewriter”) font.

CALL FLMHDR (35)

• You must press RETURN after each command, but the RETURN is not shown
in the document, except when required for clarity.

• Keystrokes that must be pressed simultaneously are linked by a hyphen, with


the modifier key indicated by capital letters before the hyphen. Example:

To send your e-mail, enter CONTROL-d at the beginning of a new line.

Version 1.0, August 1993 iii


Contents

Conventions used in this document iii

Chapter 1: The UNIX computing environment ..............................................1


Brief history of UNIX 3

Overview of the UNIX operating system 4


The UNIX kernel 4
The UNIX utilities 4
The UNIX shell 4
Figure 1: UNIX operating system 5

Learning more about UNIX 6


Classes 6
Books and local documentation 6
Online documentation 6
For users at NCAR only 6

Chapter 2: Useful UNIX commands .......................................................................7

Table 1: Basic UNIX commands 11

Logging in and logging out 12


Logging in 12
Logging out 12

UNIX command syntax 13

Getting online help with man 14

Changing your password with passwd 16


Account security 16

The UNIX file system 17


Figure 1: The hierarchical file system 17
UNIX filenaming conventions 17
Using pathnames 18
System file organization 19

Version 1.0, August 1993 v


Determining your current directory with pwd 21

Listing the contents of a directory with ls 22

Changing directories with cd 24


Figure 2: The file structure of the jones home directory 24

Looking at a file with more 25


Table 2: more subcommands 25

Looking at a file with pg 26


Table 3: pg subcommands 26

Copying files with cp 27


Figure 3: The file structure showing the new file test1.f 27

Renaming files and directories with mv 28


Figure 4: The file structure after test.f is moved to directory project2 28

Removing files with rm 29

File protection and permissions 30


Figure 5: Sample file permission fields 30
Table 4: File and directory permissions 31
Default permissions 31

Changing file permissions with chmod 32

Creating new directories with mkdir 33


Figure 6: The file structure with a new directory project3 33

Removing directories with rmdir 34


Figure 7: The file structure without the directory project2 34

Searching contents of files with grep 35

Multiple users accessing files with ln 36


Figure 8: The file structure showing the link to smith’s
file named data 36

Transferring files with rcp 37


When login ids are different 37

Transferring files with ftp 39


Opening an FTP connection 39
Transferring files 39

vi Basic UNIX Guide


Additional ftp commands 40
Using anonymous FTP accounts 40

Additional UNIX commands 42


Table 5: Additional UNIX commands 42

Chapter 3: Basic vi editor commands..................................................................43

Table 1: Basic vi commands 45

Advantages of the vi editor 47


Basic vi rules 47

Invoking the vi editor 48

Exiting the editor 49

Paging and moving around in the file 50

Inserting text 51

Deleting text 52

Replacing and changing text 54

Copying and moving text within a file 56

Copying and moving text between files 58

Searching text 60
Changing your vi editor environment 61

Chapter 4: Getting started with UNIX shells .................................................63


What is a shell? 65
What the shell does for you 65

The three common UNIX shells 66


The Bourne shell 66
The C shell 66
The Korn shell 67
Changing your UNIX shell 67

Version 1.0, August 1993 vii


What is a subshell? 68
Table 1: Features of the different shells 69

Shell initialization files 71


Bourne and Korn shell initialization files 71
C shell initialization files 72

Chapter 5: Useful shell features .............................................................................75


Table 1: Shell features 77
Table 2: Some C shell commands 78

What is a shell script? 79


Shell constructs 81

Filename expansion 83

The UNIX escape character 85

I/O redirection terminology 87


Standard I/O terminology 87

Redirection of standard output (stdout) 89


Append output redirection 89

Redirection of standard input (stdin) 90


The here document 90

Redirection of errors (stderr) 92


C shell 92
Bourne and Korn shells 92

Combining redirection techniques 93


Combining I/O redirection in the C shell 93
Combining I/O redirection in the Bourne and Korn shells 93

Pipes and filters 95

Environment variables 96
Setting an environment variable 96
Removing an environment variable 96
Viewing environment variables 97

Re-executing previous commands 98


Command history 98
Re-executing a selected command 98

viii Basic UNIX Guide


Aliasing commands 100

Managing your jobs in the UNIX environment 102


Background execution 102
Changing from background to foreground execution 103
Changing from foreground to background execution 103

Chapter 6: Using UNIX e-mail...............................................................................105


Getting started with e-mail 107
Table 1: Basic mail commands 107
Table 2: Some ~ (tilde) commands 107

Sending e-mail 108


Addressing your e-mail message 109
E-mail etiquette 110

Reading your e-mail 111

Sending a reply 112

Getting help 113

Quitting the e-mail program 113

Saving messages 114

Deleting messages 114


Undeleting messages 114

Forwarding mail 115


Forwarding to another user 115
Forwarding to yourself on another computer 116

Sending a file as a mail message 117

Editing a mail message before sending it 118


Environment variables for editing mail 118
Editing mail headers 118

Sending e-mail responses when you go on vacation 119

Creating distribution lists and address aliases 120

Customizing your e-mail environment 121

Version 1.0, August 1993 ix


E-mail error messages 122
Table 3: Possible e-mail error messages 122

Appendix A: Recommended UNIX books .....................................................123

Index ........................................................................................................................................129

SCD documentation and consulting .....................................................................133

x Basic UNIX Guide


Chapter 1

The UNIX
computing environment
Contents

Brief history of UNIX 3

Overview of the UNIX operating system 4


The UNIX kernel 4
The UNIX utilities 4
The UNIX shell 4
Figure 1: UNIX operating system 5

Learning more about UNIX 6


Classes 6
Books and local documentation 6
Online documentation 6
For users at NCAR only 6

2 Computing environment Basic UNIX Guide


The UNIX computing environment

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.

Brief history of UNIX

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.

Version 1.0, August 1993 Computing environment 3


Overview of the UNIX operating system

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.

The UNIX kernel

UNIX is an interactive, multitasking, multi-user system. The kernel is at the heart


of the UNIX operating system and is the component that schedules tasks and
manages data storage. The kernel manages the computer resources by keeping
track of disks, tapes, printers, terminals, communication lines, and other devices.
Tasks running on a UNIX computer are called processes. UNIX users may run many
processes at once; usually there is a system-imposed limit on the number. Because
UNIX is also interactive, tasks you initiate run synchronously in foreground mode;
control is returned to your terminal only after the current task has completed.
However, you may run processes in background mode, as well, which allows you
to make maximum concurrent use of many computer resources, without waiting
for completion of any single task.

The UNIX utilities

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.

The UNIX shell

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.

4 Computing environment Basic UNIX Guide


Figure 1. UNIX operating system

Shells

Utilities

Kernel

Hardware

Version 1.0, August 1993 Computing environment 5


Learning more about UNIX

Classes

Because UNIX is a popular operating system, there may be introductory and


advanced UNIX classes in your community. Ask about classes at the campus
computing center, the continuing education division of a university, the local
school district’s adult education program, and other local adult education or
computer programs.

Books and local documentation

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.

For users at NCAR only

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.

6 Computing environment Basic UNIX Guide


Chapter 2

Useful UNIX commands


Contents

Table 1: Basic UNIX commands 11

Logging in and logging out 12


Logging in 12
Logging out 12

UNIX command syntax 13

Getting online help with man 14

Changing your password with passwd 16


Account security 16

The UNIX file system 17


Figure 1: The hierarchical file system 17
UNIX filenaming conventions 17
Using pathnames 18
Absolute pathnames 18
Relative pathnames 18
The “dot” files 19
The tilde in the C shell 19
System file organization 19
Command directories 19
Home directories for users 20
Library directories 20
System directories 20

Determining your current directory with pwd 21

Listing the contents of a directory with ls 22

Changing directories with cd 24


Figure 2: The file structure of the jones home directory 24

Looking at a file with more 25


Table 2: more subcommands 25

Looking at a file with pg 26


Table 3: pg subcommands 26

Copying files with cp 27


Figure 3: The file structure showing the new file test1.f 27

8 Useful UNIX commands Basic UNIX Guide


Renaming files and directories with mv 28
Figure 4: The file structure after test.f is moved to directory project2 28

Removing files with rm 29

File protection and permissions 30


Figure 5: Sample file permission fields 30
Table 4: File and directory permissions 31
Default permissions 31

Changing file permissions with chmod 32

Creating new directories with mkdir 33


Figure 6: The file structure with a new directory project3 33

Removing directories with rmdir 34


Figure 7: The file structure without the directory project2 34

Searching contents of files with grep 35

Multiple users accessing files with ln 36


Figure 8: The file structure showing the link to smith’s file
named data 36

Transferring files with rcp 37


When login ids are different 37

Transferring files with ftp 39


Opening an FTP connection 39
Transferring files 39
Transferring binary files 40
Additional ftp commands 40
Using anonymous FTP accounts 40

Additional UNIX commands 42


Table 5: Additional UNIX commands 42

Version 1.0, August 1993 Useful UNIX commands 9


Useful UNIX commands

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.

Table 1: Basic UNIX commands

Command Description Example


man Finds manual information by keyword man passwd
passwd Changes your password passwd
pwd Lists name of present working directory pwd
ls Lists the contents of your directory ls -l
cd Changes directory cd project4
more Displays contents of a file on screen, one more file3
screenful at a time
pg Displays contents of a file on screen, one pg file3
screenful at a time
cp Copies contents of a file to a new file cp test.f nutest.f
mv Moves or renames a file mv myfile newname
rm Removes a file rm file2
chmod Changes permissions of file or directory chmod ug+x data
mkdir Makes a new directory mkdir project4
rmdir Removes a directory that does not con- rmdir project4
tain any files
grep Searches lines of input file(s) for match- grep "text_string" file4
ing pattern
ln Allows multiple users access to a single ln master_filename my_name
file; lists file in users’ directory displays
rcp Copies files to or from a remote rcp hostname:pathname new
computer rcp pathname hostname:new
ftp Transfers files between computers on the ftp remote_computer
Internet

Version 1.0, August 1993 Useful UNIX commands 11


Logging in and logging out

Ask your system administrator for instructions on connecting to the UNIX


computer, since connectivity is site dependent. Once you are connected, follow the
instructions below for logging in and logging out.

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

If you type either your login id or your password incorrectly, press


CONTROL-u to erase the line and start again. (To execute a CONTROL-u,
hold down the CONTROL key and press the u key.)
3. You are then presented with any messages of the day and information about
the status of your mailbox, followed by a shell prompt. A frequently used shell
prompt is % (percent sign), which we use in the examples in this manual.
When you see the shell prompt, you are logged in and can proceed with
computing tasks.

The shell prompt on your system may be something other than %.


4. To protect the security of your account, change your password the first time
you log in to any UNIX system. To do so, use the passwd command, which is
described later in this chapter.

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:

1. Type the logout command. Or


2. Type the exit command. Or
3. Press the CONTROL-d keys simultaneously.

12 Useful UNIX commands Basic UNIX Guide


UNIX command syntax

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:

command options filenames


• Commands are usually in lowercase. Options usually start with a - (hyphen or
minus sign), followed by a letter or a word that is case-specific, and sometimes
also followed by a value. This is only the general structure for most UNIX
commands; there are exceptions!
• Commands, options, and filenames are separated by any number of blanks
within a command line.

Example 1:

Here is a command line that shows a common UNIX syntax of a command,


followed by an option, then followed by a filename:
ls -l .login
If you type this command, you will find that the ls command with the -l option
displays information about your .login initialization file.

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.

Version 1.0, August 1993 Useful UNIX commands 13


Getting online help with man

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.

14 Useful UNIX commands Basic UNIX Guide


Example 3:

To see the manual page for the man command, type:


man man
Here is the first part of the man page you will see.

% 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:

chmod(1) - Changes mode of files or directories


cleantmp(1M) - Deletes job temporary directories
cpset(1M) - Installs object files in binary directories
dircmp(1) - Compares directories
exportfs(1M) - Export and unexport directories to NFS clients
exports(4F) - Directories to export to NFS clients
fsoffload(1M) - Lists files and directories on a logical device
rm(1) - Removes files or directories
rm(1) (see rm(1)) - Removes files or directories
rmdir(1) (see rm(1)) - Removes files or directories
spwcard(1M) - Sets wildcard levels on system directories in a secure
environmentxtab(4F) (see exports(4F)) - Directories to export to NFS clients

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.

Version 1.0, August 1993 Useful UNIX commands 15


Changing your password with passwd

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

Good account security is your responsibility. If an unauthorized user guesses your


password, your own files are in danger of being modified or deleted. We
recommend that you use a different password for each system you log into and
that you change your password at least twice a year. Here are some guidelines for
choosing a good password that cannot be easily broken manually or by password-
breaking programs.
1. Use a password of six to eight characters and at least one numeral or special
character.
2. Don’t write your password down. It’s better to pick an easy one that you can
remember than provide a written key to your account.
3. Don’t tell others your password.
4. Avoid using proper names, especially those associated with yourself or your
family; this includes rearranging the letters of your login id or other personal
data. The UNIX finger command provides some information about you; try
out this command and don’t use information that can be obtained this way.
5. Avoid words found in online dictionaries, both English and foreign.
Password-breaking programs frequently use online dictionaries to try to break
users’ passwords.
6. Don’t use your street address or phone number. Even though it uses numbers,
it’s easy to get this information from public sources, such as a telephone book.

16 Useful UNIX commands Basic UNIX Guide


The UNIX file system

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.

Figure 1. The hierarchical file system

/ root directory

...
u1 u0 bin

home directories
... ...
jones smith doc

project1 jobs file1

test.f text.c ...

UNIX filenaming conventions

UNIX filenames and directory names may be up to 255 characters in length.


However, some versions of UNIX allow a maximum of 14 characters. Therefore,
we recommend that you limit your filenames to no more than 14 characters.
UNIX is case-sensitive; the uppercase letter A is interpreted as a different character
from the lowercase letter a. Almost any set of characters is permissible, but you

Version 1.0, August 1993 Useful UNIX commands 17


should note that several characters, such as the $ (dollar sign) and the " (double
quotation mark) are special shell characters. Therefore, we recommend that you
use filenames containing just letters (uppercase or lowercase), numbers,
the . (period), and the _ (underscore).

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

18 Useful UNIX commands Basic UNIX Guide


The “dot” files

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 tilde in the C shell

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

System file organization

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

Version 1.0, August 1993 Useful UNIX commands 19


contains local commands that probably aren’t available on other UNIX computers.
For example, commands to access local printers or other site-specific equipment
are generally located in this directory.

Home directories for users

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.

20 Useful UNIX commands Basic UNIX Guide


Determining your current directory with pwd

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

Version 1.0, August 1993 Useful UNIX commands 21


Listing the contents of a directory with ls

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:

-l Produces a “long” listing for each file (and subdirectory) in this


directory. This listing includes the total number of files in the
directory, each file’s permissions, owner, size, date and time of
last modification, and filename.

-a Includes in its listing all files that begin with . (dot). By default, ls
ignores files that begin with a dot.

-F Adds a character to the filename signifying the type of file: a


trailing / (slash) indicates that the file is a subdirectory; a trailing
* (asterisk) indicates that the file is executable (see the examples
on the next page). Note that the characters are not a part of the
file’s name; they are displayed so you can easily identify various
types of files in your directories.
If you do not specify a filename on the command line, ls lists all the files in the
current directory.

Example 7:

To get a simple list of the contents of the current directory:

% 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/
%

22 Useful UNIX commands Basic UNIX Guide


Example 9:

To list the dot files in the current directory:

% ls -a
. .login file1 rje
.. .ntwkparms project1
.cshrc bin project2
%

Example 10:

To get a “long” listing of the contents of the current directory:

% 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

Version 1.0, August 1993 Useful UNIX commands 23


Changing directories with cd

To change to another directory, use the cd command:


cd pathname
This places you in the directory indicated by pathname. Pathnames can be relative
or absolute. See “The UNIX file system” earlier in this chapter for a complete
discussion of pathnames.
If you type the cd command without a pathname, you will be returned to your
home directory, regardless of your current location.

Example 11:

If jones is your home directory, to move into the project2 subdirectory, type:
cd project2
The current working directory is now project2.

Figure 2. The file structure of the jones home directory

jones

project1 project2 file1

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

24 Useful UNIX commands Basic UNIX Guide


Looking at a file with more

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.

Table 2: 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:

To look at the contents of a file named file1:


more file1
The contents of file1 are displayed on the screen a page at a time.

Version 1.0, August 1993 Useful UNIX commands 25


Looking at a file with pg

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

26 Useful UNIX commands Basic UNIX Guide


Copying files with cp

To make a copy of a file, use the cp command:


cp old_pathname new_pathname
Be sure to use a unique filename. If the new filename matches the name of an
existing file in the same directory, UNIX will replace the existing file with the new
file.
When a file is copied, the original file remains unchanged. The new file will be
owned by you and will have the current date/time stamp as its time of origin.

Example 15:

In the project1 directory, to make a copy of test.f and name it test1.f, type:
cp test.f test1.f

Figure 3. The file structure showing the new file test1.f

jones

project1 project2 file1

test.f text.c test1.f

Version 1.0, August 1993 Useful UNIX commands 27


Renaming files and directories with mv

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:

In the current working directory, to rename file1 to datafile, use relative


pathnames by typing:
mv file1 datafile

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

Figure 4. The file structure after test.f is moved to directory project2

jones

project1 project2 file1

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

28 Useful UNIX commands Basic UNIX Guide


Removing files with rm

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:

-i Causes rm to prompt you with a message to make sure that you


really want to remove the file.

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

-r Tells rm to remove files recursively. This option is generally used


when you want to remove all the files in a subdirectory and then
remove the subdirectory itself. Notice that this is a powerful
option; it removes everything in the subdirectory tree under your
current working directory!

If you want to use the -r option, we recommend that you use it in


combination with -i, so you will be asked about the removal of
each file.

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
%

Version 1.0, August 1993 Useful UNIX commands 29


File protection and permissions

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.

Figure 5. Sample file permission fields

d r w x r - x r - x

directory (d) user group all others’


or permissions permission permissions
file (-)

Read, write, and execute permissions mean different things for files and for
directories, as detailed in the following table.

30 Useful UNIX commands Basic UNIX Guide


Table 4: File and directory permissions

Permission Abbreviation File Directory


read r may be read, copied or printed filenames in that directory
may be listed
write w may be modified files in that directory may be
created and deleted (regard-
less of owner and permis-
sions on the file!)
execute x needed to execute file (such as directory is usable in a path-
shell scripts or compiled pro- name and searchable by
grams) UNIX utilities

Note that the ability to delete a file is a permission associated with


Caution directories, not with files themselves. When a subdirectory has
write permission, files within it may be deleted, regardless of the
file permission. Be careful about assigning write permission for
others on your subdirectories.

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

Version 1.0, August 1993 Useful UNIX commands 31


Changing file permissions with chmod

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

32 Useful UNIX commands Basic UNIX Guide


Creating new directories with mkdir

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

Figure 6. The file structure with a new directory project3

jones

project1 project2 project3 file1

test.f text.c

Version 1.0, August 1993 Useful UNIX commands 33


Removing directories with rmdir

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

Figure 7. The file structure without the directory project2

jones

project1 project3 file1

test.f text.c

34 Useful UNIX commands Basic UNIX Guide


Searching contents of files with grep

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:

-i Ignores difference between upper- and lowercase characters.

-n Lists line numbers where match is found.

See the grep man page for additional 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:

drwxr-xr-x 2 jones 512 Aug 28 12:04 bin


-rwxr--r-x 2 jones 512 Aug 22 10:3l file1
drwxr-xr-x 7 jones 512 Jun 20 12:50 Augusta.GA

Version 1.0, August 1993 Useful UNIX commands 35


Multiple users accessing files with ln

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

project1 project2 file1 projecta projectb

test.f text.c test1.f my_data data

36 Useful UNIX commands Basic UNIX Guide


Transferring files with rcp

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.

When login ids are different

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.

Version 1.0, August 1993 Useful UNIX commands 37


On a UNICOS computer (such as shavano at NCAR), do not use rcp
Caution
in long-running batch jobs because if the job is interrupted, the job
cannot be restarted reliably.

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

38 Useful UNIX commands Basic UNIX Guide


Transferring files with ftp

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.

Opening an FTP connection

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

Use the get command to retrieve a file from a remote computer:


get remotefile localfile
This transfers a copy of remotefile from the remote computer and places it on your
local computer as localfile. If you do not specify localfile, the file on your
computer will have the same filename as it has on the remote computer.
Use the put command to transfer a file from your local computer to a remote
computer:
put localfile remotefile
This transfers a copy of localfile to the remote computer and places it on the
remote computer as remotefile. If you do not specify remotefile, the file on the
remote computer will have the same filename as it has on your local computer.

Version 1.0, August 1993 Useful UNIX commands 39


Transferring binary 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

Additional ftp commands

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.

Using anonymous FTP accounts

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.

Do not use ftp commands in batch jobs you are submitting to a


Caution UNICOS computer (such as shavano at NCAR). If either computer
is down, the whole job will hang.

40 Useful UNIX commands Basic UNIX Guide


Example 29:

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.

Version 1.0, August 1993 Useful UNIX commands 41


Additional UNIX commands

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.

Table 5: Additional UNIX commands

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.

42 Useful UNIX commands Basic UNIX Guide


Chapter 3

Basic vi editor commands


Contents

Table 1: Basic vi commands 45

Advantages of the vi editor 47


Basic vi rules 47

Invoking the vi editor 48

Exiting the editor 49

Paging and moving around in the file 50

Inserting text 51

Deleting text 52

Replacing and changing text 54

Copying and moving text within a file 56

Copying and moving text between files 58

Searching text 60

Changing your vi editor environment 61

44 UNIX editors Basic UNIX Guide


Basic vi editor commands

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.

Table 1: Basic vi commands

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

Version 1.0, August 1993 UNIX editors 45


Table 1: Basic vi commands (continued)

ESCAPE Changes from insert mode to command mode


ndd Deletes the line the cursor is on, n indicates number of times to repeat
ndw Deletes word to right of cursor; n indicates number of times to repeat
x Deletes the character the cursor is on
D Deletes from cursor to the end of the line
Replacing and changing text
rx Replaces the character the cursor is on with x
ns Replaces n number of characters from cursor forward
ncw Changes n number of current words from cursor forward
ncc Changes n number of current lines from cursor forward
Copying and moving text
nyy or nY Yanks (copies) n lines of text
p Puts yanked (copied) text below line cursor is on
P Puts yanked (copied) text above line cursor is on
"anyy Yanks and copies to buffer a n lines of text
"anY Yanks and copies to buffer a n lines of text (same as "anyy)
"ap Puts text currently in buffer a below line cursor is on
"aP Puts text currently in buffer a above line cursor is on
Searching text
/string Searches forward for string
?string Searches backward for string
/RETURN Repeats search for string from previous search
N Reverses order of searching
Exiting the editor and writing the file
:w Writes the file without exiting the editor
:wq Writes the file and exits (quits) the editing session
:q! Quits the editing session without saving changes
ZZ Writes the file and exits the editing session

46 UNIX editors Basic UNIX Guide


Advantages of the vi editor

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.

Version 1.0, August 1993 UNIX editors 47


Invoking the vi editor

To invoke the editor, type:


vi filename
If filename already exists, a copy of it is brought into the editor’s temporary buffer
space, and you can then edit this file copy as needed. If the named file does not
exist, then the editor creates a file in its buffer space for you.
Once you are in the editor, there are two modes of operation: command mode and
insert mode. Initially, you will be in command mode and will have to tell the editor
what you want to do: insert or delete text, search, move the cursor to another place,
and so on. Each of these activities has a key defined as that function. When you
press the key, it does not appear on the screen; the editor just does whatever that
function that key represents (this will take a little getting used to). We describe
some of these letter commands on the pages that follow.

Example 1:

When a file named junk already exists, and you type:


vi junk
The screen displays something like:

Junk file begins here.


More text here - line 2
More text here - line 3
~
~
~
~
~
~
~
~

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

48 UNIX editors Basic UNIX Guide


Exiting the editor

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:

:w Writes the editor’s buffer to the permanent disk file without


exiting the editor. Do this periodically to save your work—vi
uses the filename you gave when you started the editor.

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

Junk file text begins here.


more text here
~
~
~
~
~
~
:wq
"junk" 2 lines, 25 characters

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.

Version 1.0, August 1993 UNIX editors 49


Paging and moving around in the file

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:

CONTROL-f Moves forward a screenful

CONTROL-b Moves backward a screenful

CONTROL-u Moves up one-half screen

CONTROL-d Moves down one-half screen

Two other helpful positioning commands are:

1G Moves cursor to the beginning of the file; nG moves the cursor to


the beginning of the nth line of the file

G Moves cursor to the last line of the file

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

l Moves cursor to the right one character for each l

j Moves cursor down one line for each j

k Moves cursor up one line for each k

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:

10k Moves the cursor up 10 lines (counting the one where it is


currently positioned)

50 UNIX editors Basic UNIX Guide


Inserting text

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.

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 below the cursor and begins insert mode

O Opens a line above the cursor and begins insert mode


Once you are in insert mode, everything you type is entered as text into the file.
You will stay in insert mode until you press ESCAPE (the escape key), which takes
you out of that mode.
One of the frustrating things that happens to people who are new to vi is that they
often forget they are in insert mode and when they want to move around in the file,
they type the movement command characters into the text. For instance, if you are
inserting text and need to move forward to the next page, you must press ESCAPE
before you use CONTROL-f to move forward. If you don’t press ESCAPE, the
CONTROL-f characters become part of your text.
If you don’t remember which mode you are in, press:
ESCAPE
If you are in insert mode, this puts you in command mode; if you are already in
command mode, you stay there—the keystroke doesn’t change anything. (The
terminal may “beep,” but there is no problem.)
As you are entering text, remember that BACKSPACE or DELETE erases one
character at a time to the left of the cursor, provided your UNIX environment is
properly set. See your system administrator if neither of these keys erases
backwards.

Version 1.0, August 1993 UNIX editors 51


Deleting text

Deleting text can be done by line, word, or character. Here are the commonly used
deletion commands.

dd Deletes the line the cursor is on

dw Deletes the word the cursor is on; starts from the cursor and
deletes to the end of the word

x Deletes the character the cursor is on

D Deletes everything from the cursor to the end of the line

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.

52 UNIX editors Basic UNIX Guide


Example 3:

7dd Deletes 7 lines, including the line the cursor is on

9dw Deletes 9 words, including the word the cursor is on

12x Deletes 12 characters, starting with the character the cursor is on

dd Deletes the current line

Version 1.0, August 1993 UNIX editors 53


Replacing and changing text

To replace or change text in vi, use the change commands listed below.

r Replaces the character the cursor is on with the next single


character you type

s Replaces the character the cursor is on with whatever sequence of


characters you type—until you press ESCAPE

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

cc Changes the current line to whatever sequence of characters you


type—until you press ESCAPE
You may change multiple words, characters, or lines by typing a number in front
of the command. To change the current word and the next two words, you would
type 3cw; to change four characters, you would type 4s.

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.

Jun$ file - line 3

Now, you type the new text (Trash) and press ESCAPE. The result is:

Trash file - line 3

Notice that the cursor is positioned at the end of the changed word.

54 UNIX editors Basic UNIX Guide


Example 5:

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.

Junk file - line 5


<-(Line erased; insert mode at cursor position)
Junk file - line 7

After you type the new text and press ESCAPE, the change appears as shown:

Junk file - line 5


This is the new text to replace the old line.
Junk file - line 7

Version 1.0, August 1993 UNIX editors 55


Copying and moving text within a file

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.

yy or Y Yanks (copies to the buffer) one line of text

nyy or nY Yanks (copies to the buffer) n lines of text

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

p Puts last text yanked or deleted below line cursor is on

P Puts last text yanked or deleted above line cursor is on

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.

Do not do any other editing between the yank or delete command


Caution and the put command, or you will lose the text that was in the
buffer. Always follow this order: (1) yank or delete the text, (2)
move the cursor to where you want to put the text, and (3) use a
put command to insert the text. (You can always type :q! to exit the
current editing session and preserve the file as it was before the
current editing session started.)

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

56 UNIX editors Basic UNIX Guide


Example 6:

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:

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,
followed by the put command.
~
~
~
~
6 lines yanked

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

Version 1.0, August 1993 UNIX editors 57


Copying and moving text between files

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

"ayy or "aY Yanks and copies to buffer a one line of text

"anyy or "anY Yanks and copies to buffer a n lines of text

"add Deletes and stores in buffer a one line of text

"andd Deletes and stores in buffer a n lines of text

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

:w Writes the editor’s buffer to the permanent disk file without


exiting the editor

:e filename Opens filename for editing

:e Returns to the other currently open file for editing; used to toggle
back and forth between files

58 UNIX editors Basic UNIX Guide


Example 7:

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.

Version 1.0, August 1993 UNIX editors 59


Searching text

You can search a file for a string of text using one of these commands:

/string Searches forward for the next occurrence of string

?string Searches backward for the preceding occurrence of string


To repeat the forward search, type / (slash), then RETURN, or press the n key.
When you enter the /, the cursor moves to the bottom left corner of the screen until
you press RETURN. If you continue to issue the command after you have searched
through the entire file, the search starts over at the beginning of the file.
To search backward, just type ? (question mark) followed by a RETURN. When
you enter the ?, your cursor moves to the bottom left corner of the screen until you
press RETURN. To continue the backward search, press the n key. If you continue
to issue the command after you have searched through the entire file, the search
starts over at the end of the file.
To change the direction of a search, press the N (capital N) key; to change again,
press the n (lowercase n) key.
The vi search is case sensitive. To make the search commands insensitive to case,
at the : (colon) vi prompt, type set ignorecase (or, set ic, for short). For more on the
vi :set command, see the next section “Changing your vi editor environment.”

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.

60 UNIX editors Basic UNIX Guide


Changing your vi editor environment

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.

Version 1.0, August 1993 UNIX editors 61


62 UNIX editors Basic UNIX Guide
Chapter 4

Getting started with


the UNIX shell
Contents

What is a shell? 65
What the shell does for you 65

The three common UNIX shells 66


The Bourne shell 66
The C shell 66
The Korn shell 67
Changing your UNIX shell 67
Changing shells for a single session 67
Changing your default shell for all sessions 68

What is a subshell? 68
Table 1: Features of the different shells 69

Shell initialization files 71


Bourne and Korn shell initialization files 71
C shell initialization files 72

64 UNIX shell Basic UNIX Guide


Getting started with UNIX shells

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.

What the shell does for you

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.

Version 1.0, August 1993 UNIX shell 65


The three common UNIX shells

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

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.

66 UNIX shell Basic UNIX Guide


The C shell is in widespread use in the UNIX community. One of the C shell’s chief
advantages is the history mechanism, which allows easy recall of previously-typed
commands. In fact, the history mechanism is the reason many users changed to the
C shell from the Bourne shell. The C shell also has a feature called aliasing that
allows you to define shortcuts for complex commands. (However, control
constructs are not allowed within aliases.)

The Korn shell

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.

Changing your UNIX shell

You can change your shell for the current login session or you can change your
default shell for all subsequent sessions.

Changing shells for a single session

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.

Version 1.0, August 1993 UNIX shell 67


Changing your default shell for all sessions

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.

68 UNIX shell Basic UNIX Guide


Table 1: Features of the different shells

Shell features Bourne C shell Korn shell


shell (sh) (csh) (ksh)
Control structures:
For/for each X X X
If-then-else X X X
Case/switch X X X
While X X X
Until X X
Select X X
Built-in arithmetic X X
Typed variables (fast arithmetic) X
Arrays X X
Built-in test/conditional X X X
Built-in substring manipulation X X
Command history X X
History editing X vi-like or emacs-like
Aliases X X
Shell functions X X
Control structs in alias/function X X
Alias/function passed to subshells X X
Filename substitution X X X
Filename substring substitution X extended
Variable substitution X X X
Command substitution X X X
Nested command substitution X
Co-processes X
General signal handling X signal 2 only X

Version 1.0, August 1993 UNIX shell 69


Table 1. Features of the different shell (continued)

Shell features Bourne C shell Korn shell


shell (sh) (csh) (ksh)
Redirect stderr & stdout indepen- X possible X
dently with an extra
process
Redirect output of shell loop X X
Wait for all background jobs X X X
Wait for a particular background job X
User initialization files 1 2 1 or 2
Logout files via signals 1 via signals
Directory stack X limited + aliases
Job control X X
Compatibility sh

70 UNIX shell Basic UNIX Guide


Shell initialization files

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.

Bourne and Korn shell initialization files

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

Version 1.0, August 1993 UNIX shell 71


from this shell during this login session. Your search path and your terminal type
are global information for your environment.
The last three lines of this example .profile define the shell function ls to add the
-xF options. Adding the -x option to ls causes the files to be listed in columns across
your screen; the -F option causes directories to be flagged with a / (slash) after the
name and executable files to be flagged with a * (star) after the name. The $* (dollar
star) means that any arguments you enter for the ls command will be placed here.
Your .profile file is an ASCII text file that you may view, edit, or print. You may
change the .profile file to customize your own computing environment. After you
modify the file, you must type . .profile (dot-space-dot-profile) for the changes
to take immediate effect.

C shell initialization files

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.

72 UNIX shell Basic UNIX Guide


A very simple example .login might look like this:

#!/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.

Version 1.0, August 1993 UNIX shell 73


74 UNIX shell Basic UNIX Guide
Chapter 5

Using the power of the


UNIX shell
Contents

Useful shell features 77


Table 1: Shell features 77
Table 2: Some C shell commands 78

What is a shell script? 79


Shell constructs 81

Filename expansion 83

The UNIX escape character 85

I/O redirection terminology 87


Standard I/O terminology 87

Redirection of standard output (stdout) 89


Append output redirection 89

Redirection of standard input (stdin) 90


The here document 90

Redirection of errors (stderr) 92


C shell 92
Bourne and Korn shells 92

Combining redirection techniques 93


Combining I/O redirection in the C shell 93
Combining I/O redirection in the Bourne and Korn shells 93

Pipes and filters 95

Environment variables 96
Setting an environment variable 96
Removing an environment variable 96
Viewing environment variables 97

Re-executing previous commands 98


Command history 98
Re-executing a selected command 98

Aliasing commands 100

Managing your jobs in the UNIX environment 102


Background execution 102
Changing from background to foreground execution 103
Changing from foreground to background execution 103

76 Using the UNIX shell Basic UNIX Guide


Useful shell features

Table 1: Shell features

Shell Feature Description Example


* Matches any character string ls out.*
? Matches any single character ls model?
[abc] Matches any single character in the list ls c[ao]
< Redirects program input (stdin) to come prgm < my.in.data
from a file
> Redirects program output (stdout) to a file ls > out.file
>& Redirects stdout and error messages ls >& error.file
(stderr) to a file when using the C shell
2> Redirects error messages (stderr) to a file ls 2> error.file
when using the Bourne shell
!str Re-executes the last command that begins !mak
with str (C shell)
!n Re-executes command number n in the !18
history list (C shell)
!! Re-executes previous command (C shell) !!
| Pipes (or connects) two commands so the ls -l | grep August
output from the first becomes the input to
the second
#!/bin/sh In a shell script, specifies that the follow- #!/bin/sh (must be the first line
ing script must run under the Bourne of the script file)
shell
#!/bin/csh In a shell script, either choice specifies #!/bin/csh
# that the following script must run under or
the C shell #
(must be the first line of the
script file)
#!/bin/ksh In a shell script, specifies that the follow- #!/bin/ksh (must be the first line
ing script must run under the Korn shell of the script file)

Version 1.0, August 1993 Using the UNIX shell 77


Table 2: Some C shell commands

Command Description Example


setenv Sets C shell environment variables for setenv TERM vt100
your login session
unsetenv Removes a C shell environment variable unsetenv TERM
env Displays C shell environment variable set- env
tings
history Lists recent C shell commands history
(default=24)
alias Creates a short character string that stands alias ls 'ls -a'
for a long command line in the C shell
alias Displays the aliases already set in the C alias
shell
unalias Removes a specific alias from the C shell unalias ls

78 Using the UNIX shell Basic UNIX Guide


What is a shell script?

In general, a shell script is a program written to be interpreted by a shell. In its


simplest form, it is nothing more than an executable file containing UNIX
commands. More complex shell scripts can also define and use shell variables and
shell control structures, such as loops and conditional statements.
The first line of a C shell script file must be either:
#!/bin/csh
or
#
The # (pound sign) must be the first character of the first line.
The first line of a Korn shell script file must be:
#!/bin/ksh
The # (pound sign) must be the first character of the first line.

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.

Version 1.0, August 1993 Using the UNIX shell 79


Example 1:

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

# set the variable dir1 to contain /usr/tmp/jones


set dir1 = /usr/tmp/jones

# reference the shell variable to make a directory


mkdir $dir1

# change to the new directory


cd $dir1

# redirect the list of files in dir1 to a file named ls.file


ls > ls.file

# mail the file to me


mail jones@ncar < ls.file

# remove ls.file
rm ls.file

The executable commands in this script are:

#!/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.

80 Using the UNIX shell Basic UNIX Guide


Shell constructs

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

# Check return status. If no errors, continue processing.


# status=0 means no errors.

if ($status == "0") then # load and run the program

segldr -o runsource source.o


runsource
else # compiler error
echo "Compiler errors. Program will not be run."
endif

Version 1.0, August 1993 Using the UNIX shell 81


We’ve introduced two new script concepts in this example.
• The echo command transfers the string in double quotes to the terminal screen
(the standard output device).

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

82 Using the UNIX shell Basic UNIX Guide


Filename expansion

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:

* Matches any character, any number of characters, or no character.


Use caution with this metacharacter. It is easy to reference more
than you intend.

? Matches any single character.

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

Version 1.0, August 1993 Using the UNIX shell 83


For all of the following examples, assume that your current working directory
contains the following files:
a.f aA aB ac b.f cAb cAd cbd ccd
In the C shell, the % (percent sign) is the default symbol for your shell prompt.

Example 3:

Perform the following command:


ls c*
Your screen displays:

% ls c*
cAb cAd ccd cbd
%

Example 4:

You perform the following command:


ls a?
Your screen displays:

% ls a?
aA aB ac
%

Example 5:

You perform the following command:


ls c[Ac]?
Your screen displays:

% ls c[Ac]?
cAb cAd ccd
%

84 Using the UNIX shell Basic UNIX Guide


The UNIX escape character

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:

% echo Where is the file?


echo: no match
% echo Where is the file\?
Where is the file?
%

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.

Version 1.0, August 1993 Using the UNIX shell 85


In the second case, the UNIX shell did not interpret the question mark, but simply
passed it along to the UNIX echo command.

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.

% echo Where is the file?


echo: no match
% echo 'Where is the file?'
Where is the file?
%

You will see examples of this technique in the section “Aliasing commands,” later
in this chapter.

86 Using the UNIX shell Basic UNIX Guide


I/O redirection terminology

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.

Standard I/O terminology

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:

stdin Standard input, by default, is expected from the keyboard


(Fortran unit 5).

stdout Standard output, by default, is directed to the screen


(Fortran unit 6).

stderr Standard error includes all error messages resulting from the
program. Error messages are directed to the screen by default
(Fortran unit 0).

Figure 1. UNIX default stdout, stderr, and stdin

standard output (stdout)

standard error (stderr)

standard input (stdin)

Version 1.0, August 1993 Using the UNIX shell 87


It is possible for you to tell the UNIX operating system to take input from
something other than the keyboard and to send output or errors to something
other than the screen. The term used to describe this capability is I/O redirection,
which means to reroute input or output. For example, when a program writes a
large amount of output to stdout, the screen cannot hold it all and the output is lost
as it scrolls off the top of the screen. I/O redirection allows you to capture this
output in a file, which you can then view using one of the UNIX utilities.

88 Using the UNIX shell Basic UNIX Guide


Redirection of standard output (stdout)

In a programming environment, you can capture output from a program or com-


mand in a file without changing the code in your program. You use the > (right
angle bracket, also called the greater-than sign) to redirect standard output into a
file. You can then print or edit the file. The basic syntax for output redirection is:
command > output_filename
Since you don’t see any output on your screen when you use output redirection,
the only signal to you that your command or program has completed is the return
of the shell prompt to the screen. The shell creates output_filename when the
command runs. If you wish to redirect output in your Fortran code, you would
write to Fortran unit 6 (stdout) and use the > (greater-than sign) to have your
program write to a file.

Output redirection overwrites an existing file if there is a filename


Caution match. You may prevent this by setting a shell variable called
noclobber in your .login file. To learn more about this, consult the
man page for the C shell (csh).

Append output redirection

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

Version 1.0, August 1993 Using the UNIX shell 89


Redirection of standard input (stdin)

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

The here document

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:

command [argument(s)] << 'terminating_string'


command_input
command_input
command_input
.
.
.
'terminating_string'

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.

90 Using the UNIX shell Basic UNIX Guide


Example 12:

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:

cat << 'EOF' > new.file

This is the text that will be in new.file


after I type in the terminating string

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

Version 1.0, August 1993 Using the UNIX shell 91


Redirection of errors (stderr)

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.

Bourne and Korn shells

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 and stderr to a single file named output.all:


C shell: prgm >& output.all
Bourne and Korn shells: prgm > output.all 2>&1
Example 15:

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

92 Using the UNIX shell Basic UNIX Guide


Combining redirection techniques

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.

Combining I/O redirection in the C shell

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

Combining I/O redirection in the Bourne and Korn shells

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

Version 1.0, August 1993 Using the UNIX shell 93


Example 16:

Redirecting stdin and stdout:


prgm < my.in.data > my.out.file
This causes your program to read from the my.in.data file and write to the
my.out.file file, without any changes in your program code. This example applies
to the C, Bourne, and Korn shells.

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

94 Using the UNIX shell Basic UNIX Guide


Pipes and filters

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.

Version 1.0, August 1993 Using the UNIX shell 95


Environment variables

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.

Setting an environment variable

In the C shell, environment variables are generally specified in uppercase


characters using the setenv shell command. The setenv command is used as
follows:
setenv VAR value
Blanks separate VAR, the environment variable name, and value, the value given
to the variable.

Removing an environment variable

To remove an environment variable from the C shell’s environment, use the


unsetenv command followed by the name of the variable:
unsetenv VAR

96 Using the UNIX shell Basic UNIX Guide


Viewing environment variables

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:

Remove the VISUAL environment variable from the C shell:


unsetenv VISUAL

Example 22:

To verify which shell you are currently running, type:


echo $SHELL
and the screen displays the value contained in this environment variable.

Version 1.0, August 1993 Using the UNIX shell 97


Re-executing previous commands

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:

1. Add the line

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.

Re-executing a selected command

To re-execute a command in your history list, use one of the following:

!str Re-executes the last command that begins with str, where str is a
string of characters that uniquely identifies the command

!n Re-executes command number n

!! Repeats the last command executed

98 Using the UNIX shell Basic UNIX Guide


Example 23:

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:

To re-execute the previous command, type:


!!

Version 1.0, August 1993 Using the UNIX shell 99


Aliasing commands

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.

100 Using the UNIX shell Basic UNIX Guide


Example 26:

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.

Version 1.0, August 1993 Using the UNIX shell 101


Managing your jobs in the UNIX environment

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.

102 Using the UNIX shell Basic UNIX Guide


Changing from background to foreground execution

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.

Changing from foreground to background execution

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:

% ls | grep august > august.listing


^Z
Stopped
% bg
[1] ls | grep august > august.listing &
%

Version 1.0, August 1993 Using the UNIX shell 103


104 Using the UNIX shell Basic UNIX Guide
Chapter 6

Using UNIX e-mail


Contents

Getting started with e-mail 107


Table 1: Basic mail commands 107
Table 2: Some ~ (tilde) commands 107

Sending e-mail 108


Addressing your e-mail message 109
For NCAR users: The central post office 110
E-mail etiquette 110

Reading your e-mail 111

Sending a reply 112

Getting help 113

Quitting the e-mail program 113

Saving messages 114

Deleting messages 114


Undeleting messages 114

Forwarding mail 115


Forwarding to another user 115
Forwarding to yourself on another computer 116

Sending a file as a mail message 117

Editing a mail message before sending it 118


Environment variables for editing mail 118
Editing mail headers 118

Sending e-mail responses when you go on vacation 119

Creating distribution lists and address aliases 120

Customizing your e-mail environment 121

E-mail error messages 122


Table 3: Possible e-mail error messages 122

106 Using e-mail Basic UNIX Guide


Using UNIX e-mail

Getting started with e-mail

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.

Table 1: Basic mail commands


A message list consists of user names, integers, or ranges of integers separated by spaces. If you do not
supply message list, mail uses the last message.
r message list Reply to senders only (Reversed with R on some UNIX systems)
R message list Reply to everyone on original list (Reversed with r on some UNIX
systems)
? Help
help Help
q Quit, saving unresolved messages in mbox
s message list file Append or save message to file
d message list Delete messages
u message list Undelete messages
x Quit, do not modify system mailbox
m user Mail to specific users

Table 2: Some ~ (tilde) commands


The following ~ (tilde) commands are defined for use in the mail program. To use a tilde command,
position the cursor at the first column of a new line when typing a message.
~f message_# Forwards mail to another person
~r file Read a file into the message buffer
~v Invoke vi editor on message
~h Prompt for To:, Subject:, cc:, and Bcc: headers

Version 1.0, August 1993 Using e-mail 107


Sending e-mail

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
%

108 Using e-mail Basic UNIX Guide


Example 2:

This interrupted message will not be sent to jones:

% mail jones
Subject: no book
I haven’t seen your book and I ^c
(Interrupt -- one more to kill letter)
^c
%

Example 3:

The same message will be sent to several users:

% mail smith jones cbrown


Subject: Missing book
Have you seen my copy of "UNIX for People" anywhere?
Jane Jones
^d
%

Addressing your e-mail message

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

loginid Login id of the user

host Specific computer where the user receives e-mail

domain Location or network of the host computer

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

Version 1.0, August 1993 Using e-mail 109


For NCAR users: The central post office

Mail sent to loginid@ncar.ucar.edu is delivered to the NCAR central post office


that then routes the message to the correct computer and user. When telling other
people your e-mail address, use the your_loginid@ncar.ucar.edu address, rather
than an address containing the specific name of your current computer. Then, if
you change the computer on which you work and update your e-mail address on
the NCAR central post office computer, your mail is always routed correctly. You
are responsible for updating your own e-mail address on the NCAR central post
office computer. To learn how to do this, see the UserDoc “Using the NCAR E-mail
System.” Document ordering information is on the last page of this manual.
To learn how to look up any UCAR or NCAR user’s e-mail address online, see the
description of the NCAR E-mail Query/Change software in the document
mentioned above.
If you are using a UNIX computer outside of NCAR, check with your system
administrator to see if there is a local database of user’s addresses.

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.

110 Using e-mail Basic UNIX Guide


Reading your e-mail

To read your e-mail, type:


mail
at the shell prompt. If you have mail messages, the mail program responds by
displaying a message headers list that shows, for each message in your mailbox:
• U (unread), N (new), or blank (already read) in the first column
• Message number in the second column
• Mail address of the sender
• Date the message was sent
• Size of the message (for example, 12/480 indicates 12 lines, 480 characters,
including the mail header)
• First 25 characters of the subject line
The current message is marked by >. The mail system prompt is & (ampersand).

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

Version 1.0, August 1993 Using e-mail 111


Sending a reply

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.

On some UNIX computers, typing R (capital R) instead of


Caution lowercase r sends a reply to everyone who received the original
message, not just the sender. On other UNIX computers, the
functions of r and R may be reversed. To find out whether you
need to use r or R, type a ? (question mark) at the & prompt.

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
%

112 Using e-mail Basic UNIX Guide


Getting help

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

Quitting the e-mail program

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.

Version 1.0, August 1993 Using e-mail 113


Saving messages

To save a mail message in a separate file, at the & prompt, type:


s message_number filename
If you just enter a filename with no path, then the current mail message (marked
by > in the message header list) is saved in a file in the directory where you are
working. If you want to save the file in another directory, enter a relative or
absolute pathname.

Example 7:

To store message 1 as solution.green in your equations subdirectory type:


s 1 equations/solution.green

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:

1. If you know the message number(s), at the & prompt, type:


u message_number message_number
If you type u and no number, the last message you deleted will be undeleted.
2. If you don’t know the message number(s), you can retrieve all the mail you
had at the beginning of the current mail session. At the & prompt, type:
x
to exit the mail program and return all your mail messages to your mailbox.

Once you have deleted mail and quit the mail program using the
Caution q command, there is no way to recover deleted mail.

114 Using e-mail Basic UNIX Guide


Forwarding mail

There are two kinds of mail forwarding.

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.

Forwarding to another user

1. To forward a mail message to another user, at the & prompt, type:


m loginid@host
2. Type a subject line at the Subject: prompt.
3. Indicate which message to forward by typing:
~f message_number
where message_number is the number of the message you want to forward.
The screen displays the following:

Interpolating: message_number
(continue)

4. The message to forward is now in your mail message, although it isn’t


displayed on the screen. You may type an additional message if you want or
just press CONTROL-d at the beginning of a new line to send the message.

Version 1.0, August 1993 Using e-mail 115


Forwarding to yourself on another computer

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.

116 Using e-mail Basic UNIX Guide


Sending a file as a mail message

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.

When using any ~ (tilde) command, be sure your cursor is positioned


Note in the first character space on a new line. More ~ (tilde) commands are
described later in this chapter.

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
%

Version 1.0, August 1993 Using e-mail 117


Editing a mail message before sending it

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.

Environment variables for editing mail

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

Editing mail headers

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:

followed by the list of recipients of your mail message.

118 Using e-mail Basic UNIX Guide


Follow the steps below to continue editing the mail header information:

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.

Sending e-mail responses when you go on vacation

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.

If you subscribe to e-mail lists on BITNET, you should unsubscribe


Caution
before using vacation. In some cases, the BITNET lists cause your
vacation messages to go out to all list subscribers

Version 1.0, August 1993 Using e-mail 119


Creating distribution lists and address aliases

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:

Here is an example of aliases in a .mailrc file.

alias list1 liz@cgdra kent@ncar tisone@hao robin@mmm \


burghart@rdss cal@ncar nad@niwot
alias v valent@ncar
alias phil phil@utoronto.bitnet
alias joe sauruman@sprlx.span
alias woods gwoods@solar.stanford.edu
alias list2 cal@ncar nad@niwot \
haerer@redcloud

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

120 Using e-mail Basic UNIX Guide


Customizing your e-mail environment

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

Version 1.0, August 1993 Using e-mail 121


E-mail error messages

A returned message will probably have a header like:

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.

Table 3: Possible e-mail error messages

Error message Problem Action


Host unknown The host.domain part of address is Check that typing and e-mail
incorrect. address are correct.
User unknown Message received by proper host, Check that typing and
but loginid was incorrect. loginid are correct.
Hostname lookup failure Mail system unable to look up IP Contact system admin. or
address of the destination host. e-mail specialist.
Host unreachable Problem at other network Contact system admin. or
Network is unreachable e-mail specialist.

Connection refused Remote host or mail server Contact system admin. or


program is down. e-mail specialist.

Connection timed out Destination network or host is Contact system admin. or


down. e-mail specialist.

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.

122 Using e-mail Basic UNIX Guide


Appendix A

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.

New to UNIX but have technical background

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.

Version 1.0, August 1993 Recommended books 125


Introducing UNIX System V, Morgan, Rachel, and Henry McGilton. McGraw-Hill
Book Company, 1987. ISBN 0-07-043152-3 for the revised edition ($19.95).
NCAR Library call no. QA76.76.O63M68. An excellent introduction to AT&T’s
System V UNIX. Extremely comprehensive yet reads easily. Good for technical
people and confident beginners. Covers information accurately.

Advanced

Advanced UNIX Programming, Rochkind, Marc J. Prentice-Hall, Inc., 1985. ISBN


0-13-011800-1 ($35.95 paper). NCAR Library call no. QA76.76.O63R63. Excel-
lent book on programming in a UNIX environment. Shows why, when, and
how to use System V calls or the C interface to the operating system. Contains
information about UNIX internals from the application programmer’s point of
view. This book describes programming at the UNIX system call level. The au-
thor covers every system call in detail and provides sample code. Included are
comprehensive treatments of sophisticated subjects such as signals and inter-
processor communication. While little knowledge of UNIX is assumed, this
book is one that all UNIX experts will want to keep handy. The book is oriented
toward System V and its derivatives. However, the author discusses portability
issues with specific mention of BSD, XENIX, and other systems.
Advanced Programmer’s Guide to UNIX System V, Thomas, Rebecca, Lawrence
Rodgers, and Jean Yates. McGraw-Hill, 1986. ISBN 0-07-881711-9 ($40.00).
NCAR Library call no. QA76.6.T43. A good reference for UNIX System V users
and programmers. The book contains many good shell scripts and C code ex-
amples, especially on interprocess communications.
The Design of the UNIX Operating System, Bach, Maurice J. Prentice-Hall, Inc., 1986.
ISBN 0-13-201799-7 ($31.95). NCAR Library call no. QA76.6.B3. Describes in
detail the System V internal kernel algorithms and data structures.

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.

126 Appendix A Basic UNIX Guide


UNIX Desktop Guide to the Korn Shell, Valley, John. Hayden Books, 1985. ISBN
0-672-48513-3 ($27.95). This book covers Korn and Bourne shell features and
shell programming. The numerous examples emphasize user productivity.
Differences between the Korn and Bourne shells are clearly noted for portabil-
ity between systems.

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.

UNIX system manuals

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

Version 1.0, August 1993 Recommended books 127


UNIX System Administrator’s Reference Manual, Prentice-Hall, 1988. ISBN
0-13-936147-2 ($24.95 paper).
UNIX System User’s Manual, Prentice Hall, 1987. ISBN 0-13-938242-9 ($42.00
paper). NCAR Library call no. QA76.76.O63A486.
4.3BSD UNIX Manuals, University of California at Berkeley, Howard Press c/o
USENIX Association. System manuals for Berkeley Software Distribution Re-
lease 4.3 UNIX.

128 Appendix A Basic UNIX Guide


Index

& (ampersand) background quick reference, 11, 42


execution, 102 repeating in C shell, 98
.forward file, 116 syntax, 13
.login file, 118 Consulting, for NCAR users, 129
.mailrc file, 121 Copying files, 27
.rhosts file, 37 cp command, 27
\ (backslash) escape character, 85
~ (tilde) in C shell, 19
D
A dailyb, at NCAR, 6
Deleting directories, 34
Aliasing commands Deleting files, 29
C shell, 100 diff command, 42
e-mail, 120 Directories
ascii command, in FTP, 40 changing, 24
creating, 33
deleting, 34
B home, 17
listing, 22
Background job execution, 102 moving, 28
Backslash (\) escape character, 85 naming conventions, 17
binary command, in FTP, 40 parent, 19, 34
Binary files, transferring, 40 removing, 34
Books, recommended, 125–128 renaming, 28
Bourne shell, 66, 77–103 root, 17
BSD (Berkeley Software Distribution), 3 searching, 35
working (current), 21
Documentation
C locally written, 6
online, 14
C shell, 66, 77–103 written by SCD, 133
quick reference, 78 Dot files, 19
tilde (~) in, 19
cat command, 25
cd command, 24 E
chmod command, 32
Classes, 6 echo command, 82
comm command, 42 in C shell, 97
Command mode, in vi, 48 Editor, vi, 45–61
Commands, 11–42 E-mail, 107–122
aliasing in C shell, 100 .mailrc file, 121
options, 13 addressing, 109

Version 1.0, August 1993 129


environment, modifying, 121 Foreground job execution, 102
etiquette, 110 FTP (File Transfer Protocol), 39–41
forwarding, 115–116 ftp command, 39
help in, 113
leaving, 113
quick reference, 107 G
quitting, 113
reading, 111 get command, in FTP, 39
removing, 114 grep command, 35
replying to, 112
restoring, 114
saving, 114 H
security of, 110
sending, 108–110 head command, 42
sending files in, 117 Help, online, 14
undeleting, 114 here document, 90
vacation command, 119 Hierarchy, of files, 17–20
env command, in C shell, 97 History, in C shell, 98
Escape character, 85 Home directory, 17

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

130 Basic UNIX Guide


M commands, 11, 42
e-mail, 107
man command, 14 shells, 77
man pages (online manual), 14 vi editor, 45–46
Metacharacters, 83
mget command, in FTP, 40
mkdir command, 33 R
more command, 25
Moving directories, 28 rcp command, 37
Moving files, 28 Redirection, in shells, 89–93
mput command, in FTP, 40 Removing files, 29
mv command, 28 Renaming files and directories, 28
rm command, 29
rmdir command, 34
N Root directory, 17

NCAR users, information for, 6


S
O Security, of accounts, 16
setenv command, in C shell, 96
Online help, 14 Shells, 4, 65–103
Operating system See also C shell; Bourne shell; Korn
diagram of, 5 shell
overview of, 3–5 aliasing commands in C shell, 100
changing for single session, 67
changing the default, 68
P compared, 69–70
filters, 95
Parent directory, 19, 34 history in C shell, 98
passwd command, 16 login, 65
Passwords, of files, 16 pipes, 95
Pathnames, 18 prompt, 65
pg command, 26 quick reference, 77
Pipes, 95 redirecting stderr (standard error), 92
Processes, 4 redirecting stdin (standard
ps command, 42 output), 90
put command, in FTP, 39 redirecting stdout (standard
pwd command, 21 output), 89
redirection combined, 93
scripts, 79–82
subshells, 68
Q Shells, books about, 126
Quick reference Start-up files. See Files: initialization
C shell, 78 status variable, in C shell, 82
stderr (standard error), 87

Version 1.0, August 1993 131


redirection of, 92
stdin (standard input), 87
redirection of, 90
stdout (standard output), 87
redirection of, 89
Subshells, 68
System V, 3

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

132 Basic UNIX Guide


SCD documentation and consulting

To order SCD-produced documentation, send e-mail to docorder@ncar.ucar.edu


on the Internet. You may also call Mary Buck at (303) 497-1232.

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

When prompted for a login name, type:

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:

get docs/catalog/userdoc.catalog local_filename


get docs/catalog/orderform.catalog local_filename
quit

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

To contact an SCD consultant, send e-mail to consult1@ncar.ucar.edu on the


Internet, or call (303) 497-1278.

Version 1.0, August 1993 Recommended books 133


134 Appendix A Basic UNIX Guide
Index

& (ampersand) background Consulting, for NCAR users, 129


execution, 102 Copying files, 27
.forward file, 116 cp command, 27
.login file, 118
.mailrc file, 121
.rhosts file, 37 D
\ (backslash) escape character, 85
~ (tilde) in C shell, 19 dailyb, at NCAR, 6
Deleting directories, 34
Deleting files, 29
A diff command, 42
Directories
Aliasing commands changing, 24
C shell, 100 creating, 33
e-mail, 120 deleting, 34
ascii command, in FTP, 40 home, 17
listing, 22
moving, 28
B naming conventions, 17
parent, 19, 34
Background job execution, 102 removing, 34
Backslash (\) escape character, 85 renaming, 28
binary command, in FTP, 40 root, 17
Binary files, transferring, 40 searching, 35
Books, recommended, 125–128 working (current), 21
Bourne shell, 66, 77–103 Documentation
BSD (Berkeley Software Distribution), 3 locally written, 6
online, 14
written by SCD, 129
C Dot files, 19

C shell, 66, 77–103


quick reference, 78 E
tilde (~) in, 19
cat command, 25 echo command, 82
cd command, 24 echo command, in C shell, 97
chmod command, 32 Editor, vi, 45–61
Classes, 6 E-mail, 107–122
comm command, 42 .mailrc file, 121
Command mode, in vi, 48 addressing, 109
Commands, 11–42 aliases in, 120
aliasing in C shell, 100 deleting, 114
options, 13 editing, 118–119
quick reference, 11, 42 environment, modifying, 121
repeating in C shell, 98 etiquette, 110
syntax, 13 forwarding, 115–116

Version 1.0, August 1993 131


help in, 113 G
leaving, 113
quick reference, 107 get command, in FTP, 39
quitting, 113 grep command, 35
reading, 111
removing, 114
replying to, 112 H
restoring, 114
saving, 114 head command, 42
security of, 110 Help, online, 14
sending, 108–110 here document, 90
sending files in, 117 Hierarchy, of files, 17–20
undeleting, 114 History, in C shell, 98
vacation command, 119 Home directory, 17
env command, in C shell, 97
Escape character, 85
I
F I/O (Input/Output)
redirection of, 89–94
file command, 42 terms defined, 87
Files Insert mode, in vi, 51
copying, 27
deleting, 29
dot, 19
filename expansion, 83
K
linking, 36 Kernel, 4
listing, 22 kill command, 42
moving, 28 Korn shell, 67, 77–103
naming conventions, 17
organization of, 17–20
pathnames, 18
permissions, 30–32 L
protecting, 16, 30–32
less command, 25
remote copying, 37
ln command, 32, 36
removing, 29
Logging in, 12
renaming, 28
Logging out, 12
searching, 35
Login shell, 65
transferring, 37–41
ls command, 22
viewing, 25–26
Filters, 95
find command, 42
finger command, 16 M
Foreground job execution, 102
FTP (File Transfer Protocol), 39–41 man command, 14
ftp command, 39 man pages (online manual), 14

132 Basic UNIX Guide


Metacharacters, 83 R
mget command, in FTP, 40
mkdir command, 33 rcp command, 37
more command, 25 Redirection, in shells, 89–93
Moving directories, 28 Removing files, 29
Moving files, 28 Renaming files and directories, 28
mput command, in FTP, 40 rm command, 29
mv command, 28 rmdir command, 34
Root directory, 17

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

Version 1.0, August 1993 133


System V, 3

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

134 Basic UNIX Guide

Das könnte Ihnen auch gefallen