0% found this document useful (0 votes)
746 views

Digital UNIX: Command and Shell User's Guide

Uploaded by

Tiberiu Comsa
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
746 views

Digital UNIX: Command and Shell User's Guide

Uploaded by

Tiberiu Comsa
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 394

Digital UNIX

Command and Shell Users Guide


Order Number: AA-PS2HD-TE

March 1996 Product Version: Digital UNIX Version 4.0 or higher

This books explains to the general Digital UNIX user how to use commands and shells and how to communicate with other network users.

Digital Equipment Corporation Maynard, Massachusetts

Digital Equipment Corporation makes no representations that the use of its products in the manner described in this publication will not infringe on existing or future patent rights, nor do the descriptions contained in this publication imply the granting of licenses to make, use, or sell equipment or software in accordance with the description. Possession, use, or copying of the software described in this publication is authorized only pursuant to a valid written license from Digital or an authorized sublicensor. Digital Equipment Corporation 1996 All rights reserved.

The following are trademarks of Digital Equipment Corporation: ALLIN1, Alpha AXP, AlphaGeneration, AlphaServer, AlphaStation, AXP, Bookreader, CDA, DDIS, DEC, DEC Ada, DEC Fortran, DEC FUSE, DECnet, DECstation, DECsystem, DECterm, DECUS, DECwindows, DTIF, MASSBUS, MicroVAX, OpenVMS, POLYCENTER, Qbus, StorageWorks, TruCluster, TURBOchannel, ULTRIX, ULTRIX Mail Connection, ULTRIX Worksystem Software, UNIBUS, VAX, VAXstation, VMS, XUI, and the DIGITAL logo. NFS is a registered trademark of Sun Microsystems, Inc. Open Software Foundation, OSF, OSF/1, OSF/Motif, and Motif are trademarks of the Open Software Foundation, Inc. UNIX is a registered trademark in the United States and other countries licensed exclusively through X/Open Company Ltd.

All other trademarks and registered trademarks are the property of their respective holders.

Contents

About This Book


Audience Scope .......................................................................................... ...................................................................................... ............................................................................ ........................................................................... xix xix xx xxi xxii xxiii

...............................................................................................

Organization

Related Documents Readers Comments Conventions

......................................................................................

1
1.1 1.2 1.3 1.4 1.5

Getting Started
Logging In Logging Out ............................................................................... ............................................................................. ...................................................................... .................................................... ....................................................... ....................................................... ............................................................... 11 14 14 16 16 17 18 19 19 110

Using Commands

Stopping Command Execution Setting Your Password 1.5.1 1.5.2 Password Guidelines Password Procedures

1.6

Getting Help 1.6.1 1.6.2

.............................................................................

Displaying and Printing Online Reference Pages (man) . . . . . . . . Locating Commands Using Descriptive Keywords . . . . . . . . . . . . . .

2
2.1 2.2 2.3

Overview of Files and Directories


Overview of Text Editors ........................................................... ............................... ........................... 21 22 25 26 27 27 28 213

Creating Sample Files with the vi Text Editor 2.3.1 2.3.2 2.3.3 2.3.4

Understanding Files, Directories, and Pathnames

Files and Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Directories and Subdirectories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Displaying the Name of Your Current (Working) Directory (pwd) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Tree-Structure File System and Pathnames . . . . . . . . . . . . . . . . . . . ........................................

2.4

Specifying Files with Pattern Matching

3
3.1

Managing Files
Listing Files (ls) 3.1.1 3.1.2 3.1.3 ........................................................................ 32 32 33 33 35 36 37 310 313 314 314 315 316 317 319 320 321

Listing Contents of the Current Directory . . . . . . . . . . . . . . . . . . . . . . . . . . Listing Contents of Other Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Flags Used with the ls Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ........................................................................

3.2

Displaying Files 3.2.1 3.2.2

Displaying Files Without Formatting (pg, more, cat) . . . . . . . . . . . Displaying Files with Formatting (pr) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......................................................

3.3 3.4

Printing Files (lpr, lpq, lprm) Linking Files (ln) 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5

......................................................................

Hard Links and Soft Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Links and File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How Links Work Understanding Filenames and i-numbers . Removing Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .....................................................................

3.5

Copying Files (cp) 3.5.1 3.5.2

Copying Files in the Current Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Copying Files into Other Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

iv Contents

3.6

Renaming or Moving Files (mv) 3.6.1 3.6.2

.................................................

322 322 323 324 326 327 327 328 330

Renaming Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Moving Files into a Different Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ............................................................... ........................................................ ..................................................................

3.7 3.8 3.9

Comparing Files (diff) Removing Files (rm) 3.9.1 3.9.2

Sorting File Contents (sort)

Removing a Single File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing Multiple Files Matching Patterns . . . . . . . . . . . . . . . . . . . . .....................................................

3.10

Determining File Type (le)

4
4.1 4.2

Managing Directories
Creating a Directory (mkdir) Changing Directories (cd) 4.2.1 4.2.2 4.2.3 ....................................................... 41 44 44 45 47 48 48 410 410 411 412 412 413

...........................................................

Changing Your Current Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Relative Pathname Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing Directories Through Symbolic Links . . . . . . . . . . . . . . . . . . ..................................................... ......................................................... .....................................................

4.3 4.4 4.5 4.6

Displaying Directories (ls F) Copying Directories (cp) Renaming Directories (mv) Removing Directories (rmdir) 4.6.1 4.6.2 4.6.3 4.6.4 Removing Removing Removing Removing

............................................................

Empty Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multiple Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Your Current Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Files and Directories Simultaneously (rm r) . . . . . . .

5
5.1

Controlling Access to Your Files and Directories


Understanding Password and Group Security Files 5.1.1 The /etc/passwd File ........................ 51 52

.......................................................

Contents v

5.1.2 5.2 5.3 5.4

The /etc/group File

.......................................................... ................................................... .............................. ............................

53 53 55 58 58 59 510 510 511 511 513 515 517 518 519 520

Protecting Files and Directories

Displaying File and Directory Permissions (ls) Setting File and Directory Permissions (chmod) 5.4.1 5.4.1.1 5.4.1.2 5.4.1.3 5.4.1.4 5.4.2

Specifying Permissions with Letters and Operation Symbols . Changing File Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing Directory Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Pattern-Matching Characters . . . . . . . . . . . . . . . . . . . . . . . . . Setting Absolute Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...................... ...............

Specifying Permissions with Octal Numbers

5.5

Setting Default Permissions with the User Mask (umask) 5.5.1 Setting the umask

........................................................... ....................................... .........................

5.6 5.7 5.8 5.9

Changing Your Identity to Access Files Superuser Concepts

................................................................... ..............................................

Changing Owners and Groups (chown and chgrp) Additional Security Considerations

6
6.1 6.2

Using Processes
Understanding Programs and Processes 6.2.1 Redirecting Input and Output 6.2.1.1 6.2.1.2 6.2.2 6.2.2.1 6.2.2.2 6.2.3 ........................................ .......................... ........................................... 61 61 62 62 63 64 64 65 65 66

Understanding Standard Input, Output, and Error

Reading Input from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Redirecting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .................................. Bourne and Korn Shell Error Redirection . . . . . . . . . . . . . . . . C Shell Error Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .........

Redirecting Standard Error to a File

Redirecting Both Standard Error and Standard Output

6.3

Running Several Processes Simultaneously

...................................

vi Contents

6.3.1 6.3.2 6.4

Running Foreground Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running Background Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .........................................

66 67 68 69 69 611 611 612 613 614

Monitoring and Terminating Processes 6.4.1 Checking Process Status 6.4.1.1 6.4.1.2 6.4.2 6.4.3 6.4.4

..................................................

The ps Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The jobs Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Canceling a Foreground Process (Ctrl/C) . . . . . . . . . . . . . . . . . . . . . . . . . . . Canceling a Background Process (kill) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Suspending and Resuming a Foreground Process (C Shell Only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...............

6.5

Displaying Information About Users and Their Processes

7
7.1 7.2

Shell Overview
Purpose of Shells 7.2.1 7.2.2 ...................................................................... .......................... 71 72 73 74 74 75 75 76 76 77 77 78 78 710 711 711 711

Summary of Bourne, C, and Korn Shell Features

More Information on C and Korn Shell Features . . . . . . . . . . . . . . . . . The Restricted Bourne Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .................................................................

7.3

Changing Your Shell 7.3.1 7.3.2 7.3.3

Determining What Shell You Are Running . . . . . . . . . . . . . . . . . . . . . . . . Temporarily Changing Your Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Permanently Changing Your Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ................................................................ .................

7.4

Command Entry Aids 7.4.1 7.4.1.1 7.4.1.2 7.4.2 7.4.3

Using Multiple Commands and Command Lists

Running Commands in Sequence with a Semicolon (;) . Running Commands Conditionally . . . . . . . . . . . . . . . . . . . . . . . . .

Using Pipes and Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Grouping Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4.3.1 7.4.3.2 Using Parentheses ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Braces { } . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .........................................................................

7.4.4

Quoting

Contents vii

7.4.4.1 7.4.4.2 7.4.4.3 7.5

Using the Backslash ( \ ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Single Quotes ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Double Quotes (" ") . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..............................................................

712 712 713 713 713 714 716 716 718 719 719 720 721 721 721 722 723 724 725 725 726 726 728

The Shell Environment 7.5.1 7.5.2 7.5.3

The login Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shell Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...........................................

7.6 7.7

Login Scripts and Your Environment Using Variables 7.7.1 7.7.1.1 7.7.1.2 7.7.1.3 7.7.2 7.7.3 7.7.4 Setting Variables

........................................................................ ............................................................

Bourne and Korn Shell Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . C Shell Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Variables in All Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Referencing Variables (Parameter Substitution) . . . . . . . . . . . . . . . . . . . Displaying the Values of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Clearing the Values of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .................................................

7.8 7.9

How the Shell Finds Commands Using Logout Scripts 7.9.1 7.9.2

.................................................................

Logout Scripts and the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Sample .logout File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ................................................

7.10

Using Shell Procedures (Scripts) 7.10.1 7.10.2

Writing and Running Shell Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying a Run Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8
8.1 8.2

Shell Features
Comparison of C, Bourne, and Korn Shell Features C Shell Features 8.2.1 8.2.2 8.2.3 ....................... 81 83 83 86 88

........................................................................

Sample .cshrc and .login Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Command History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

viii Contents

8.2.4 8.2.5 8.2.6 8.2.7 8.3

Filename Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Built-In Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Built-In Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ................................................................

89 810 811 812 814 814 816 817 818 819 820 824 825 827 828 829 830 832 833

Bourne Shell Features 8.3.1 8.3.2 8.3.3 8.3.4

Sample .prole Login Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Built-In Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Built-In Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...................................................................

8.4

Korn Shell Features 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.4.6 8.4.7 8.4.8

Sample .prole and .kshrc Login Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Command History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Command Line Editing Using the fc Command . . . . . . . . . . . . . . . . . . 8.4.4.1 Examples of Command Line Editing ...................... Filename Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Built-In Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Built-In Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9
9.1 9.2 9.3 9.4

Using the System V Habitat


Setting Up Your Environment Compatibility for Shell Scripts ..................................................... .................................... ........................................ .................................................... 92 93 94 94

How the System V Habitat Access Works System V Habitat Command Summary

10
10.1 10.2

Obtaining Information About Network Users and Hosts


Identifying Users on the Local Host ........................................... .............................. 101 102

Obtaining Information About Network Users

Contents ix

10.2.1 10.2.2 10.2.3 10.2.4 10.3 10.4 10.5

Obtaining Information About a Specic User . . . . . . . . . . . . . . . . . . . . Obtaining Information About Users on a Remote Host . . . . . . . Obtaining Information About an Individual User on a Remote Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Customizing Output from the nger Command . . . . . . . . . . . . . . . . . .................. ..................

103 104 104 104 105 108 109

Obtaining Information about Remote Hosts and Users Obtaining Information About Users on Remote Hosts Determining Whether a Remote Host Is Online

...........................

11
11.1 11.2

Sending and Receiving Messages


Addressing Mail Messages 11.2.1 11.2.2 ....................................................... ......................................... 111 112 114 114 115 115 116 117 119 1110 1111 1111 1112 1114 1114 1115 1115 1116 1116 1117

Sending a Mail Message Using mailx

Editing a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Aborting a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Aborting a Message with Ctrlc . . . . . . . . . . . . . . . . . . . . . . . . . . . . Aborting a Message with an Escape Command . . . . . . . ...................................

11.2.2.1 11.2.2.2 11.2.3 11.3

Including a File within a Message

Receiving a Mail Message 11.3.1 11.3.2 11.3.3

........................................................

Deleting a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Replying to a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Saving a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Saving a Message in a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Saving a Message in a Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...................................................

11.3.3.1 11.3.3.2 11.3.4 11.4 11.5 11.6

Forwarding a Message

Getting Help from mailx Exiting Mail 11.6.1 11.6.2 Customizing Mail Sessions

.......................................................... .......................................................

...........................................................................

Creating Mail Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Mail Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .......................................

11.7

The Message Handling (MH) Program

x Contents

11.8 11.9

Sending and Receiving Messages With write Sending and Receiving Messages with talk

.............................. .................................

1121 1123

12
12.1

Copying Files to Another Host


Copying Files Between a Local and a Remote Host 12.1.1 12.1.2 12.1.3 12.1.4 ..................... 121 122 123 129 129 129 1210

Using rcp to Copy Files Between Local and Remote Hosts . Using ftp to Copy Files Between Local and Remote Hosts . Using mailx to Copy ASCII Files Between Local and Remote Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using write to Copy Files Between Local and Remote Hosts . .

12.2 12.3

Copying Directories of Files Between a Local and a Remote Host Copying Files Between Two Remote Hosts

................................

13
13.1 13.2 13.3

Working on a Remote Host


Using rlogin to Log in to a Remote Host Using telnet to Log Into a Remote Host .................................... .......................... ..................................... 131 132 133

Using rsh to Run Commands on a Remote Host

14
14.1 14.2 14.3

The UUCP Networking Commands


UUCP Pathname Conventions Connecting to a Remote Host 14.3.1 14.3.1.1 14.3.1.2 ................................................... ............................................. ............................ .................................................... 141 142 142 143 143 144 144 146 148

Finding Hosts that Support UUCP

Using cu to Connect to a Remote Host

Using cu to Connect by Name to a Remote Host . . . . Using cu to Specify a Directly-Connected Remote Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.3.1.3 Using cu to Connect by Telephone to a Remote Host . 14.3.1.4 Local cu Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.3.1.5 Using cu to Connect a Local Computer to Several Remote Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Contents xi

14.3.2

Using tip to Connect to a Remote Host

............................

149 149 1410 1412 1413 1415 1418 1419 1420 1420 1421 1422 1423 1424 1426 1427 1427 1428 1429 1431 1432

14.3.2.1 14.3.2.2 14.3.2.3 14.3.2.4 14.3.3 14.4

Using tip to Connect by Name to a Remote Host . . . . Using tip to Connect by Telephone to a Remote Host . Local tip Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using tip to Connect a Local Computer to Several Remote Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..

Using ct to Connect to a Remote Terminal with a Modem

Using uux to Run Commands on Remote Hosts 14.4.1 14.4.2 14.4.3

..........................

Using uux from the Bourne or Korn Shells . . . . . . . . . . . . . . . . . . . . . . . Using uux from the C Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Other uux Features and Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....................................

14.5

Using UUCP To Send and Receive Files 14.5.1 14.5.2

Using UUCP to Copy Files in the Bourne and Korn Shells . Using UUCP to Copy Files in the C Shell . . . . . . . . . . . . . . . . . . . . . . . . ........................................ ....................................

14.6 14.7 14.8

Using uuto with uupick to Copy Files Using uuto to Send a File Locally 14.8.1 The uustat Command Displaying Job Status of UUCP Utilities 14.8.1.1

..............................................

.....................................................

Displaying the Holding Queue Output with a uustat Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.8.1.2 Displaying the Current Queue Output with uustat Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the uulog Command to Display UUCP Log Files . . . . . Monitoring UUCP Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14.8.2 14.8.3

A
A.1

A Beginners Guide to Using vi


Getting Started A.1.1 A.1.2 A.1.3 A.1.4 ......................................................................... A2 A2 A4 A4 A6 A6

Creating a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Opening an Existing File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Saving a File and Quitting vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Moving Within a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.1.4.1 Moving the Cursor Up, Down, Left, and Right ........

xii Contents

A.1.4.2

Moving the Cursor by Word, Line, Sentence, and Paragraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.1.4.3 Moving and Scrolling the Cursor Forward and Backward Through a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.1.4.4 Movement Command Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . Entering New Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Editing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.1.6.1 A.1.6.2 A.1.6.3 A.1.6.4 Deleting Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deleting Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Text Editing Command Summary . . . . . . . . . . . . . . . . . . . . . . . . . .

A7 A8 A9 A10 A13 A13 A14 A14 A14 A15 A16 A16 A16 A17 A18 A18 A18 A20 A21 A22 A22 A24

A.1.5 A.1.6

A.1.7 A.1.8 A.2

Undoing a Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Finishing Your Edit Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......................................................

Using Advanced Techniques A.2.1 A.2.2 A.2.3 A.2.4

Searching for Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deleting and Moving Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Yanking and Moving Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Other vi Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...........................................

A.3

Using the Underlying ex Commands A.3.1 A.3.2 A.3.3 A.3.4 A.3.5

Making Substitutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Writing a Whole File or Parts of a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deleting a Block of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Customizing Your Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Saving Your Customizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

B
B.1 B.2

Creating and Editing Files with ed


Understanding Text Files and the Edit Buffer Creating and Saving Text Files B.2.1 B.2.2 B.2.3 B.2.4 ............................... B1 B2 B2 B2 B3 B4 B4 B5

...................................................

Starting the ed Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Entering Text The a (Append) Subcommand . . . . . . . . . . . . . . . . . . . Displaying Text The p (print) Subcommand . . . . . . . . . . . . . . . . . . . . Saving Text The w (write) Subcommand . . . . . . . . . . . . . . . . . . . . . . . . B.2.4.1 B.2.4.2 Saving Text under the Same Filename . . . . . . . . . . . . . . . . . . . . Saving Text under a Different Filename . . . . . . . . . . . . . . . . . .

Contents xiii

B.2.4.3 B.2.5 B.3

Saving Part of a File

............................................ ..........

B5 B6 B6 B7 B7 B8 B9 B10 B10 B12 B12 B13 B13 B13 B14 B15 B15 B15 B16 B16 B17 B18 B18 B19 B19 B19 B20 B21 B21 B22

Leaving the ed Program The q (quit) Subcommand

Loading Files into the Edit Buffer B.3.1 B.3.2 B.3.3

...............................................

Using the ed (edit) Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the e (Edit) Subcommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the r (read) Subcommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...................................

B.4

Displaying and Changing the Current Line B.4.1 B.4.2

Finding Your Position in the Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing Your Position in the Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...........................................................................

B.5

Locating Text B.5.1 B.5.2 B.5.3

Searching Forward Through the Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Searching Backward Through the Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing the Direction of a Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ................

B.6

Making Substitutions The s (substitute) Subcommand B.6.1 B.6.2 B.6.3 B.6.4 B.6.5 B.6.6 B.6.7

Substituting on the Current Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Substituting on a Specic Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Substituting on Multiple Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing Every Occurrence of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Substituting at Line Beginnings and Ends . . . . . . . . . . . . . . . . . . . . . . . . . . Using a Context Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..............................

B.7

Deleting Lines The d (delete) Subcommand B.7.1 B.7.2 B.7.3

Deleting the Current Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deleting a Specic Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deleting Multiple Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ................................ ................

B.8 B.9

Moving Text The m (move) Subcommand B.9.1 B.9.2

Changing Lines of Text The c (change) Subcommand

Changing a Single Line of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing Multiple Lines of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...............................

B.10

Inserting Text The i (insert) Subcommand

xiv Contents

B.10.1 B.10.2 B.11 B.12 B.13

Using Line Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using a Context Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...........................

B22 B23 B23 B24 B25

Copying Lines The t (transfer) Subcommand Using System Commands from ed Ending the ed Program

............................................

............................................................

C
C.1 C.2

Using Internationalization Features


Understanding Locale C.2.1 C.2.2 C.2.3 C.2.4 C.2.5 ................................................................ ..................... C1 C2 C3 C4 C5 C5 C5 C5 C6 C8 C10 C10 C10 C10

How Locale Affects Processing and Display of Data

Collation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Date and Time Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Numeric and Monetary Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Yes/No Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...............................

C.3 C.4

Determining Whether a Locale Has Been Set Setting a Locale C.4.1 C.4.2

.......................................................................

Locale Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Limitations of Locale Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C.4.2.1 C.4.2.2 C.4.2.3 Locale Settings Are Not Validated . . . . . . . . . . . . . . . . . . . . . . . . . File Data Is Not Bound to a Locale . . . . . . . . . . . . . . . . . . . . . . . . Setting LC_ALL Overrides All Other Locale Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

D E

Customizing Your mailx Session Using Escape Commands in Your mailx Session

Contents xv

Using the mailx Commands

Index Examples
11-1: Output from mailx Help Command 12-1: Using ftp to Copy a File ............................................. 1114 124

............................................................

Figures
1-1: Shell Interaction with the User and the Operating System 2-1: A Typical Digital UNIX File System 2-2: Relative and Full Pathnames 3-1: Removing Links and Files 4-2: Copying a Directory Tree 7-1: Flow Through a Pipeline 9-1: System V Habitat ................ 15 29 212 318 43 49 57 79 92

............................................

........................................................ ........................................................... ......................

4-1: Relationship Between Directories and Subdirectories 5-1: File and Directory Permission Fields

............................................................ ............................................. .............................................................

.......................................................................

Tables
3-1: The ls Command Options 3-3: The pr Command Flags 3-4: The lpr Command Flags 5-2: Permission Combinations ............................................................ ................................................... ............................................................... .............................................................. ....................... ............................................................ ............................ 34 35 38 311 55 512 513

3-2: The ls l Command Information

5-1: Differences Between File and Directory Permissions 5-3: How Octal Numbers Relate to Permission Fields

xvi Contents

5-4: The umask Permission Combinations 7-1: Shell Names and Default Prompts 7-2: Multiple Command Operators 7-3: Command Grouping Symbols 7-4: Shell Quoting Conventions

............................................ ................

514 62 75 77 710 712 714 717 727 81 84 85 86 89 812 813 815 816 818 819 821 823 824 826 832 833 95 104

6-1: Shell Notation for Reading Input and Redirecting Output

................................................

...................................................... ...................................................... .......................................................... ........................................... ............................................. .............................................. ...................................................

7-5: Selected Shell Environment Variables 7-6: System and Local Login Scripts 7-7: Description of Example Shell Script 8-1: C, Bourne, and Korn Shell Features 8-2: Example .cshrc Script 8-3: Example .login Script 8-4: C Shell Metacharacters

................................................................. ................................................................. ................................................................ ......................................... ........................................................... ......................................................... ............................................ ........................................................ .................................................. ............................................... ..............................................

8-5: Reexecuting History Buffer Commands 8-6: Built-In C Shell Variables 8-7: Built-In C Shell Commands 8-9: Bourne Shell Metacharacters

8-8: Example Bourne Shell .prole Script 8-10: Built-In Bourne Shell Variables 8-11: Built-In Bourne Shell Commands 8-12: Example Korn Shell .prole Script 8-13: Example .kshrc Script 8-14: Korn Shell Metacharacters

............................................................... ......................................................... ....................................... ..................................................... .................................................. ..................................................

8-15: Reexecuting History Buffer Commands 8-16: Built-In Korn Shell Variables 8-17: Built-In Korn Shell Commands 9-1: User Commands Summary 10-1: Options to the nger Command

..........................................................

Contents xvii

10-2: Options to the ruptime Command

............................................... ...................... ....... ...................

107 1119 125 127 128 134 145 148 1412 1414 1417 1421 1424 1425 1426 1430 1431 A6 A9 A12 A15 A22 C7 C8 D1 E1 F1

11-1: Commands for the MH Message-Handling Program 12-2: ftp Subcommands for Directory and File Modication 12-3: ftp Subcommands for Help and Status Information 13-1: telnet Subcommands 14-2: Local cu Commands 14-4: Local tip Commands 14-1: Options to the cu Command 14-3: Options to the tip Command 14-5: Options to the ct Command 14-6: Options to the uux Command

12-1: ftp Subcommands for Connecting to a Host and Copying Files

........................

................................................................. ....................................................... ...................................................... ....................................................... ..................................................... ................................................. ................................................. .................................................... ................................................ ................................................ ............................................ ........................................ .............................................. ................................................ ............................................... ................. ................................................................. .................................................................

14-7: Options to the UUCP Command 14-8: Options to the uupick Command 14-9: Options to the uuto Command 14-10: Options to the uustat Command 14-11: Options to the uulog Command

A-1: Write and Quit Command Summary A-2: Cursor Movement Command Summary A-3: Text Insertion Command Summary A-4: Text Editing Command Summary A-5: Selected vi Environment Variables C-1: Locale Names

........................................................................... ..............................

C-2: Environment Variables That Inuence Locale Functions D-1: Variables for Customizing Your mailx Session E-1: Escape Commands in mail F-1: Commands for the mailx Program

.......................................................... ................................................

xviii Contents

About This Book

Command and Shell Users Guide introduces users to the basic use of commands and shells in the Digital UNIX operating system. This book also documents how to communicate with other network users. This preface covers the following topics: Audience Scope Organization Related Documents Conventions

Audience
This book is written for those who do not have extensive knowledge of UNIX compatible operating systems. This book explains important concepts, provides tutorials, and is organized according to task.

Scope
This book introduces you to the use of commands and shells. After reading this book, you should be able to: Gain access to your system and issue commands Understand le and directory concepts Manage les and directories Control access to your les and directories Manage processes Understand and manage your shell environment Use the vi and ed text editors Use network applications to communicate with network users and access remote systems and processes

Organization
This book is organized into 13 chapters and 6 appendices: Chapter 1 Chapter 2 Shows how to log in and out of your system, enter commands, set your password, and obtain online help. Provides an overview of the le system, consisting of the les and directories that are used to store text, programs, and other data. This chapter also introduces you to the vi text editor, a program that allows you to create and modify les. Shows how to manage les. You will learn how to list, display, copy, move, link, and remove them. Explains how to manage directories. You will learn how to create, change, display, copy, rename, and remove them. Shows how to control access to your les and directories by setting appropriate permissions. It also describes standard password and group security issues as well as provides an overview of additional security considerations. Describes how the operating system creates and keeps track of processes. This chapter explains how to redirect process input, output, and error information, run processes simultaneously, display process information, and cancel processes. Introduces features common to the three shells available with the operating system: the Bourne, C, and Korn shells. You learn how to change your shell, use command entry aids, understand some features of your shell environment (login scripts, environment and shell variables), set and clear variables, write logout scripts, and write and run basic shell procedures. Provides detailed reference information about the C, Bourne, and Korn shells, comparing their features. It details the commands and environment variables of each program and shows how to set up your login script. Shows how to access the System V habitat, a subset of commands, subroutines, and system calls that conforms to the System V Interface Denition (SVID). Provides information on how to get information about other users and remote hosts on the network. Provides information about how to send a message to another user.

Chapter 3 Chapter 4 Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10 Chapter 11

xx About This Book

Chapter 12 Chapter 13 Chapter 14

Provides information about how to copy les to or between remote hosts. Provides information about how to log in to or execute commands at a remote host. Provides information about the UNIX-to-UNIX Copy Program (UUCP) for performing communication tasks concurrently on both a local and remote host.

The appendixes in this book provide the following information: Appendix A Appendix B Teaches you how to use the basic features of the vi text editor. Teaches you how to use the ed text editor. Detailed information about ed is provided because all systems have this editor, and ed can be used in critical system management situations when no other editor can be used. Describes the internationalization features that allow users to process data and interact with the system in a manner appropriate to their native language, customs, and geographic region. Provides a list of variables that can be used in the .mailrc le to customize a mailx session. Provides a list of escape commands that can be used to perform certain tasks from within a mailx session. Provides a list of commands that can be used to send, read, delete, or save messages using mailx.

Appendix C

Appendix D Appendix E Appendix F

Related Documents
The following Digital UNIX user documents are available in Bookreader format on your CD-ROM and optionally in hardcopy: Quick Reference Card Documentation Overview, Glossary, and Master Index System Administration Reference Pages Section 1 Reference Pages Section 8 and 1m

The following OSF/1 documents are currently available from Prentice-Hall: OSF/1 Command Reference OSF/1 System and Network Administrators Reference

About This Book xxi

The printed version of the Digital UNIX documentation set is color coded to help specic audiences quickly nd the books that meet their needs. (You can order the printed documentation from Digital.) This color coding is reinforced with the use of an icon on the spines of books. The following list describes this convention:
Audience General users System and network administrators Programmers Device driver writers Reference page users Icon G S P D R Color Code Blue Red Purple Orange Green

Some books in the documentation set help meet the needs of several audiences. For example, the information in some system books is also used by programmers. Keep this in mind when searching for information on specic topics. The Documentation Overview, Glossary, and Master Index provides information on all of the books in the Digital UNIX documentation set.

Readers Comments
Digital welcomes any comments and suggestions you have on this and other Digital UNIX manuals. You can send your comments in the following ways: Fax: 603-881-0120 Attn: UEG Publications, ZK03-3/Y32 Internet electronic mail: [email protected] A Readers Comment form is located on your system in the following location:
/usr/doc/readers_comment.txt

Mail: Digital Equipment Corporation UEG Publications Manager ZK03-3/Y32 110 Spit Brook Road Nashua, NH 03062-9987 A Readers Comment form is located in the back of each printed manual. The form is postage paid if you mail it in the United States.

xxii About This Book

Please include the following information along with your comments: The full title of the book and the order number. (The order number is printed on the title page of this book and on its back cover.) The section numbers and page numbers of the information on which you are commenting. The version of Digital UNIX that you are using. If known, the type of processor that is running the Digital UNIX software.

The Digital UNIX Publications group cannot respond to system problems or technical support inquiries. Please address technical questions to your local system vendor or to the appropriate Digital technical support ofce. Information provided with the software media explains how to send problem reports to Digital.

Conventions
The following conventions are used in this book:
% $ # % cat A percent sign represents the C shell system prompt. A dollar sign represents the system prompt for the Bourne and Korn shells. A number sign represents the superuser prompt. Boldface type in interactive examples indicates typed user input. Italic (slanted) type indicates variable values, placeholders, and function argument names. In syntax denitions, brackets indicate items that are optional and braces indicate items that are required. Vertical bars separating items inside brackets or braces indicate that you choose one item from among those listed. In syntax denitions, a horizontal ellipsis indicates that the preceding item can be repeated one or more times. A cross-reference to a reference page includes the appropriate section number in parentheses. For example, cat(1) indicates that you can nd information on the cat command in Section 1 of the reference pages. This symbol indicates that you hold down the rst named key while pressing the key or mouse button that follows the slash. In examples, this key combination is enclosed in a box (for example, Ctrl/C ).

file
[ | ] { | }

. . . cat(1)

Ctrl/x

About This Book xxiii

Getting Started

This chapter introduces the basic tasks for using the operating system. Before reading this chapter, familiarize yourself with your systems hardware components. If you are familiar with the UNIX operating system or other operating systems, you may want to skim this chapter. After completing this chapter, you will be able to: Log in to and log out of the operating system Set and change your password Execute commands Stop command execution View and display reference (man) pages

To use the operating system to its full capabilities, you must learn how to create and modify les with a text editing program. See Chapter 2 for an overview of text editors, and Appendix A and Appendix B for information on the vi and ed text editors, respectively. Once you learn how to use a text editor, you should have the basic skills necessary to start using the operating system. Security Note If your system is running the optional enhanced security, your login and password procedures may be different than the procedures documented in this book. See the Security document for more information.

1.1 Logging In
To use the operating system, your operating system must be installed and running and you must be logged in. Logging in identies you as a valid system user and creates a work environment that belongs to you alone.

Before logging in, obtain your username and password from the system administrator. A username (typically your surname or initials) identies you as an authorized user. A password (a group of characters that is easy for you to remember but difcult for others to guess) veries your identity. Think of your username and password as electronic keys that give you access to the system. When you enter your username and password during the login process, you identify yourself as an authorized user. Your password is an important part of system security because it prevents unauthorized use of your data. For more information on passwords, see Section 1.5.1. The rst step in the login process is to display the login prompt. When your system is running and your workstation is on, the following login prompt appears on your screen:
login:

On some systems, you may have to press the Return key a few times to display the login prompt. Your systems login prompt screen may be somewhat different. For example, in addition to the login prompt, the screen may display the system name and the version number of the operating system. To log in, perform the following steps: 1. Enter your username at the login prompt. If you make a mistake, use the Delete key or Backspace key to correct it. For example, if your username is larry, enter:
login: larry

The password prompt appears:


login: larry Password:

2. Enter your password. For security reasons, the password does not display on the screen when you type it. If you think you made a mistake while typing your password, press the Return key. If your password is incorrect, the system displays a message and prompts you to enter your username and password again. After you enter your username and password correctly, the system displays the shell prompt, usually a dollar sign ($) prompt or a percent sign (%) prompt. Your systems shell prompt may be different.

12 Getting Started

Note In this book, the shell prompt display is a dollar sign ($). The shell prompt display tells you that your login is successful, and that the system is ready to go to work for you. The shell prompt is your signal that the shell is running. The shell is a program that interprets all commands you enter, runs the programs you have asked for, and sends the results to your screen. For more information about commands and the shell prompt, see Section 1.3 and Chapter 7. When you rst log in, you are automatically placed in your login directory. See Chapter 2 for information about your login directory. If your system does not display the shell prompt, you are not logged in. You may, for example, have entered your username or your password incorrectly. Try to log in again. If you still cannot log in, see your system administrator. Note Your system may not require you to have a password, or you may have been assigned a password that is common to all new users. To ensure security in these cases, set your own password. For information on how to create or change a password, see Section 1.5. Many systems display a welcome message and announcements whenever users log in. For example, the following is a typical login screen (your screen may vary):
Welcome to the Operating System Fri Dec 7 09:48:25 EDT 19nn You have mail. $

The preceding announcement contains the following pieces of information: A greeting The date and time of your last login Note this information whenever you log in, and tell your system administrator if you have not logged in at the time specied. A wrong date and time might indicate that someone has been breaking into your system. Whether you have mail messages waiting to be read Briey, mail is a program that allows you to send and receive electronic mail. The system displays the message You have mail when there are mail messages for you that are waiting to be read. If you have no mail messages, this line does not appear.

Getting Started 13

1.2 Logging Out


When you are ready to end your work session, log out of the system. Logging out leaves the operating system running for other users and also ensures that no one else can use your work environment. To log out, perform the following steps: 1. Make sure that the shell prompt is displayed. 2. Press Ctrl/D. If Ctrl/D does not work, enter the exit command. The system displays the login prompt. On some systems, a message may also be displayed. At this point, you or another user may log in.

1.3 Using Commands


Operating system commands are programs that perform tasks on the operating system. The operating system has a large set of commands that are described in the remaining chapters of this book and in the related reference pages. Entering a command is an interactive process. When you enter a command, the shell interprets that command, and then gives an appropriate response that is, the system either runs the program or displays an error message. A shell reads every command you enter and directs the operating system to do what is requested. Therefore, the shell is a command interpreter. The shell acts as a command interpreter in the following way: 1. The shell displays a shell prompt and waits for you to enter a command. 2. You enter a command, the shell analyzes it, and locates the requested program. 3. The shell asks the system to run the program, or it returns an error message. 4. When the program completes execution, control returns to the shell, which again displays the prompt. Figure 1-1 shows the relationship between the user, the shell, and the operating system. The shell interacts with both the user to interpret commands and with the operating system to request command execution.

14 Getting Started

Figure 1-1: Shell Interaction with the User and the Operating System
70 TK

d i g i t a l

VAX 6000-410

Command line

Shell

Interpreted command

User types command

Shell interprets command The Operating System executes command


ZK0530UR

The operating system supports three different shells: the Korn, C, and Bourne shells. Your system administrator determines which shell is active when you log in for the rst time. For more information about shells, see Chapter 7. When using the operating system, enter commands at the shell prompt on the command line. For example, to display todays date and time, enter:
$ date

If you make a mistake while typing a command, use the Delete key or Backspace key to erase the incorrect characters and then retype them. An argument is a string of characters that follows a command name. An argument species the data the command uses to complete its action. For example, the man command gives you information about operating system commands. To display complete information about the date command, enter:
$ man date

Commands can have options that modify the way a command works. These options are called ags and immediately follow the command name. Most commands have several ags. If you use ags with a command, arguments follow the ags on the command line.

Getting Started 15

For example, suppose that you use the f ag with the man command. This ag displays a one-line description of a specied command. To display a one-line description of the date command, you would enter:
$ man f date

While a command is running, the system does not display the shell prompt because the control passes to the program you are running. When the command completes its action, the system displays the shell prompt again, indicating that you can enter another command. In addition to using the commands provided with the system, you can also create your own personalized commands. Refer to Section 7.10.1 for information about creating these special commands.

1.4 Stopping Command Execution


If you enter a command and then decide that you do not want it to complete executing, enter Ctrl/C. The command call stops executing, and the system displays the shell prompt. You can now enter another command. Depending upon the command, partial completion of the command may have varied results (referred to as an unknown state). To see the result of stopping a command during execution, enter Ctrl/C after executing commands such as ls -l to list les in a directory or cat filename to view a le on the screen.

1.5 Setting Your Password


Your username is public information and generally does not change. Your password, on the other hand, is private. In most instances, when your system account is established, the system administrator assigns a password that is common to new users. After getting familiar with the system, select your own password to protect your account from unauthorized access. In addition, change your password periodically to protect your data from unauthorized access. To set your password, use the passwd command. If your account does not have a password, use the passwd command to set one. For information on passwd procedures, see Section 1.5.2.

16 Getting Started

1.5.1

Password Guidelines
The following guidelines are useful in selecting a password: Do not choose a word found in a dictionary. Do not use personal information as your password, or as a substring of it, such as your username, names (yours, your familys, your companys), initials, or the make or model of your car. Do not use the default password you received with your account. Do not use old passwords or the same prex or sufx you used in previous passwords. This rule also applies to any passwords you may have used in previous jobs. Do not choose a password that is easy to guess (includes all of the above options) even if you reverse their spelling. Choose a password that is hard to guess, not hard to remember. Do not choose passwords shorter than six characters in length. Your password can be up to eight characters long. (Password length is measured in bytes, rather than characters, but we can regard these terms as the same, for now.) Do not write your password on paper or place it in a le on the system. If possible, use a mixture of uppercase and lowercase letters in your password. You also should include any combination of numbers, punctuation marks, or underscores ( _ ) in your password. Change your password frequently, especially if you think it might have been compromised.

On most systems, you can change your password as often or seldom as you like. However, to protect system security, your system administrator may set limits on how often you should change your password, the length of time your password remains valid, or the nature of changes you can make. Some typical password restrictions could be the following: Character restrictions Minimum number of alphabetic characters Minimum number of other characters, such as punctuation or numbers Minimum number of characters in a new password that must be different from the old password Maximum number of consecutive duplicate characters allowed in a password Maximum number of weeks before your password expires

Time restrictions

Getting Started 17

Number of weeks before you can change a password

See your system administrator for more information about password restrictions.

1.5.2

Password Procedures
To set or change your password, follow these steps: 1. Enter the passwd command:
$ passwd

The system displays the following message (identifying you as the user) and prompts you for your old password:
Changing password for username Old password:

If you do not have an old password, the system does not display this prompt. Go to step 3. 2. Enter your old password. For security reasons, the system does not display your password as you enter it. After the system veries your old password, it is ready to accept your new password, and displays the following prompt:
New password:

3. Enter your new password at the prompt. Remember that your new password entry does not appear on the screen. Finally, to verify the new password (since you cannot see it as you enter), the system prompts you to enter the new password again:
Re-enter new password:

4. Enter your new password again. As before, the new password entry does not appear on the screen. When the shell prompt returns to the screen, your new password is in effect. If you change your password and the new password does not conform to password regulations, you receive a message stating the specic problem and the restrictions in effect for the system.

18 Getting Started

Note Try to remember your password because you cannot log in to the system without it. If you forget your password, see your system administrator.

1.6 Getting Help


Most operating system commands needed for your work are described in this book. If you want to learn more about these and other commands, see the reference pages. The reference pages are provided in three formats: online (see Section 1.6.1), Bookreader (see Chapter 5 in the Installation Guide for more information) and optionally in hard copy (see Related Documents in the About This Book section of this document). When the hard copy documents and Bookreader are unavailable, you can quickly access online command documentation by using the following commands: The man command displays online reference pages. The apropos command displays a one line summary of each command pertaining to a specied subject.

The following sections describe these features.

1.6.1

Displaying and Printing Online Reference Pages (man)


Online reference pages contain information about commands. To view a reference page online, use the man command. For example, to view the reference page for the date command, enter the following (your screen display may vary):
$ man date date(1) NAME date - Displays or sets the date SYNOPSIS Without Superuser Authority date [-u] [+field_descriptor ...] With Superuser Authority date [-nu] [MMddhhmm.ssyy | alternate_date_format] [+field_descriptor ..] The date command writes the current date and time to standard output. DESCRIPTION The date command writes the current date and time to standard output if --More--(7%) date(1)

Getting Started 19

The symbol --More--(7%) at the bottom of the page indicates that 7% of the reference page is currently displayed. At this point, you can press the Space bar to display the next screen of information, press the Return key to display one more line of information, or enter q to quit and return to the shell prompt. Use the following command format to print a reference page:
man manpage| lpr P printer_name

For example, to print the reference page for the date command on a specic printer, enter:
man date | lpr printer_name

The reference page for the date command is now queued for printing on printer_name. See Section 3.3 for more information about the lpr command. To display a brief, one-line description of a command, use the man f command. For example, to display a brief description of the who command, enter:
$ man f who who (1) - Identifies users currently logged in $

For complete information on the man command and its options, you can display the reference page by entering the following:
$ man man

1.6.2

Locating Commands Using Descriptive Keywords


The apropos command and the man k command are useful tools if you forget a command name. Note The apropos and the man k commands require access to the whatis database. This database is available if your system manager loaded the default whatis database when the operating system was installed or created the database later using the catman command. The apropos and man k commands perform the same function. These commands allow you to enter a command description. The commands then list all the commands that t that description. As shown in the exammple, if a command description contains more than one word, the words must be enclosed in double quotes (" "). If the

110 Getting Started

command description contains only one word, it is not necessary to enclose the descriptive word in double quotes. Assume that you cannot remember the name of the command that displays who is logged on to the system. To display the names and descriptions of all commands that have something to do with displaying users who are logged in, enter one of the following:
$ apropos "logged in"

or
$ man k "logged in"

The system displays:


rwho (1c) users (1) w (1) who (1) who is logged in on local machines print names of users who are logged in display who is logged in and what they are doing identifies users currently logged in

Note The numbers enclosed in parentheses refer to the section numbers of the reference pages. After using the apropos or man k commands, you now know that several commands: rwho, users, w, and who can be used to display the users who are logged into the system.

Getting Started 111

Overview of Files and Directories

This chapter provides an introduction to les, le systems, and text editors. A le is a collection of data stored together in the computer. Typical les contain memos, reports, correspondence, programs, or other data. A le system is the useful arrangement of les into directories. A text editor is a program that allows you to create new les and modify existing ones. After completing this chapter, you will be able to: Create les with the vi text editor. These les will be useful for working through the examples later in this book. Understand the le system components and concepts.

This knowledge can help you design a le system that is appropriate for the type of information you use and the way you work.

2.1 Overview of Text Editors


An editor is a program that allows you to create and change les containing text, programs, or other data. An editor does not provide the formatting and printing features of a word processor. With a text editor, you can: Create, read, and write les Display and search for data Add, replace, and remove data Move and copy data Run operating system commands

Your editing takes place in an edit buffer that you can save or discard. The vi and ed text editing programs are available on the operating system. Each editor has its own methods of displaying text as well as its own set of subcommands and rules. For information about vi, read the next section and Appendix A. For information about ed, see Appendix B.

Your system may contain additional editors; see your system administrator for details.

2.2 Creating Sample Files with the vi Text Editor


This section shows how to create three les with the vi text editor. The goal of this section is to have you create, using a minimal set of commands, les that can be used for working through the examples later in this book. For more information about vi, see Appendix A and the vi(1) reference page. Note If you are familiar with a different editing program, you can use that program to create the three sample les described in this section. If you have already created three les with an editing program, you can use those les by substituting their names for the lenames used in the examples. When following the steps that are used to create the sample les, only enter the text that is shown in boldface characters. System prompts and output are shown in a different typeface, like this. To create three sample les, follow these steps: 1. Start the vi program by typing vi and the name of a new le at the shell prompt. Press the Return key:
$ vi file1 Return :

This is a new le, so the system responds by putting your cursor at the top of a screen:
~ ~ ~ ~ ~ ~ "file1" [New file]

Note the blank lines on your screen that begin with a tilde (~). These tildes indicate the lines that contain no text. Because you have not entered any text, all lines begin with a tilde. 2. To specify that you want to insert text to the new le, type the lowercase letter i. The system does not display the i that you enter.

22 Overview of Files and Directories

Enter the following sample text, pressing the Return key after each line. To correct mistakes before moving to the next line, press the Delete key or the Backspace key to move backward over the mistake. Retype the text correctly.
You start the vi program by entering Return the vi command optionally followed by the name Return of a new or existing file. Escape ~ ~ ~ ~ ~ ~ "file1" [New file]

3. To indicate that you have nished your current work, press the Escape key and type a colon (:). Note Depending upon how your terminal or workstation is set up, the Escape key may be programmed to perform a different function. It is possible that one of the function keys on your keyboard may have been set up to perform the "escape" function. See your system administrator if your Escape key does not operate properly. The colon is displayed as a prompt at the bottom of the screen as follows:
You start the vi program by entering the vi command optionally followed by the name of a new or existing file. ~ ~ ~ ~ ~ ~ :

4. Next, enter a lowercase letter w. Entering the letter w indicates to the system that you want to write, or save, a copy of the new le in your current, user directory (see Chapter 4 for an explanation about your current directory).

Overview of Files and Directories 23

Your screen will look like this:


You start the vi program by entering the vi command optionally followed by the name of a new or existing file. ~ ~ ~ ~ ~ ~ "file1" [New file] 3 lines, 111 characters

The system displays the name of the new le as well as the number of lines and characters it contains. The system is still in the vi text editor so you can create two more sample les. The process is the same as the one you used to create file1, but the text you enter will be different. 5. To create the second le, file2, type a colon (:). The colon is displayed as a prompt at the bottom the screen. Enter vi file2 to create your second sample le. The system responds with a screen that looks like this:
~ ~ ~ ~ ~ ~ ~ "file2" No such file or directory

The message file2 No such file or directory indicates that file2 is a new le. 6. Indicate that you want to insert text to the new le by typing the lowercase letter i. Enter the following sample text:
If you have created a new file, you will find that it is easy to add text.

7. Press the Escape key, type a colon (:), and enter the lowercase letter w to write, or save, the le in your current directory.

24 Overview of Files and Directories

Your screen will look like this:


If you have created a new file, you will find that it is easy to add text. ~ ~ ~ ~ ~ ~ ~ "file2" [New file] 2 lines, 75 characters

8. To create the third le, follow the instructions in step 5. However, name the le file3, and enter the following sample text:
You will find that vi is a useful editor that has many features.

9. Then, press the Escape key, type a colon (:), and enter the wq command. The wq command writes the le, quits (or exits) the editor, and returns you to the shell prompt.

2.3 Understanding Files, Directories, and Pathnames


A le is a collection of data stored in a computer. A le stored in a computer is like a document stored in a ling cabinet because you can retrieve it, open it, process it, close it, and store it as a unit. Every computer le has a lename that both users and the system use to refer to the le. A le system is the arrangement of les into a useful pattern. Any time you organize information, you create something like a computer le system. For example, the structure of a manual le system (le cabinets, le drawers, le folders, and documents) resembles the structure of a computer le system. (The software that manages the le storage is also known as the le system, but that usage of the term does not occur in this chapter.) Once you have organized your le system (manual or computer), you can nd a particular piece of information quickly because you understand the structure of the system. To understand the le system, you should rst become familiar with the following three concepts: Files and lenames Directories and subdirectories Tree structures and pathnames

Overview of Files and Directories 25

2.3.1

Files and Filenames


A le can contain the text of a document, a computer program, records for a general ledger, the numerical or statistical output of a computer program, or other data. A le name can contain any character except the following because these characters have special meaning to the shell: slash ( / ) backslash ( \ ) ampersand (&) left- and right-angle brackets (< and >) question mark (?) dollar sign ($) left bracket ([) asterisk (*) or vertical bar or pipe symbol ( | )

You may use a period or dot ( . ) in the middle of a lename, but never at the beginning of the lename unless you want the le to be "hidden" when doing a simple listing of les. For information about characters with special meanings to your shell, refer to the sections about metacharacters in Chapter 8. For information about listing hidden les, see Section 3.1.3. Note Unlike some operating systems, this operating system distinguishes between uppercase and lowercase letters in lenames (that is, it is case sensitive). For example, the following three lenames represent three distinct les: filea, Filea, and FILEA. Use lenames that reect the actual contents of your les. For example, a lename such as memo.advt might indicate that the le contains a memo about advertising. On the other hand, lenames such as filea, fileb, or filec tell you nothing about the contents of that le. It is also a good idea to use a consistent pattern to name related les. For example, suppose you have an advertisting report that is divided into chapters, with each chapter contained in a separate le. You might name these les in the following way: chap1.advt chap2.advt chap3.advt

26 Overview of Files and Directories

The maximum length of a lename depends upon the le system used on your operating system. For example, your le system may allow a maximum lename length of 255 characters (the default), or it may allow a maximum lename length of only 14 characters. Because knowing the maximum lename length is important to name les with meaningful le names, see your system administrator for details.

2.3.2

Directories and Subdirectories


You can organize your les into groups and subgroups that resemble the cabinets, drawers, and folders in a manual le system. These groups are called directories, and the subgroups are called subdirectories. A wellorganized system of directories and subdirectories lets you retrieve and manipulate the data in your les quickly. Directories differ from les in two signicant ways: Directories are organizational tools; les are storage places for data. Directories contain the names of les, other directories, or both.

When you rst log in, the system automatically places you in your login directory. This directory was created for you when your computer account was established. However, a le system in which all les are arranged under your login directory is not necessarily the most efcient method to organize your les. As you work with the system, you may want to set up additional directories and subdirectories so you can organize your les into useful groups. For example, assume that you work for the Sales department and are responsible for four lines of automobiles. You may want to create a subdirectory under your login directory for each automobile line. Each subdirectory can contain all memos, reports, and sales gures applicable for the automobile model. Once your les are arranged into a directory structure that you nd useful, you can move easily between directories. See Chapter 4 for information about creating directories and moving between them.

2.3.3

Displaying the Name of Your Current (Working) Directory (pwd)


The directory in which you are working at any given time is your current, or working directory.

Overview of Files and Directories 27

Whenever you are uncertain about the directory in which you are working or where that directory exists in the le system, enter the pwd (print working directory) command as follows:
$ pwd

The system displays the name of your current directory in the format:
/usr/msg

This information indicates that you are currently working in a directory named msg that is located under the usr directory. The /usr/msg notation is known as the pathname of your working directory. See the following section for information about pathnames.

2.3.4

The Tree-Structure File System and Pathnames


The les and directories in the le system are arranged hierarchically in a structure that resembles an upside-down tree with the roots at the top and the branches at the bottom. This arrangement is called a tree structure. You can nd more detailed information about the directory structure in the hier(5) reference page. Figure 2-1 shows a typical le system arranged in a tree structure. The names of directories are printed in bold, and the names of les are printed in italics.

28 Overview of Files and Directories

Figure 2-1: A Typical Digital UNIX File System


1

bin

user

dev

etc

lib

lost + found usr tmp

chang

smith

plans

report

payroll

1Q 2Q

3Q 4Q

part1 part2 part3

regular

contract

1Q

2Q

3Q 4Q

1Q 2Q

3Q 4Q
ZK0531UR

At the top of the le system shown in Figure 2-1 (that is, at the root of the inverted tree structure) is a directory called the root directory. The symbol that represents this rst major division of the le system is a slash ( / ). At the next level down from the root of the le system are eight directories, each with its own system of subdirectories and les. Figure 2-1, however, shows only the subdirectories under the directory named user. These are the login directories for the users of this system. The third level down the tree structure contains the login directories for two of the systems users, smith and chang. It is in these directories that smith and chang begin their work after logging in. The fourth level of the gure shows three directories under the chang login directory: plans, report, and payroll.

Overview of Files and Directories 29

The fth level of the tree structure contains both les and subdirectories. The plans directory contains four les, one for each quarter. The report directory contains three les comprising the three parts of a report. Also on the fth level are two subdirectories, regular and contract, which further organizes the information in the payroll directory.

A higher level directory is frequently called a parent directory. For example, in Figure 2-1, the directories plans, report, and payroll all have chang as their parent directory. A pathname species the location of a directory or a le within the le system. For example, when you want to change from working on File A in Directory X to File B in Directory Y, you enter the pathname to File B. The operating system then uses this pathname to search through the le system until it locates File B. A pathname consists of a sequence of directory names separated by slashes ( / ) that ends with a directory name or a lename. The rst element in a pathname species where the system is to begin searching, and the nal element species the target of the search. The following pathname is based on Figure 2-1:
/user/chang/report/part3

The rst slash ( /) represents the root directory and indicates the starting place for the search. The remainder of the pathname indicates that the search is to go to the user directory, then to the chang directory, next to the report directory, and nally to the part3 le. Whether you are changing your current directory, sending data to a le, or copying or moving a le from one place in your le system to another, you use pathnames to indicate the objects you want to manipulate. A pathname that starts with a slash ( /) (the symbol representing the root directory) is called a full pathname or an absolute pathname. You can also think of a full pathname as the complete name of a le or a directory. Regardless of where you are working in the le system, you can always nd a le or a directory by specifying its full pathname. The le system also lets you use relative pathnames. Relative pathnames do not begin with the / / that represents the root directory because they are relative to the current directory. You can specify a relative pathname in one of several ways: As the name of a le in the current directory. As a pathname that begins with the name of a directory one level below your current directory.

210 Overview of Files and Directories

As a pathname that begins with .. (dot dot, the relative pathname for the parent directory). As a pathname that begins with . (dot, which refers to the current directory). This relative pathname notation is useful when you want to run your own version of an operating system command in the current directory (for example ./ls ).

Every directory contains at least two entries: .. (dot dot), and . (dot, which refers to the current directory). In Figure 2-2, for example, if your current directory is chang, the relative pathname for the le 1Q in the contract directory is payroll/contract/1Q. By comparing this relative pathname with the full pathname for the same le, /user/chang/payroll/contract/1Q, you can see that using relative pathnames means less typing and more convenience.

Overview of Files and Directories 211

Figure 2-2: Relative and Full Pathnames

bin

user

dev

etc

lib

lost + found usr tmp

chang

smith

plans

report

payroll

1Q 2Q

3Q 4Q

part1 part2 part3

regular

contract

1Q 2Q = Relative pathname = Full pathname

3Q 4Q

1Q 2Q

3Q 4Q

ZK0532UR

In the C shell and the Korn shell, you may also use a tilde (~) at the beginning of relative pathnames. The tilde character species a users login (home) directory. For example, to specify your own login directory, use the tilde alone. To specify the login directory of user chang, specify ~chang. For more information on using relative pathnames, see Chapter 4.

212 Overview of Files and Directories

Note If there are other users on your system, you may or may not be able to get to their les and directories, depending upon the permissions set for them. For more information about le and directory permissions, see Chapter 5. In addition, your system may contain enhanced security features that may affect access to les and directories. If so, see your system administrator for details.

2.4 Specifying Files with Pattern Matching


Commands often take lenames as arguments. To use several different lenames as arguments to a command, you can type out the full name of each le, as the following example shows:
$ ls file1 file2 file3

However, if the lenames have a common pattern (in this example, the file prex), the shell can match that pattern, generate a list of those names, and automatically pass them to the command as arguments. The asterisk (*), sometimes referred to as a wildcard, matches any string of characters. In the following example, the ls command nds the name of every text le in the current directory that includes the file prex:
$ ls file*

The file* matches any lename that begins with file and ends with any other character string. The shell passes every lename that matches this pattern as an argument for the ls command. Thus, you do not have to enter (or even remember) the full name of each le in order to use it as an argument. Both commands (ls with all lenames typed out and ls file*) do the same thing they pass all les with the file prex in the directory as arguments to the ls command. There is one exception to the general rules for pattern matching. When the rst character of a lename is a period, you must match the period explicitly. For example, ls * displays the names of all les in the current directory that do not begin with a period. The command ls a displays all lenames that begin with a period. This restriction prevents the shell from automatically matching the relative directory names. These are . (called dot, standing for the current directory) and .. (called dot dot, standing for the parent directory). For more information on relative directory names, see Chapter 4. If a pattern does not match any lenames, the shell displays a message informing you that no match has been found.

Overview of Files and Directories 213

In addition to the asterisk (*), operating system shells provide other ways to match character patterns. The following list summarizes all pattern-matching characters and provides examples. Character * Action Matches any string, including the null string. For example, th* matches th, theodore, and theresa. ? Matches any single character. For example, 304?b matches 304Tb, 3045b, 304Bb, or any other string that begins with 304, ends with b, and has one character in between. [...] Matches any one of the enclosed characters. For example, [AGX]* matches all lenames in the current directory that begin with A, G, or X. [.-.] Matches any character that falls within the specied range, as dened by the current locale. For more information on locale, see Appendix C. For example, [T-W]* matches all lenames in the current directory that begin with T, U, V, or W. [!...] Matches any single character except one of those enclosed. For example, [!abyz]* matches all lenames in the current directory that begin with any character except a, b, y, or z. This pattern matching is available only in the Bourne and Korn shells. Because this operating system is an internationalized operating system, it provides the following additional pattern-matching features: Character [[:class:]] Action A character class name enclosed in bracket-colon delimiters matches any of the set of characters in the named class. The supported classes are alpha, upper, lower, digit, alnum, xdigit, space, print, punct, graph, and cntrl. For example, the alpha character class name species that you want to match any alphabetic character

214 Overview of Files and Directories

(uppercase and lowercase) as dened by the current locale. If you are running an American-based locale, alpha matches any character in the alphabet (A-Z, a-z). [[=char=]*] A character enclosed in bracket-equal delimiters matches any equivalence class character. An equivalence class is a set of collating elements that all sort to the same primary location. It is generally designed to deal with primary-secondary sorting; that is, for languages such as French that dene groups of characters as sorting to the same primary location, and then having a tie-breaking, secondary sort. For more information on internationalized pattern-matching characters, see the grep(1) reference page. For more information on the operating systems internationalization features, see Appendix C.

Overview of Files and Directories 215

Managing Files

This chapter describes how to manage les on your system. After completing this chapter, you will be able to: List les Display and print les Link les Copy, rename, and move les Compare and sort les Remove les from the system Determine le type

To learn about managing les, follow the examples in this chapter. Do each example in order so that the information on your screen is consistent with the information in this book. Before you can work through the examples, you must be logged in and your login directory must contain the following three les created in Chapter 2: file1, file2, and file3. To produce a listing of the les in your login directory, enter the ls command, which is explained in the following section. If you are using les with different names, make the appropriate substitutions as you work through the examples. In the following examples, when you are asked to return to your login directory, enter the cd (change directory) command as follows:
$ cd $

In the preceding example, the dollar sign ($) represents the shell prompt. Your shell prompt may vary.

In addition, before working on the examples in this chapter, create a subdirectory called project in your login directory. To do so, enter the following mkdir (make directory) command from your login directory:
$ mkdir project $

For more information on the cd and mkdir commands, see Section 4.2 and Section 4.1 respectively.

3.1 Listing Files (ls)


You can display a listing of the contents of one or more directories with the ls (list directory) command. This command produces a list of the les and subdirectories (if any) in your current directory. You can also display other types of information, such as the contents of directories other than your current directory. The format of the ls command is:
ls

The ls command has a number of options, called ags that enable you to display different types of information about the contents of a directory. Refer to Section 3.1.3 for information about these ags.

3.1.1

Listing Contents of the Current Directory


To list the contents of your current directory, enter:
$ ls

Used without ags in this format, the ls command lists the names of the les and directories in your current directory:
$ ls file1 $ file2 file3 project

You may also list portions of your current directorys contents by using the command format:
ls lename

The filename entry can be the name of the le or a list of lenames separated by spaces. You may also use pattern-matching characters to specify les. See Chapter 2 for information on pattern matching.

32 Managing Files

For example, to list the les whose names begin with the characters file, you would enter the following command:
$ ls file* file1 file2 $ file3

3.1.2

Listing Contents of Other Directories


To display a listing of the contents of a directory other than your current directory, use the following format:
ls dirname

The dirname entry is the pathname of the directory whose contents you want to display. In the following example, the current directory is your login directory, and you want to display the /users directory. Your system may contain another directory with a name similar to the /users directory. The name of the /users directory is preceded by a slash ( / ), which indicates that the system should begin searching from the root directory.
$ ls /users amy beth chang mark monique ron $ george jerry larry

The ls command lists directory and lenames in collated order as determined by the current locale. For more information about locales (as used with internationalization), see Appendix C.

3.1.3

Flags Used with the ls Command


In its simplest form, the ls command displays only the names of les and directories contained in the specied directory. However, ls has several ags that provide additional information about the listed items or change the way in which the system displays the listing. When you want to include ags with the ls command, use the following format:
ls agname(s)

The -flagname(s) entry species one or more ags (options) that you are using with the command. For example, the l ag produces a long listing of the directory contents. Note also that all ls ags are preceded by the dash character ().

Managing Files 33

If you want to use multiple ags with the command, enter the ag names together in one string:
$ ls lta

Table 3-1 lists some of the most useful ls command ags. Table 3-1: The ls Command Options
Flag l Action Lists in long format. An l listing provides the type, permissions, number of links, owner, group, size, and time of last modication for each le or directory listed. Sorts the les and directories by the time they were last modied (latest rst), rather than collated by name. Reverses the order of the sort to get reverse collated order (ls r), or reverse time order (ls tr). Lists all entries including "hidden les". Without this ag, the ls command does not list the names of entries that begin with a dot (.), such as .profile, .login, and relative pathnames.

t r a

The following example shows a long (l) listing of a current directory. The name larry shows the owner of the les. Your username and group name will replace larry and system on the screen.
$ ls l total 4 -rw-r--r--rw-r--r--rw-r--r-drwxr-xr-x $ 1 1 1 2 larry larry larry larry system system system system 101 75 65 32 Jun Jun Jun Jun 5 5 5 5 10:03 10:03 10:06 10:07 file1 file2 file3 project

Table 3-2 explains the information displayed on your screen after you enter the ls l command.

34 Managing Files

Table 3-2: The ls l Command Information


Field total 4 drwxr-xr-x Information Number of 512-byte blocks taken up by les in this directory. File type and permissions set for each le or directory. The rst character in this eld indicates le type: (dash) for ordinary les b for block-special les c for character-special les d for directories l for symbolic links p for pipe-special les (rst in, rst out) s for local sockets The remaining characters indicate what read (r), write (w), and execute (x) permissions are set for the owner, group, and others. In addition, other permission information may also be displayed.a Number of links to each le.b Username of the les owner. Group to which the le belongs. Number of bytes in the le. Date and time the le was created or last modied in the format dened by your current locale. Name of the le or directory.

1 larry system 101 Jun 5 10:03 file1

Table notes: a. For more information on permissions, see Chapter 5. b. For an explanation of le links, see Section 3.4. There are other ls command ags that you may nd useful as you gain experience with the operating system. For detailed information about the ls command ags, see the ls(1) reference page.

3.2 Displaying Files


You can view any text le stored on your system with a text editor. However, if you want to just look at a le without making any changes, you may view it (with or without screen formatting) using a variety of operating system commands. The following sections describe these commands.

Managing Files 35

3.2.1

Displaying Files Without Formatting (pg, more, cat)


The following commands display a le just as it is, without adding any special characteristics that govern the appearance of the contents: pg cat more

For information on displaying les with formatting, see Section 3.2.2. To display a le without formatting, the general format is:
command lename

The command entry is one of the following command names: pg, more, or cat. The filename entry can be the name of one le, or a series of lenames separated by spaces. You may also use pattern-matching characters to specify your les. See Chapter 2 for information on using patternmatching characters. The pg command allows you to view one or more les. In the following example, the pg command displays the contents of file1 in your login directory:
$ pg file1 You start the vi program by entering the command vi, optionally followed by the name of a new or existing file. $

To view the contents of both file1 and file2, enter both le names on the command line. When you display les that contain more lines than will t on the screen, the pg command pauses as it displays each screen. To view the next screen of information in a le, press the Return key until you reach the end of the current le. When you press the Return key at the end of the current le, the start of the next le is displayed. The pg command always displays multiple les in the order in which you listed them on the command line. In the example shown below, (EOF): (end of le) means that you are at the end of the current le.
$ pg file1 file2You start the vi program by entering the command vi, optionally followed by the name of a new or existing file. (EOF): Return (Next file: file2) Return If you have created a new file, you will find that it is easy to add text. (EOF): Return $

36 Managing Files

At the Next file: filename prompt, you can enter the n option to go back to the previous le instead of displaying the next le. When you display les that contain more lines than will t on the screen, the pg command pauses as it displays each screen. To see the next screen of information in a le, press the Return key. The more command also allows you to enter multiple le names on the command line and is very much like the pg command in the way that it handles long les. If the le(s) contains more lines than can t on your screen, more pauses and displays a message telling you what percentage of the le you have viewed thus far. At this point, you can do one of the following: Press the Space bar to display the remainder of the le a page at a time Press the Return key to display one line at a time Type q to quit viewing the le(s)

The cat command also displays text. However, it is less useful for viewing long les because it does not paginate les. When viewing a le that is larger than one screen, the contents will display too quickly to be read. When this happens, press Ctrl/S to halt the display. You can then read the text. When you want to display the remainder of the le, press Ctrl/Q. Because cat is not useful for viewing long les, you may prefer using the pg or more command in these cases. The pg, more, and cat commands all have additional options that you may nd useful. For more information, refer to the reference pages for these commands.

3.2.2

Displaying Files with Formatting (pr)


Formatting is the process of controlling the way the contents of your les appear when you display or print them. The pr command displays a le in a simple but useful style. Note The pr command does not interpret any text formatting macros that may reside in your le. The pr command does not format les the same way as nroff or troff, for example. To display a le with simple formatting, the format of the command is:
pr lename

The filename entry can be the name of the le, the relative pathname of the le, the full pathname of the le, or a list of lenames separated by

Managing Files 37

spaces. The format you use depends on where the le is located in relation to your current directory. You may also use pattern-matching characters to specify les. See Chapter 2 for information on pattern matching. Used without any options, the pr command does the following: Divides the contents of the le into pages Puts the date, time, page number, and lename in a heading at the top of each page Leaves ve blank lines at the end of the page

When you use the pr command to display a le, its contents may scroll off your screen too quickly for you to read them. When this happens, you can view the formatted le by using the pr command along with the more command. The more command instructs the system to pause at the end of each screenful of text. For example, to display a long le called report so that it pauses when the screen is full, enter the following command:
$ pr report more

When the system pauses at the rst screen of text, press the Space bar to display the next screen. The previous command uses the pipe symbol ( | ) to take the output from the pr command and use it as input to the more command. For more information on pipes, see Section 7.4.2. Sometimes you may prefer to display a le in a more sophisticated format. You can use a number of ags in the command format to specify additional formatting features. Table 3-3 explains several of these ags. Table 3-3: The pr Command Flags
Flag +page Action Begins formatting on page number page. Otherwise, formatting begins on page 1. For example, the pr +2 file1 command starts formatting file1 on page 2. column Formats page into columns. Otherwise, pr formats pages with one column. For example, the pr 2 file1 command formats file1 into two columns.

38 Managing Files

Table 3-3:
Flag m

(continued)
Action Formats all specied les at the same time, side-by-side, one per column. For example, the pr m file1 file2 command displays the contents of file1 in the left column, and that of file2 in the right column.

Formats double-spaced output. Otherwise, output is single-spaced. For example, the pr d file1 command displays file1 in double-spaced format.

Uses a formfeed character to advance to a new page. (Otherwise, pr issues a sequence of linefeed characters.) Pauses before beginning the rst page if the standard output is a terminal. Uses a formfeed character to advance to a new page. (Otherwise, issues a sequence of linefeed characters.) Does not pause before beginning the rst page if the standard output is a terminal. Sets line width to num columns. Otherwise, line width is 72 columns. For example, the pr w40 file1 command sets the line length of file1 to 40 columns.

wnum

onum

Offsets (indents) each line by num column positions. Otherwise, offset is 0 (zero) column positions. For example, the pr o5 file1 command indents each line of file1 ve spaces.

lnum

Sets page length to num lines. Otherwise, page length is 66 lines. For example, the pr 130 file1 command sets the page length of file1 to 130 lines.

h string

Uses the specied string of characters, rather than the lename, in the header (title) that is displayed at the top of every page. If string includes blanks or special characters, it must be enclosed in (single quotes). For example, the pr h My Novel file1 command species My Novel as the title.

Prevents pr from formatting headings and the blank lines at the end of each page. For example, the pr t file1 command species that file1 be formatted without headings and blank lines at the end of each page.

Managing Files 39

Table 3-3:
Flag schar

(continued)
Action Separates columns with the character char rather than with blank spaces. You must enclose special characters in single quotes. For example, the pr s* file1 command species that asterisks separate columns.

You can use more than one ag at a time with the pr command. In the following example, you instruct pr to format file1 with these characteristics: With double spacing (d) With the title "My Novel" rather than the name of the le

$ pr dh My Novel file1

For detailed information about pr and its ags, refer to the pr(1) reference page.

3.3 Printing Files (lpr, lpq, lprm)


Use the lpr command to send one or more les to the system printer. The lpr command actually places les in a print queue, which is a list of les waiting to be printed. Once the lpr command places your les in the queue, you can continue to do other work on your system while you wait for the les to print. The general format of the lpr command is:
lpr lename

The filename entry can be the name of the le, the relative pathname of the le, the full pathname of the le, or a list of lenames separated by spaces. The format you use depends on where the le is located in relation to your current directory. You may also use pattern-matching characters to specify les. See Chapter 2 for information on pattern matching. If your system has more than one printer, use the following format to specify where you want the le to print:
lpr P printername lename

310 Managing Files

The P ag indicates that you want to specify a printer. The printername entry is the name of a printer. Printers often have names such as lp0, lp1, and lpn. Ask your system administrator for the printer names at your facility. If your system has more than one printer, one of them is the default printer. When you do not enter a specic printername, your print request goes to the default printer. The following example shows how to use the lpr command to print one or more les on a printer named lp0:
$ lpr Plp0 file1 $ lpr Plp0 file2 file3 $

The rst lpr command sends file1 to the lp0 printer and then displays the shell prompt: a dollar sign ($). The second lpr command sends file2 and file3 to the same print queue, and then displays the shell prompt before the les nish printing. Several lpr command ags enable you to control the way in which your le prints. Following is the general format for using a ag with this command:
lpr ag lename

Table 3-4 explains some of the most useful lpr command ags. Table 3-4: The lpr Command Flags
Flag #num Action Prints num copies of the le. Otherwise, lpr prints one copy. For example, the lpr #2 file1 command prints two copies of file1. Sets line width to num columns. Otherwise, line width is 72 columns. For example, the lpr w40 file1 command prints file1 with lines that are 40 columns long. Offsets (indents) each line by num space positions. Otherwise, offset is 8 spaces. For example, the lpr i5 file1 command prints file1 with lines that are indented ve spaces. Formats the le using pr as a lter.

wnum

inum

Managing Files 311

Table 3-4:
Flag T string

(continued)
Action Uses the specied string of characters, rather than the lename, in the header used by pr. Requires the p option. If the string includes blanks or special characters, it must be enclosed in (single quotes). For example, the lpr p T My Novel file1 command species My Novel as the title. Sends mail when the le completes printing. For example, the command lpr m file1 species that you want mail to be sent to you once file1 prints.

Once you have entered the lpr command, your print request is entered into the print queue. To see the position of the request in the print queue, use the lpq command. To look at the print queue, enter:
$ lpq

If your request has already been printed, or if there are no requests in the print queue, the system responds with the following message:
no entries

If there are entries in the print queue, the system lists them and indicates which request is currently being printed. Following is a typical listing of print queue entries (your listing will vary):
Rank active 1st 2nd 3rd Owner marilyn sue juan larry Job 489 135 360 490 Files report letter (standard input) travel Total Size 8470 bytes 5444 bytes 969 bytes 1492 bytes

The lpqcommand displays the following for each print queue entry: Its priority Its owner Its job number Name of the le Size of the le in bytes

For example, Marilyns report (job number 489) is currently being printed, and the requests of Sue, Juan, and Larry are pending.

312 Managing Files

When you print les, the position of the request in the queue as well as its size may help you estimate when your request may be nished. Generally, the higher the priority number in the queue and the larger the print request, the more time it will take. If your system has more than one printer, use the following format to specify which print queue you want to see:
lpq P printername lename

The P ag indicates that you want to specify a print queue. The printername entry is the name of a particular printer. Use the lpstat s command to learn the names of all the printers. If you decide not to print your request, you can delete it from the print queue by using the lprm command. The general format of the lprm command is the following:
lprm jobnumber

The jobnumber entry species the job number that the system has assigned to your print request. You can see the job number by entering the lpq command. For example, if Larry wants to cancel his print request, he can enter:
$ lprm 490 $

The travel le will be removed from the print queue.

3.4 Linking Files (ln)


A link is a connection between a lename and the le itself. Usually, a le has one link a connection to its original lename. However, you can use the ln (link) command to connect a le to more than one lename at the same time. Links are convenient whenever you need to work with the same data in more than one place. For example, suppose you have a le containing assemblyline production statistics. You use the data in this le in two different documents in a monthly report prepared for management, and in a monthly synopsis prepared for the line workers. You can link the statistics le to two different lenames, for example, mgmt.stat and line.stat, and place these lenames in two different directories. In this way, you save storage space because you have only one copy of the le. More importantly, you do not have to update multiple les. Because mgmt.stat and line.stat are linked, editing one automatically updates the other, and both lenames always refer to the same data.

Managing Files 313

3.4.1

Hard Links and Soft Links


There are two kinds of links available for your use: hard links and soft (symbolic) links. Hard links allow you to link only les in the same le system. When you create a hard link, you are providing another name for the same le. All the hard link names for a le, including the original name, are on equal footing. It is incorrect to think of one lename as the real name, and another as only a link. Soft links or symbolic links allow you to link both les and directories. In addition, you may link both les and directories across different le systems. A symbolic link is actually a distinct le that contains a pointer to another le or directory. This pointer is the pathname to the destination le or directory. Only the original lename is the real name of the le or directory. Unlike a hard link, a soft link is actually only a link.

With both hard and soft links, changes made to a le through one name appear in the le as seen through another name. A major difference between hard and soft links occurs when removing them. A le with hard-linked names persists until all its names have been removed. A le with soft-linked names vanishes when its original name has been removed; any remaining soft links then point to a nonexistent le. See Section 3.4.5.

3.4.2

Links and File Systems


The term le system as used in this discussion of links differs from its earlier usage in this book. Previously, a le system was dened as a useful arrangement of les into a directory structure. Here, the same term acquires a more precise meaning: the les and directories contained within a single disk partition. A disk partition is a physical disk, or a portion of one, that has been prepared to contain le directories. You can use the df command to discover the name of the disk partition that holds any particular directory on your operating system. Here is an example in which df shows that the directories /u1/info and /etc are in different le systems, but that /etc and /tmp are in the same le system:

314 Managing Files

$ df /u1/info Filesystem 512-blks /dev/rz2c 196990 $ df /etc Filesystem 512-blks /dev/rz3a 30686 $ df /tmp Filesystem 512-blks /dev/rz3a 30686 $

used 163124 used 19252 used 19252

avail capacity 14166 92% avail capacity 8364 70% avail capacity 8364 70%

Mounted on /u1 Mounted on / Mounted on /

3.4.3

Using Links
To link les in the same le system, use the following command format:
ln /dirname1/lename1 /dirname2/lename2

The /dirname1/filename1 entry is the pathname of an existing le. The /dirname2/filename2 entry is the pathname of a new lename to be linked to the existing /dirname1/filename1. The dirname1 and dirname2 arguments are optional if you are linking les in the same directory. If you want to link les and directories across le systems, you can create symbolic links. To create a symbolic link, add an s ag to the ln command sequence and specify the full pathnames of both les. The ln command for symbolic links takes the following form:
ln s /dirname1/lename1 /dirname2/lename2

The /dirname1/filename1 entry is the pathname of an existing le. The /dirname2/filename2 entry is a pathname of a new lename in a different le system. In the following example, the ln command links the new lename checkfile to the existing le named file3:
$ ln file3 checkfile $

Now use the more command to verify that file3 and checkfile are two names for the same le:
$ more file3

Managing Files 315

The system displays the following:


You will find that vi is a useful editor that has many features. $

Now display the text of checkfile:


$ more checkfile You will find that vi is a useful editor that has many features. $

Notice that both file3 and checkfile contain the same information. Any change that you make to the le under one name will show up when you access the le by its other name. Updating file3, for example, will also update checkfile. If your two les were located in directories that are in two different le systems, you need to create a symbolic link between them. For example, to link a le called newfile that is in the /reports directory to the le called mtgfile in the /summary directory, you can create a symbolic link by using the following:
$ ln s /reports/newfile /summary/mtgfile $

The information in both les is still updated in the same manner as previously explained.

3.4.4

How Links Work Understanding Filenames and i-numbers


Each le has a unique identication number, called an i-number. The inumber refers to the le itself data stored at a particular location rather than to the lename. The i-number distinguishes the le from other les within the same le system. A directory entry is a link between an i-number that represents a physical le and a lename. It is this relationship between les and lenames that enables you to link multiple lenames to the same physical le that is, to the same i-number. To display the i-numbers of les in your current directory, use the ls command with the i (print i-number) ag in the following format:
ls i

316 Managing Files

Examine the identication numbers of the les in your login directory. The number preceding each lename in the listing is the i-number for that le.
$ ls i 1079 checkfile $ 1077 file1 1078 file2 1079 file3

The i-numbers in your listing will differ from those shown in this example. However, the important thing to note is the identical i-numbers for file3 and checkfile, the two les linked in the previous example. In this case, the i-number is 1079. Because an i-number represents a le within a particular lesystem, hard links cannot exist between separate le systems. The situation is entirely different with symbolic links, where the link becomes a new le with its own, new i-number. The symbolic link is not another lename on the original les i-number, but instead is a separate le with its own i-number. Because the symbolic link refers to the original le by name, rather than by i-number, symbolic links work correctly between separate le systems.

3.4.5

Removing Links
The rm (remove le) command does not always remove a le. For example, suppose that a le is linked to more than one lename; that is, several names refer to the same i-number. In this case, the rm command removes the link between the i-number and that lename, but leaves the physical le intact. The rm command actually removes a physical le only after it has removed the last link between that le and a lename, as shown in Figure 3-1. When a symbolic link is removed, the lename specifying the pointer to the destination le or directory is removed. For detailed information about the rm command, refer to Section 3.9.

Managing Files 317

Figure 3-1: Removing Links and Files


name2

File name1

rm name1

File

name2

rm name2

ZK0533UR

To display both the i-numbers and the number of lenames linked to a particular i-number, use the ls command with the i (print i-number) and the l (long listing) ags in the following format:
ls il

Examine the links in your login directory. Remember that the i-numbers displayed on your screen will differ from those shown in the example and that your username and your groups name will replace the larry and system entries.
$ ls il total 3 1079 -rw-r--r-1077 -rw-r--r-1078 -rw-r--r-1079 -rw-r--r-1080 drwxr-xr-x $ 2 1 1 2 2 larry larry larry larry larry system system system system system 65 101 75 65 32 Jun Jun Jun Jun Jun 5 5 5 5 5 10:06 10:03 10:03 10:06 10:07 checkfile file1 file2 file3 project

Again, the rst number in each entry shows the i-number for that lename. The second element in each line shows the le permissions, described in detail in Chapter 5.

318 Managing Files

The third eld for each entry, the number to the left of the username, represents the number of links to that i-number. Notice that file3 and checkfile have the same i-number, 1079, and that both show two links. Each time the rm command removes a lename, it reduces the number of links to that i-number by one. In the following example, use the rm command to remove the lename checkfile.
$ rm checkfile $

List the contents of the directory with the ls il command. Notice that the rm command has reduced the number of links to i-number 1079, which is the same i-number to which file3 is linked, by one.
$ ls il total 3 1077 -rw-r--r-1078 -rw-r--r-1079 -rw-r--r-1080 drwxr-xr-x $ 1 1 1 2 larry larry larry larry system system system system 101 75 65 32 Jun Jun Jun Jun 5 5 5 5 10:03 10:03 10:06 10:07 file1 file2 file3 project

3.5 Copying Files (cp)


This section provides information about how to copy les on a local system. For information about copying les to and from remote systems see Chapter 12 and Chapter 14. The cp (copy) command copies a le from one lename to another lename in your current directory or copies the le from one directory to another directory. The cp command is especially useful to make backup copies of important les. Because the backup and the original are two distinct les, you can make changes to the original while still maintaining an unchanged copy in the backup le. This is helpful in case something happens to the original version. Also, if you decide you do not want to save your most recent changes to the original le, you can begin again with the backup le. Compare the cp command, which actually copies les, with the ln command, which creates multiple names for the same le. Section 3.4 explains the ln command in detail. Refer also to the cp(1) and ln(1) reference pages. The format of the cp command is:
cp source destination

Managing Files 319

The source entry is the name of the le to be copied. The destination entry is the name of the le to which you want to copy source. The source and destination entries can be lenames in your current directory or pathnames to different directories. This statement is true when you are copying les from one directory to another. To copy the contents of an entire directory to another directory (recursively, using the -r option), see Section 4.4. To copy les to a different directory, use the general format of the cp command. In this case, source is a series of one or more lenames and destination is a pathname that ends with the name of the target directory. In the source entry you may also use pattern-matching characters.

3.5.1

Copying Files in the Current Directory


The cp command creates the destination le if it does not already exist. However, if a le with the same name as the destination le does exist, cp copies the source le over the existing destination le. Caution If the destination le exists, your shell may allow the cp command to erase the contents of that le before it copies the source le. As a result, be certain that you do not need the contents of the destination le, or that you have a backup copy of the le, before you use it as the destination le for the cp command. If you use the C shell, see Table 8-6 for the noclobber variable that can be set to prevent the erasure of the destination le. In the following example, the destination le does not exist, so the cp command creates it. First, list the contents of your login directory:
$ ls file1 $ file2 file3 project

Copy the source le, file2, into the new destination le, file2x:
$ cp file2 file2x $

320 Managing Files

List the contents of the directory to verify that the copying process was successful:
$ ls file1 $ file2 file2x file3 project

3.5.2

Copying Files into Other Directories


You need a subdirectory to work through the following example, so create one called reports with the mkdir command:
$ mkdir reports $

To copy the file2 le into the reports directory, enter:


$ cp file2 reports $

List the contents of reports to verify that it contains a copy of file2:


$ ls reports file2 $

You can also use the cp command to copy multiple les from one directory into another directory. The format of the command is:
cp lename1 lename2 dirname

In the following example, enter the cp command to copy both file2 and file3 into the reports directory, and then list the contents of that directory:
$ cp file2 file3 reports $ ls reports file2 file3 $

In the above example, you do not have to specify file2 and file3 as part of the dirname entry because the les being copied are retaining their original lenames. You may also use pattern-matching characters to copy les. For example, to copy file2 and file3 into reports, enter:
$ cp file* reports $

Managing Files 321

To change the name of a le when you copy it into another directory, enter the name of the source le (the original le), the directory name, a slash ( / ), and then the new lename. In the following example, copy file3 into the reports directory under the new name notes, and list the contents of the reports directory:
$ cp file3 reports/notes $ ls reports file2 file3 notes $

3.6 Renaming or Moving Files (mv)


You can use the mv (move) command to perform the following actions: Move one or more les from one directory into another directory Rename les

The format of the mv command is:


mv oldlename newlename

The oldfilename entry is the name of the le you want to move or rename. The newfilename entry is the new name you want to assign to the original le. Both entries can be names of les in the current directory, or pathnames to les in a different directory. You may also use patternmatching characters. The mv command links a new name to an existing i-number and breaks the link between the old name and that i-number. It is useful to compare the mv command with the ln and cp commands, which are explained in Section 3.4 and Section 3.5. Refer also to the reference pages for these commands.

3.6.1

Renaming Files
In the following example, rst list the i-number of each le in your current directory with the ls i command. Next, enter the mv command to change the name of le file2x to newfile. The i-numbers displayed on your screen will differ from the numbers in the example:
$ ls 1077 1078 $ mv $ i file1 1088 file2x file2 1079 file3 file2x newfile 1080 project 1085 reports

322 Managing Files

Again, list the contents of the directory:


$ ls i 1077 file1 1078 file2 $ 1079 file3 1088 newfile 1080 project 1085 reports

Note two things in this example: The mv command changes the name of le file2x to newfile. The i-number for the original le (file2x) and newfile is the same 1088.

The mv command removes the connection between i-number 1088 and lename file2x, replacing it with a connection between i-number 1088 and lename newfile. However, the command does not change the le itself.

3.6.2

Moving Files into a Different Directory


You can also use the mv command to move one or more les from your current directory into a different directory. Note Type the target directory name carefully because the mv command does not distinguish between lenames and directory names. If you enter an invalid directory name, the mv command takes that name as a new lename. The result is that the le is renamed rather than moved. In the following example, the ls command lists the contents of your login directory. The mv command moves file2 from your current directory into the reports directory. The ls command then veries that the le has been removed:
$ ls file1 file2 file3 $ mv file2 reports $ ls file1 file3 newfile $ newfile project reports

project

reports

List the contents of the reports directory to verify that the command has moved the le:
$ ls reports file2 file3 $ notes

Managing Files 323

You may also use pattern-matching characters to move les. For example, to move file1 and file3 into reports, you could enter the following command:
$ mv file* reports $

Now list the contents of your login directory to verify that file1 and file3 have been moved:
$ ls newfile $ project reports

Copy file1, file2, and file3 back into your login directory. The dot (.) in the following command line species the current directory, which in this case is your login directory:
$ cp reports/file* . $

Verify that the les are back in your login directory:


$ ls file1 $ file2 file3 newfile project reports

Lastly, verify that file1, file2, and file3 are still in the reports directory:
$ ls reports file1 file2 $ file3 newfile project reports

3.7 Comparing Files (diff)


You can compare the contents of text les with the diff command. Use the diff command when you want to pinpoint the differences in the contents of two les that are expected to be somewhat different. The format of the diff command is:
diff le1 le2

The diff command scans each line in both les looking for differences. When the diff command nds a line (or lines) that differ, for each line that is different the following information is reported: Line numbers of any changes Whether the difference is an addition, a deletion, or a change to the line

324 Managing Files

If the change is caused by an addition, diff displays:


l[,l] a r[,r]

where l is a line number in file1 and r is a line number in file2. The a indicates an addition. If the difference was a deletion, diff would specify a d; if the difference was a change to a line, diff would specify a c. The actual differing lines follow. In the leftmost column, a left angle bracket (<) indicates lines from file1, and a right angle bracket (>) indicates lines from file2. For example, suppose that you want to quickly compare the following meeting rosters in the les jan15mtg and jan22mtg:
jan15mtg alice colleen daniel david emily frank grace helmut howard jack jane juan lawrence rusty soshanna sue tom jan22mtg alice brent carol colleen daniel david emily frank grace helmut jack jane juan lawrence rusty soshanna sue tom

Managing Files 325

Instead of tediously comparing the list by sight, you can use the diff command to compare jan15mtg with jan22mtg as follows:
$ diff jan15mtg jan22mtg 2a3,4 > brent > carol 10d11 < howard $

Here we nd that Brent and Carol attended the meeting on January 22, and Howard did not. We know this because the line number and text output indicate that brent and carol are additions to le jan22mtg and that howard is a deletion. In cases where there are no differences between les, the system will merely return your prompt. For more information, see the diff(1) reference page.

3.8 Sorting File Contents (sort)


You can sort the contents of text les with the sort command. You can use this command to sort a single le or multiple les. The format of the sort command is:
sort lename

The filename entry can be the name of the le, the relative pathname of the le, the full pathname of the le, or a list of lenames separated by spaces. You may also use pattern-matching characters to specify les. See Chapter 2 for information about pattern matching. A good example of what the sort command can do for you is to sort a list of names and put them in collated order as dened by your current locale. For example, assume that you have lists of names that are contained in three les, list1, list2, and list3:
list1 Zenith, Andre Dikson, Barry DAmbrose, Jeanette Julio, Annette list2 Rocca, Carol Shepard, Louis Hillary, Mimi Chung, Jean list3 Hamilton, Abe Anastio, William Saluccio, William Hsaio, Peter

326 Managing Files

To sort the names in all three les, enter:


$ sort list* Anastio, William Chung, Jean DAmbrose, Jeanette Dickson, Barry Hamilton, Abe Hillary, Mimi Hsaio, Peter Julio, Annette Rocca, Carol Saluccio, Julius Shepard, Louis Zenith, Andrew $

You can also capture the sorted list by redirecting the screen output to a le that you name by entering:
$ sort list* >newlist $

For more information about redirecting output, see Chapter 6. For a detailed description of the sort command and its many options, see the sort(1) reference page.

3.9 Removing Files (rm)


When you no longer need a le, you can remove it with the rm (remove le) command. Use this command to remove a single le or multiple les. The format of the rm command is:
rm lename

The filename entry can be the name of the le, the relative pathname of the le, the full pathname of the le, or a list of lenames. The format you use depends on where the le is located in relation to your current directory.

3.9.1

Removing a Single File


In the following example, you remove the le called file1 from your login directory.

Managing Files 327

First, return to your login directory with the cd (change directory) command. Next, enter the pwd (print working directory) command to verify that your login directory is your current directory, and then list its contents. Remember that the system substitutes the name of your login directory for the notation /u/uname in the example.
$ cd $ pwd /u/uname $ ls file1 file2 $

file3

newfile

project

reports

Enter the rm command to remove newfile, and then list the contents of the directory to verify that the system has removed the le.
$ rm newfile $ ls file1 file2 $ file3 project reports

You must have permission to access a directory before you can remove les from it. For information about directory permissions, see Chapter 5. Note In addition to removing one or more les, rm also removes the links between les and lenames. The rm command actually removes the le itself only when it removes the last link to that le. For information about using the rm command to remove links, see Section 3.4.5.

3.9.2

Removing Multiple Files Matching Patterns


You can remove more than one le at a time with the rm command by using pattern-matching characters. See Chapter 2 for a description of patternmatching characters. For example, suppose your current directory contains the following les: receivable.jun, payable.jun, payroll.jun, and expenses.jun. You can remove all four of these les with the rm *.jun command.

328 Managing Files

Caution Be certain that you understand how the * pattern-matching character works before you use it. For example, the rm * command removes every le in your current directory. Be especially careful with * at the beginning or end of a lename. If you mistakenly enter rm * name instead of rm *name, you will remove all your les, rather than just those ending with name. (If your system is backed up on a regular basis, your system administrator can help you recover lost les.) You may prefer to use the i ag with the rm command, which prompts you for verication before deleting a le or les. See the end of this section for details. You can also use the pattern-matching question mark (?) character with the rm command to remove les whose names are the same, except for a single character. For example, if your current directory contains the les record1, record2, record3, and record4, you can remove all four les with the rm record? command. For detailed information about pattern-matching characters, see Chapter 2. When using pattern-matching characters, you may nd the i (interactive) ag of the rm command particularly useful. The rm i command allows you to selectively delete les. For each le selected by the command, the operating system asks whether or not you want to delete or retain the le. For example, suppose that your directory contains the les record1, record2, record3, record4, record5, and record6. Create those les now in your login directory by using the touch command as follows:
$ touch record1 record2 record3 record4 record5 record6 $

The touch command is useful when you want to create empty les, as you are now. For complete information on the touch command, see the touch(1) reference page. If you want to remove four of the six les that begin with the characters record, enter:
$ rm i record? rm: remove record1?n rm: remove record2?y rm: remove record3?y rm: remove record4?y rm: remove record5?y rm: remove record6?n $

In the preceding example, you have deleted all les except for record1 and record6.

Managing Files 329

Note In addition to removing one or more les, the rm command also provides an option, the r ag, that removes les and directories at the same time. See Chapter 4 for more information.

3.10 Determining File Type (le)


Use the file command when you want to see what kind of data a le contains without having to display its contents. The file command displays whether the le is one of the following: A text le A directory Input for one of the text formatting packages troff, nroff, or eqn input text Source code for the C or FORTRAN programming languages An executable le

The file command is especially useful when you suspect that a le contains a compiled program. Displaying the contents of a compiled program can produce disconcerting results on your screen. The format of the file command is:
le lename

The filename entry can be the name of the le, the relative pathname of the le, the full pathname of the le, or a list of lenames. The format you use depends on where the le is located in relation to your current directory. You may also use pattern-matching characters to specify les. See Chapter 2 for information on pattern matching. For example, to determine the le type of entries in your login directory, enter the following:
$ cd $ pwd /u/uname $ file * file1: ascii text file2: ascii text file3: ascii text project: directory record1: empty record6: empty reports: directory $

330 Managing Files

The file command has identied file1, file2, and file3 as ASCII text les, project and reports as directories, and record1 and record6 as empty les. For more information on the file command, see the file(1) reference page.

Managing Files 331

Managing Directories

This chapter shows you how to manage directories on your system. After completing this chapter, you will be able to: Create directories Change directories Display, copy, and rename directories Remove directories

To learn about managing directories, try the examples in this chapter. You should perform each example in sequence so that the information on your screen is consistent with the information shown in this chapter. Before you can do the examples, you must be logged in and your login directory should be in the state that you left it after doing the examples in Chapter 3. As a result, your login directory should contain the following: The les file1, file2, file3, record1, and record6 The subdirectory, reports, that contains the les file1, file2, file3, and notes The empty subdirectory project

If you are using les with different names, make the appropriate substitutions as you work through the examples. To produce a listing of the les in your current directory, use the ls command, which is explained in Chapter 3.

4.1 Creating a Directory (mkdir)


Directories allow you to organize individual les into useful groups. For example, you could put all the sections of a report in a directory named reports, or the data and programs you use in cost estimating in a directory named estimate. A directory can contain les, other directories, or both. Your login directory was created for you when your computer account was established. However, you will probably need additional directories to organize the les you create while working with the operating system. You create new directories with the mkdir (make directory) command.

The format of the mkdir command is:


mkdir dirname

The dirname entry is the name you want to assign to the new directory. The system creates dirname as a subdirectory of your working directory. This means that the new directory is located at the next level below your current directory. In the following example, return to your login directory by entering the cd command, and create a directory named project2:
$ cd $ mkdir project2 $

Now, create a subdirectory in the reports directory by entering a relative pathname:


$mkdir reports/status $

Figure 4-1 shows the new le system tree structure. The project, project2, and reports directories are located one level below your login directory, and the status subdirectory is located one level below the reports directory.

42 Managing Directories

Figure 4-1: Relationship Between Directories and Subdirectories


Login Directory file1 file2 file3 reports subdirectory file1, file2 file3, notes record1 record6

project subdirectory

project2 subdirectory

status subdirectory

ZK0534UR

Like lenames, the maximum length of a directory name depends upon the le system used on your computer. For example, your le system may allow a maximum directory name length of 255 bytes (the default), or it may allow a maximum directory name length of only 14 bytes. Knowing the maximum directory name length is important to help you give meaningful names to your directories. See your system administrator for details. The operating system does not have a symbol or notation that automatically distinguishes between a lename and a directory name, so you may nd it useful to establish your own naming conventions to designate les and directories. However, you can use the ls F command to distinguish between lenames and directory names when the contents of your current directory are displayed. For more information on this command, see Section 4.3.

Managing Directories 43

4.2 Changing Directories (cd)


The cd (change directory) command allows you to switch from your current (working) directory to another directory. You can move to any directory in the le system from any other directory in the le system by executing cd with the proper pathname. Note You must have execute permission to access a directory before you can use the cd command. For information about directory permissions, see Chapter 5. The format of the cd command is:
cd pathname

The pathname entry can either be the full pathname or the relative pathname of the directory that you want to set as your current directory. If you enter the cd command without a pathname, the system returns you to your login directory (also known as your HOME directory). To check the name of and display the path for your current directory, enter the pwd (print working directory) command. See Chapter 2 for information about the pwd command.

4.2.1

Changing Your Current Directory


In the following example, you enter the pwd command to display the name (which is also the pathname) of your working directory. You then use the cd command to change your current directory. First return to your login directory, if necessary, by entering the cd command without a pathname. Next, enter the pwd command to verify that your login directory is your current directory. Remember that the system substitutes the name of your login directory for the notation /u/uname in the example:
$ cd $ pwd /u/uname $

44 Managing Directories

Enter the cd command with the relative pathname project2 to change to the project2 directory:
$ cd project2 $

Enter pwd again to verify that project2 is the current directory. Then, enter cd to return to your login directory:
$ pwd /u/uname/project2 $ cd $

To change your current directory to the status directory, which is a different branch of the le system tree structure, enter the cd command with a full pathname:
$ cd reports/status $ pwd /u/uname/reports/status $

4.2.2

Using Relative Pathname Notation


You can use the following relative pathname notation to change directories quickly: Dot notation (. and ..) Tilde notation (~)

Every directory contains at least two entries that are represented by dot (.) and dot dot (..). These entries refer to directories relative to the current directory: dot (.) dot dot (..) This entry refers to the current directory. This entry refers to the parent directory of your working directory. The parent directory is the directory immediately above the current directory in the le system tree structure.

To display the . and .. entries as well as any les beginning with a period, use the a ag with the ls command.

Managing Directories 45

In the following example, change to the reports directory by changing rst to your login directory and then to the reports directory:
$ cd $ cd reports $

The ls command displays the directory contents as well as the status subdirectory you created earlier:
$ ls file1 $ file2 file3 notes status

Now, execute the ls a command to list all directory entries as well as those that begin with a dot (.) the relative directory names:
$ ls a ./ ../ $ file1 file2 file3 notes status

You can use the relative directory name dot dot (..) to refer to les and directories located above the current directory in the le system tree structure. That is, if you want to move up the directory tree one level, you can use the relative directory name for the parent directory rather than using the full pathname. In the following example, the cd .. command changes the current directory from reports to your login directory, which is the parent directory of reports. Remember that the /u/uname entry represents your login directory.
$ pwd /u/uname/reports $ cd .. $ pwd /u/uname $

To move up the directory structure more than one level, you can use a series of relative directory names, as shown in the following example. The response to the following pwd command, the slash ( / ) entry, represents the root directory.
$ cd ../.. $ pwd / $

46 Managing Directories

In the C shell and the Korn shell, you may use a tilde (~) to specify a users login directory. For example, to specify your own login directory, use the tilde alone as follows:
$ cd ~

The above tilde notation does not save you keystrokes because in all operating system shells you may get the same results by merely entering cd from any place in the le system. However, if you want to access a directory below your login directory, tilde notation can save you keystrokes. For example, to access the reports directory from anywhere in the le system, enter the following:
$ cd ~/reports

Tilde notation is also very useful when you want to access a le or directory either in or below another users login directory. You may not know the precise location of that users login directory, but assuming you have the appropriate permissions, you could get there with a minimum of keystrokes. For example, from any place in the le system, you could specify the login directory of a hypothetical user jones by entering the following:
$ cd ~jones

In addition, if user jones tells you that you can nd a le in the status directory immediately below the login directory, you can access the directory by entering the following:
$ cd ~jones/status

4.2.3

Accessing Directories Through Symbolic Links


When directories are connected through a symbolic link, the parent directory you access with the cd command differs depending upon whether you are specifying the actual directory name or the relative directory name. In particular, using the full pathname to nd the parent of a symbolically linked directory results in accessing the actual parent directory. For example, suppose user2 is working on a le in the /u/user2/project directory, which is the symbolic link to /u/user1/project. To change to the actual parent directory (/u/user2), user2 types the following:
$ cd /u/user2 $ pwd /u/user2 $

Managing Directories 47

If user2 specied the relative directory name (..), the parent directory of the symbolic link would be accessed. For example, suppose user2 is working on the same le in the /u/user2/project directory, which is the symbolic link to /u/user1/project. To access the parent directory of the symbolic link, user2 enters the following:
$ cd .. $ pwd /u/user1 $

Instead of being in the /u/user2 directory, user2 is now in the directory called /u/user1. For background information on symbolic links, see Section 3.4.

4.3 Displaying Directories (ls F)


A directory can contain subdirectories as well as les. To display subdirectories, use the ls F command. This command displays the contents of the current directory and marks each directory with a trailing slash character ( / ) so that it can be readily distinguished from a le. The format of the ls F command is:
ls F

In the following example, return to your login directory and enter the ls F command to display the directory contents. Note that the project, project2, and reports directories are marked with a slash:
$ cd $ ls F file1 file2 $ file3 project/ project2/ record1 record6 reports/

Some C and Korn Shell users dene an alias for the ls command so that whenever they enter ls, the ls F command is executed. For more information about dening aliases, see Chapter 8.

4.4 Copying Directories (cp)


You can use the cp command with the r ag to recursively copy directories and directory trees to another part of the le system. The cp r command has the following format:
cp r source destination

48 Managing Directories

The source entry is the name of the directory to be copied. The destination entry is the name of the directory location to which you want to copy source. Figure 4-2 shows how the cp r command in the following example copies the directory tree reports into the directory project. It is assumed that the command is entered from the login directory:
$ cp r reports project $

Figure 4-2: Copying a Directory Tree


Login Directory file1 file2 file3 reports subdirectory file1, file2 file3, notes record1 record6

project subdirectory

project2 subdirectory

status subdirectory reports subdirectory file1, file2, file3, notes

status subdirectory
ZK0535UR

Note that the reports directory les, file1, file2, file3, and notes, as well as the status subdirectory, have been copied to project.

Managing Directories 49

4.5 Renaming Directories (mv)


You can use the mv command to rename a directory only when that directory is contained in the same disk partition. The format of the mv command is:
mv olddirectoryname newdirectoryname

The olddirectoryname entry is the name of the directory you want to move or rename. The newdirectoryname entry is the new name you want to assign to the original directory name. In the following example, rst change to the reports directory. Then, enter ls i d command to list the i-number for the status directory:
$ cd reports $ ls i d status 1091 status $

Now, enter the mv command to change the name of status to newstatus. Then, list the i-number for the newstatus directory:
$ mv status newstatus $ ls i d newstatus 1091 newstatus $

Notice that the second ls i d command does not list the original directory name status. However, it does list the new directory name, newstatus, and displays the same i-number (1091 in this example) for the new directory as for the original status directory.

4.6 Removing Directories (rmdir)


When you no longer need a particular directory, you can remove it from the le system with the rmdir (remove directory) command. This command removes only empty directories those that contain no les or subdirectories. For information about removing les from directories, see Section 4.6.4 and Section 3.9. The format of the rmdir command is:
rmdir dirname

The dirname entry is the name, or pathname, of the directory you want to remove. Before working through the examples in the following sections, create three subdirectories in the directory project2.

410 Managing Directories

First, use the cd project2 command to set project2 as your current directory. Next, use the mkdir command to create the schedule, tasks, and costs directories. Then, list the contents of the project2 directory:
$ cd project2 $ mkdir costs schedule tasks $ ls -F costs/ schedule/ tasks/ $

Finally, use the cd command to return to your login directory:


$ cd $ pwd /u/uname $

4.6.1

Removing Empty Directories


The rmdir command removes only empty directories. If you try to remove a directory that contains any les or subdirectories, the rmdir command displays an error message, as the following example shows:
$ rmdir project2 rmdir: project2 not empty $

Note You cannot remove a directory while you are positioned in it. To remove a directory, you must be elsewhere in the directory tree. See Section 4.6.3 for more information. Before you can remove the directory project2, you must rst remove the contents of that directory. In the following example, the cd command makes project2 your current directory, and the ls -F command lists the contents of project2:
$ cd project2 $ ls -F costs/ schedule/ tasks/

Now remove the directory schedule from the current directory, and then list the remaining contents of the project2 directory:
$ rmdir schedule $ ls -F costs/ tasks/ $

Managing Directories 411

The project2 directory still contains two subdirectories: costs and tasks. You can remove them by using pattern-matching characters, as described in the next section. Once these subdirectories are removed, you can delete the project2 directory, as described in Section 4.6.3.

4.6.2

Removing Multiple Directories


You can remove more than one directory at a time with the rmdir command by using pattern-matching characters. See Chapter 2 for detailed information about pattern-matching characters. For example, suppose that you are in the project2 directory and want to remove two subdirectories: costs and tasks. To do so, enter the rmdir *s?s command. Then, enter the ls command to verify that the project2 directory contains no entries:
$ rmdir *s?s $ ls $

Caution Entering the rmdir command with the asterisk (*) character alone removes ALL empty directories from your current directory. Use the asterisk (*) pattern-matching character with care.

4.6.3

Removing Your Current Directory


You cannot remove your current directory while you are still working in it. You can remove it only after you move into another directory. You generally enter the dot dot (..) command to move into the parent directory of your current directory, and then enter rmdir with the pathname of the target directory. The directory project2 is empty. To remove project2, rst move to your login directory, which is the parent directory of project2. Then, use the rmdir dirname command to remove project2, and enter ls to conrm the removal:
$ cd $ rmdir project2 $ ls file1 file2 file3 $

project/

record1

record6

reports/

412 Managing Directories

Your login directory no longer contains the project2 directory.

4.6.4

Removing Files and Directories Simultaneously (rm r)


The rmdir command removes only directories, not les. You can, however, remove les and directories at the same time by using the rm command with the r (recursive) ag. The rm r command rst deletes the les from a directory and then deletes the directory itself. It deletes the directory you specify as well as any subdirectories (and the les they contain) below it on the directory tree. This command should be used with caution. The format of the rm r command is:
rm r pathname

The pathname entry can either be the full pathname or the relative pathname of the directory that you want to remove. You may also use pattern-matching characters to specify les. Caution Be certain that you understand how the r ag works before you use it. For example, entering the rm r * command from your login directory deletes all les and directories to which you have access. If you have superuser authority and are in the root directory, this command will delete all system les. See Section 5.7 for more information about superuser authority. When using the rm r command to remove les or directories, it is a good idea to include the i ag in the command line:
rm ri pathname

When you enter the command in this form, the system prompts you for verication before actually removing the specied item(s). In this way, by answering y (yes) or n (no) in response to the prompt, you control the actual removal of a le or directory. Keep in mind that using the ri option may require you to reply to many, many prompts (depending upon how many les you have).

Managing Directories 413

Controlling Access to Your Files and Directories

This chapter shows you how to control access to your system as well as your les and directories. After reading this chapter, you will be able to: Understand password, group, and system security issues Understand le and directory permissions Display and set le and directory permissions Change owners and groups Change your identity to access les Understand superuser concepts Learn where to nd information about enhancements to security that may be installed on your system

A good way to learn about the topics in this chapter is to do the examples so that the information on your screen is consistent with the information in this book. Before you can work through the examples, you must be logged in and your login directory should be in the state that you left it after doing the examples in Chapter 4. Your login directory should contain: The les file1, file2, file3, record1, and record6 The subdirectory reports that contains the file1, file2, file3, and notes les and the subdirectory newstatus The project subdirectory that contains the les file1, file2, file3, and notes as well as the subdirectory status

If you are using les with different names, make the appropriate substitutions as you work through the examples.

5.1 Understanding Password and Group Security Files


Before a user can log in successfully, the user must be made known to the system by the creation of a user account. Adding a user account is a routine but critical activity that is usually performed by the system administrator.

When a user account is created, information about the new user is added to the following two les: /etc/passwd This le contains individual user information for all users of the system. /etc/group This le contains group information for all groups on the system.

These les dene who can use the system and each users access rights. In addition, all other system security controls depend upon password and group security. The following sections describe the /etc/passwd and /etc/group les.

5.1.1

The /etc/passwd File


The /etc/passwd le contains records that dene login accounts and attributes for all system users. This le can be altered only by a user with superuser privileges. See Section 5.7 for more information. Each record in the /etc/passwd le denes a login account for an individual user. The elds are separated by colons and the last eld ends with a newline character. The following text shows the format of an /etc/passwd le entry and describes the meaning of each eld:
username:password:UID:GID:user_info:login_directory:login_shell

username password

Your login name. Your password stored in encrypted form. Encryption prevents unauthorized users or programs from discovering your actual password. If no password has been specied for a user, this eld will be blank. (User ID) A unique number identifying you to the system. (Group ID) A number identifying your default group. You can belong to one or more groups. This eld can contain the following: your full name, maximum le size (a number limiting the maximum size of any le you create or extend), and site specic information (an attribute serving various purposes for each installation - it normally records biographical information).

UID GID user_info

login_directory Your current directory after logging in to the system. It is usually a directory you own and use to store private les. login_shell The program run by the login program after you successfully log in to the system. It is normally a shell

52 Controlling Access to Your Files and Directories

program used to interpret commands. For more information on shells, see Chapter 7 and Chapter 8. The following example is a sample entry in the /etc/passwd le:
lee:NebPsa9qxMkbD:201:20:Lee Voy,sales,x1234:/users/lee: \ /usr/bin/sh

The user account lee has user ID 201 and group ID 20. Lees full name is Lee Voy, and his department and telephone are listed. The login directory is /users/lee and the Bourne shell (/usr/bin/sh) is dened as the command interpreter. The password eld contains Lees password in encrypted form.

5.1.2

The /etc/group File


The /etc/group le denes login accounts for all groups using the system. This le can be altered only by a user with superuser privileges. See Section 5.7 for more information. Each record in the group database denes the login account of one group. Groups provide a convenient way to share les among users with a common interest or who are working on the same project. Each entry in the /etc/group le is a single line that contains four elds. The elds are separated by colons, and the last eld ends with a newline character. The following text shows the format of each entry and describes the meaning of each eld:
groupname:password:GID:user1[, user2, ..., userN ]

groupname password GID usernames

A unique character string that identies the group to the system. This eld is always empty. Entries in this eld are ignored. (Group ID) A unique number that identies the group to the system. A list of users who belong to the group.

5.2 Protecting Files and Directories


The operating system has a number of commands that enable you to control access to your les and directories. You can protect a le or directory by setting or changing its permissions, which are codes that determine the way in which anyone working on your system can use the stored data.

Controlling Access to Your Files and Directories 53

Setting or changing permissions is also referred to as setting or changing the protections on your les or directories. You generally protect your data for one or both of the following reasons: Your les and directories contain sensitive information that should not be available to everyone who uses your system. Not everyone who has access to your les and directories should have the permission to alter them. Caution Your system may allow two or more users to make changes to the same le at the same time without informing them. If this is so, the system saves the changes made by the last user to close the le; changes made by the other users are lost (some text editors warn users of this situation). It is therefore a good idea to set le permissions to allow only authorized users to modify les. The specied users should then communicate about when and how they are using the les. Each le and each directory has nine permissions associated with it. Files and directories have the following three types of permissions: r (read) w (write) x (execute)

These three permissions occur for each of the following three classes of users: u (user/owner) g (group) o (all others; also known as "world")

The r permission allows users to view or print the le. The w permission allows users to write to (modify) the le. The x permission allows users to execute (run) the le or to search directories. The user/owner of a le or directory is generally the person who created it. If you are the owner of a le, you can change the le permissions with the chmod command, which is described in Section 5.4. The group species the group to which the le belongs. If you are the owner of a le, you can change the group ID of the le with the chgrp command, which is described in Section 5.8.

54 Controlling Access to Your Files and Directories

Note If you do not own a le, you cannot change its permissions or group ID unless you have superuser authority. See Section 5.7 for more information. The meanings of the three types of permissions differ slightly between ordinary les and directories. See Table 5-1 for more information. Table 5-1: Differences Between File and Directory Permissions
Permission r (read) For a File Contents can be viewed or printed. Contents can be changed or deleted. File can be used as a program. For a Directory Contents can be read, but not searched. Normally r and x are used together. Entries can be added or removed. Directory can be searched.

w (write) x (execute)

5.3 Displaying File and Directory Permissions (ls)


To display the current le permissions, enter the ls command with the l ag. To display the permissions for a single le or selected les, enter the following command:
$ ls l filename

The filename entry can be the name of the le or a list of lenames separated by spaces. You may also use pattern-matching characters to specify les. See Section 5.4.1.3 for more information. To display the permissions for all of the les in your current directory, enter the ls l command:
$ ls l total 7 -rw-r--r--rw-r--r--rw-r--r-drwxr-xr-x -rw-r--r--rw-r--r-drwxr-xr-x $ 1 1 1 2 1 1 2 larry larry larry larry larry larry larry system system system system system system system 101 171 130 32 0 0 32 Jun Jun Jun Jun Jun Jun Jun 5 5 5 5 5 5 5 10:03 10:03 10:06 10:07 11:03 11:03 10:31 file1 file2 file3 project record1 record6 reports

Controlling Access to Your Files and Directories 55

The rst string of each entry in the directory shows the permissions for that le or directory. For example, the fourth entry, drwxr-xr-x, shows the following: That this is a directory (the d notation) That the owner can view it, write in it, and search it (the rwx sequence) That the group can view it and search it, but not write in it (the rst r-x sequence) That all others can view it and search it, but not write in it (the second r-x sequence)

The third eld shows the les owner, (in this case, larry), and the fourth eld shows the group to which the le belongs, (in this case, system). To list the permissions for a single directory, use the ls ld command:
$ ls ld reports drwxr-xr-x 2 larry $ system 32 Jun 5 10:31 reports

Taken together, all the permissions for a le or directory are called its permission code. As Figure 5-1 shows, a permission code consists of four parts: A single character shows the le type. The dash () indicates an ordinary le, d a directory, and l a symbolic link. Any other character indicates an I/O (Input/Output) device. A 3-character permission eld shows user (owner) permissions, which may be any combination of read, write, and execute. Another 3-character permission eld shows group permissions. Another 3-character permission eld shows permissions for all others.

56 Controlling Access to Your Files and Directories

Figure 5-1: File and Directory Permission Fields


permission type owner rwx group rwx others rwx

(file) d l b c p s (directory) (symbolic link) (block special file) (character special file) (named pipe special file) (local socket special file)

r read w write x execute

ZK0536UR

When you create a le or directory, the system automatically supplies a predetermined permission code. The following is a typical le permission code:
-rw-r--r--

This le permission code species that the owner has read and write permissions while the group and all others have read permission. The dashes () in some positions following the le-type notation indicate that the specied class of user does not have permission for that operation. The following is a typical directory permission code:
drwxr-xr-x

This directory permission code species that owner has read, write, and search permissions, while the group and all others have read and search permissions. The default permission codes that your system provides relieve you from the task of specifying them explicitly every time you create a le or directory. If you want to create your own default permission codes, you must change your user mask with the umask command. For an explanation of the umask command, see the description of the command in Section 5.5.

Controlling Access to Your Files and Directories 57

5.4 Setting File and Directory Permissions (chmod)


Your ability to change permissions gives you a great deal of control over the way your data can be used. Use the chmod (change mode) command to set or change the permissions for your les and directories. For example, you obviously permit yourself to read, modify, and execute a le. You generally permit members of your group to read a le. Depending upon the nature of your work and the composition of your group, you often allow them to modify or execute it. You generally prohibit all other system users from having any access to a le. Note You must be the owner of the le or directory (or have superuser authority) before you can change its permissions. This means that your username must be in the third eld in an ls l listing of that le. It is important to realize that whatever restrictions you impose on le and directory access, the superuser can always override them. For example, if you use the chmod command to specify that only you can have access to the report20 le. The superuser can still access this le. For more information on this topic, see Section 5.7. There are two ways to specify the permissions set by the chmod command: You can specify permissions with letters and operation symbols. You can specify permissions with octal numbers.

The following sections describe how to specify permissions with letters and operation symbols, as well as with octal numbers.

5.4.1

Specifying Permissions with Letters and Operation Symbols


You can use letters and operation symbols to change le and directory permissions. The following is the format of the chmod command when using letters and operation symbols:
chmod userclass-operation-permission lename

58 Controlling Access to Your Files and Directories

The userclass-operation-permission entry actually represents three codes that specify the user class, group, operation, and permission code that you want to activate. The filename entry is the name of the le or les whose permissions you want to change. You may also use patternmatching characters to specify les. See Section 5.4.1.3 for more information. User classes, operations, and permissions are dened as follows: Use one or more of these letters to represent the userclass: u g o a + = r s w x 5.4.1.1 User (owner) Group All others (besides owner and group) All (user, group, and all others) Add permission Remove permission Assign permission regardless of previous setting Read Set user or group ID Write Execute

Use one of these symbols to represent the operation:

Use one or more of these letters to represent the type of permission:

Changing File Permissions In the following example, rst enter the ls l command to display the permissions for the file1 le:
$ ls l file1 -rw-r--r-1 larry $ system 101 Jun 5 10:03 file1

The owner (larry) has read/write permissions while the group and others have only read permissions.

Controlling Access to Your Files and Directories 59

Now, enter the chmod command with the ags go+w. This command expands the permissions for both the group (g) and for others (o) by giving them write access (+w) to file1 in addition to the read access they already have:
$ chmod go+w file1 $

Next, list the new permissions for the le:


$ ls l file1 -rw-rw-rw- 1 larry $ system 101 Jun 5 10:03 file1

You have given your group and all other system users write permission to file1. 5.4.1.2 Changing Directory Permissions The procedure for changing directory permissions is the same as that for changing le permissions. However, to list the information about a directory, you use the ls ld command:
$ ls ld project drwxr-xr-x 2 larry $ system 32 Jun 5 10:07 project

Now change the permissions with the chmod g+w command so that the group (g) has write permission (+w) for the directory project:
$ chmod g+w project $ ls ld project drwxrwxr-x 2 larry $ system 32 Jun 5 10:07 project

5.4.1.3

Using Pattern-Matching Characters If you want to make the same change to the permissions of all entries in a directory, you can use the pattern-matching character asterisk (*) with the chmod command. For information on pattern-matching characters, see Chapter 2. In the following example, the command chmod g+x * gives execute (x) permission to the group (g) for all les (*) in the current directory:

510 Controlling Access to Your Files and Directories

$ chmod g+x * $

Now enter the ls l command to show that the group now has execute (x) permission for all les in the current directory:
$ ls l total 7 -rw-rwxrw-rw-r-xr--rw-r-xr-drwxrwxr-x -rw-r-xr--rw-r-xr-drwxr-xr-x $ 1 1 1 2 1 1 2 larry larry larry larry larry larry larry system system system system system system system 101 171 130 32 0 0 32 Jun Jun Jun Jun Jun Jun Jun 5 5 5 5 5 5 5 10:03 10:03 10:06 10:07 11:03 11:03 10:31 file1 file2 file3 project record1 record6 reports

5.4.1.4

Setting Absolute Permissions An absolute permission assignment (=) resets all permissions for a le or les, regardless of how the permissions were set previously. In the following example, the ls l command lists the permissions for the file3 le. Then the chmod a=rwx command gives all three permissions (rwx) to all users (a).
$ ls l file3 -rw-r-x-r-- 1 larry $ chmod a=rwx file3 $ ls l file3 -rwxrwxrwx 1 larry $ system 130 Jun 5 10:06 file3

system

130 Jun 5 10:06 file3

You can also use an absolute assignment to remove permissions. In the following example, the chmod a=rw newfile command removes the execute permission (x) for all groups (a) from the file3 le:
$ chmod a=rw file3 $ ls l file3 -rw-rw-rw1 larry $ system 130 Jun 5 10:06 file3

5.4.2

Specifying Permissions with Octal Numbers


You can also use octal numbers to change le and directory permissions. To use octal number permission codes with the chmod command, enter the command in the following form:
chmod octalnumber lename

The octalnumber entry is a 3-digit octal number that species the permissions for owner, group, and others. The filename entry is the name

Controlling Access to Your Files and Directories 511

of the le whose permissions you want to change. It can be the name of the le or a list of lenames separated by spaces. You may also use patternmatching characters to specify les. See Section 5.4.1.3 for more information. An octal number corresponds to each type of permission: 4 = read 2 = write 1 = execute To specify a group of permissions (a permissions eld), add together the appropriate octal numbers (r, w, and x denote read, write, and execute respectively): 3= 6= 7= 0= -wx (2 + 1) rw- (4 + 2) rwx (4 + 2 + 1) --- (no permissions)

Table 5-2 lists the eight possible permission combinations. Table 5-2: Permission Combinations
Octal Number 0 1 2 3 4 5 6 7 Binary Number 000 001 010 011 100 101 110 111

Permissions None --x -w-wx r-r-x rwrwx

Description No permissions granted Execute Write Write/execute Read Read/execute Read/write Read/write/execute

The entire permission code for a le or directory is specied with a 3-digit octal number, one digit each for owner, group, and others. Table 5-3 shows some typical permission codes and how they relate to the permission elds.

512 Controlling Access to Your Files and Directories

Table 5-3: How Octal Numbers Relate to Permission Fields


Octal Number 777 755 700 666 Owner Field rwx rwx rwx rwGroup Field rwx r-x --rwOthers Field rwx r-x --rwComplete Code rwxrwxrwx rwxr-xr-x rwx-----rw-rw-rw-

For example, you could use the following commands to change the permission of file3 using octal numbers:
$ ls l file3 -rw-rw-rw- 1 larry $ chmod 754 file3 $ ls l file3 -rwxr-xr-- 1 larry $ system 130 Jun 5 10:06 file3

system

130 Jun 5 10:06 file3

5.5 Setting Default Permissions with the User Mask (umask)


Every time you create a le or a directory, default permissions are established for it. These default permissions are initially established either by the operating system or the program you are running (both will be considered to be the creating program in the umask description that follows). Setting default permissions relieves you from the task of specifying permission codes explicitly every time you create a le or directory. The operating system assigns the default permission values of 777 for executable les and 666 for all other les. If you want to further restrict whatever permissions are established by a program when it creates a le or directory, you must specify a user mask with the umask command. The user mask is a numeric value that determines the access permissions when a le or directory is created. As a result, when you create a le or directory, its permissions are set to what the creating program species, minus what the umask value forbids. The umask command has the following format:
umask octalnumber

Controlling Access to Your Files and Directories 513

The octalnumber entry is a 3-digit octal number that species the permissions to be subtracted from the default permissions (777 or 666). Setting the user mask is very similar to setting the permission bits discussed in Section 5.4.2. The permission code for a le or directory is specied with a 3-digit octal number. Each digit represents a type of permission. The position of each digit (rst, second, or third) represents 3 bits that correspond to the following: The rst digit is for the owner of the le (you). The second digit is for the group of the le. The third digit is for others.

When you set the umask, you are actually specifying which permissions are not to be granted regardless of the permissions requested by the le creating program. Table 5-4 lists the eight possible umask permission combinations for easy reference. Note that the umask permission values are the inverse of those specied for regular permission codes. Also note that these permission values are applied to those set by the creating program. Table 5-4: The umask Permission Combinations
Allowed Permissions rwx rwr-x r--wx -w--x none

Octal Number 0 1 2 3 4 5 6 7

Description Read/write/execute Read/write Read/execute Read Write/execute Write Execute No permissions granted

For example, if you specify a user mask of 027 (and the le is executable): The owner is allowed all permissions requested by the program creating the le. The group is not allowed write permission.

514 Controlling Access to Your Files and Directories

The others are not allowed any permissions.

A good user mask value to set for your own les and directories depends upon how freely information resources are shared on your system. The following guidelines may be useful: In a very open computing environment, you might specify 000 as a user mask value, which allows no restrictions on le/directory access. As a result, when a program creates a le and species permission codes for it, the user mask imposes no restrictions on what the creating program has specied. In a more secure computing environment, you might specify 066 as a user mask value, which allows you total access, but prevents all others from being able to read or write to your les. As a result, when a le is created, its permissions are set to what the creating program species, minus the user mask restrictions that prevent read/write access for everyone but you. In a very secure computing environment, you might specify 077 as a user mask value, which means that only you have access to your les. As a result, when a le is created, its permissions are set to what the creating program species, minus the user mask restrictions that prevent anyone else from reading, writing, or executing your les.

To show you how umask works, assume that you have entered the following command:
$ umask 037

This command establishes a permission code of 740 (if the le is executable) and produces the following results: You (the owner) are allowed all permissions. Members of your group are not allowed write and execute permissions. All others are not allowed any permissions.

Further, assume that you have just created a le. By default, your editor always assigns the following default permissions: owners are allowed all permissions, and all others only read and execute permissions. However, since you have previously set a user mask of 037, it further restricts the le permissions. As a result, the owner still has all permissions, but the group cannot execute the le, and all others have no permissions.

5.5.1

Setting the umask


You may activate the umask command in two ways:

Controlling Access to Your Files and Directories 515

Include the umask command in your login script. This is the most common and efcient way to specify your user mask because the specied value is set automatically for you whenever you log in. For a discussion of login scripts, see Chapter 7. For examples of umask commands in login scripts, see Chapter 8. Enter the umask command at the shell prompt during a login session. The user mask value you set is in effect for that login session only.

For a more detailed example of how the user mask works in restricting permissions for les you create with a text editor, follow the steps in this procedure: 1. Enter the following command to nd out what the current value of your user mask is:
$ umask

If the user mask value is 000, there are no restrictions on the permissions established by le-creating programs. Go to step 3. If the user mask value is set, write it down. Go to step 2. 2. Set the user mask value to 000 so that that there will be no restrictions on the permissions established by le-creating programs. Before resetting the user mask, make sure you have written down the current value in case you need to reset it. Enter the following command:
$ umask 000

3. Create a le, save it, and then exit your editor. 4. Display the permissions of the le by using the ls l command. We will assume for the sake of the example that read/write permissions are granted for all users:
$ ls -l -rw-rw-rw$ 1 user-name 15 Oct 27 14:42 yourfile

5. Reset the user mask to 022 by entering the following command:


$ umask 022

A user mask of 022 establishes the following permission restrictions: owners are allowed all permissions and all others are allowed only read and execute permissions. 6. Create another le, save it, and then exit your editor.

516 Controlling Access to Your Files and Directories

7. Display the permissions of the le by entering the ls l command:


$ ls l -rw-r--r-$ 1 user-name 15 Oct 27 14:45 yourfile2

Notice that the write permissions for the group and all others have been removed in accordance with the user mask value of 022. 8. Reset the user mask to its original value or to another value (if you choose). Note A user with superuser privileges can override whatever access restrictions you impose on les and directories. For more information on this topic, see Section 5.7. On occasion, the results you obtain when specifying a user mask may vary from what you intended. If so, see your system administrator. The operating system provides a default user mask value of 022, which allows the owner all permissions, but prevents members of your group or any other users from writing to your les. However, your systems user mask default may vary.

5.6 Changing Your Identity to Access Files


The su command allows you to alter your identity during a login session. A reason for altering your identity is to be able to access les that you do not own. To protect system security, you should not assume another identity without the owners or the system administrators permission. The su command allows you to log in to another users account only if you know that users password. The su command authenticates you and then resets both the processs user ID and the effective user ID to the value of the newly specied user ID. The effective user ID is the user ID currently in effect for the process, although it may not be the user ID of the person logged in. The format of the su command is:
su username

The username entry is the username of the person whose identity you want to assume. If after altering your identity, you want to conrm what identify you have assumed, use the whoami command. This command displays the username of the identity you have assumed.

Controlling Access to Your Files and Directories 517

After completing your work under a new identity, you should return to your own login identity. To do so, press Ctrl/D or enter the exit command. The following example shows how Juan assumes Lucys identity with the su command, conrms it with the whoami command, removes a le, and then returns to his own login identity with the exit command:
$ su lucy Password: ... $ whoami lucy $ rm file9 $ exit $ whoami juan $

For more information, see the su(1) and whoami(1) reference pages.

5.7 Superuser Concepts


Every system has a superuser who has permissions that supersede those of ordinary users. This superuser is often referred to as root. The root user has absolute power over the running of the system. This user has access to all les and all devices and can make any changes to the system. The root user is said to have superuser privileges. The following is a list of tasks ordinarily performed by root users: Edit les not normally changeable by ordinary users (for example, /etc/passwd) Be able to change ownership and permissions of all les Execute restricted commands like mount or reboot Kill any process running on your system Add and remove user accounts Boot and shut down the system Back up the system

Many of the preceding tasks are performed by system administrators who require superuser privileges. The system administrators job is to manage the system by performing the preceding tasks, installing new software, analyzing system performance, and reporting hardware failures. Depending upon your computing environment, you may or may not be the system administrator for your system or have root privileges. Your site conguration as well as your job responsibilities will determine your privileges.

518 Controlling Access to Your Files and Directories

If you work from a terminal or workstation that accesses a centralized system, you will probably not be the system administrator or have root privileges. In this situation, the system administrator, who is in charge of maintaining, conguring, and upgrading the system, will be the person who has root privileges. If you perform your tasks from a workstation that is either independent or networked to other workstations or systems, you may indeed have root privileges for your own workstation, but not be the system administrator of your site. In this situation, you would maintain your own workstation only. However, the system administrator would still maintain shared machines and networks. To become a root user, use the su command. You must also know the password for the root user. The format of the su command is:
su root

The following example shows how Juan becomes a root user to perform an administrative task:
$ su root Password: ... # _

The new prompt, a number sign (#), indicates that Juan has become a root user and that a shell has been created for his use. The root user shell (often the C shell) is dened in the /etc/passwd le. Juan may now perform the administrative task. Caution Because the root user has absolute power over the system, the password should be carefully protected. Otherwise, unauthorized use of the system may result in corruption or destruction of data. After completing your work as the root user, you should return to your own login identity. To do so, press Ctrl/D or enter the exit command. You are then returned to the system prompt.

5.8 Changing Owners and Groups (chown and chgrp)


In addition to setting permissions, you can control how a le or directory is used by changing its owner or group. Use the chown command to change the owner and the chgrp command to change the group.

Controlling Access to Your Files and Directories 519

Note In order to use the chown command, you must have superuser privileges. For more information on this topic, see Section 5.7. Enter the chown command in the following form:
chown owner lename

The owner entry is the username of the new owner of the le. The filename entry is a list of one or more les whose ownership you want to change. You may also use pattern-matching characters to specify les. See Section 5.4.1.3 for more information. Enter the chgrp command in the following form:
chgrp group le

The group entry is the group ID or group name of the new group. Note that to change the group ownership of a le, you must be a member of the group to which you are changing the le. The file entry is a list of one or more les whose ownership you want to change. For more information, see the chown(1) and chgrp(1) reference pages.

5.9 Additional Security Considerations


The security guidelines enforced at your site protect your les from unauthorized access. See your system administrator for complete information about security guidelines. In addition, it is wise to avoid running untrusted software (software that is from an unknown source or that has not been validated for system security). When you run a program, that program has all of your access rights, and nothing prevents the program from being used to illicitly access, observe, or alter sensitive les. You should be aware of three types of programs that compromise security: Trojan horse A trojan horse is a program that performs, or appears to perform, its dened task properly; however, it also performs hidden functions that may be malicious. A trojan horse program emulates the program that you intended to run, but may perform an unwanted action. It might vandalize your les by altering or deleting them, or compromise the les by making illegal copies of them. A typical trojan horse is the login trojan horse, which mimics the systems login prompt on the display and waits for you to enter a username and password. The program mails or copies this information to the user responsible for the trojan horse. As the trojan horse exits, it
520 Controlling Access to Your Files and Directories

displays Login incorrect. The real login program then runs. Most users assume they typed the password incorrectly, and are unaware that they were deceived. Computer worm A computer worm is a program that moves around a computer network, making copies of itself. For example, a login computer worm can log in to a system, copy itself onto the system, start running, log in to another system, and then continue this process indenitely. Computer virus A computer virus program is really a type of trojan horse. Normally, a trojan horse waits passively for the right user to run it (usually a privileged user). Viruses spread by inserting themselves in other executable les, thus increasing the threat and extent of compromise of privacy or integrity. Be careful of programs that were not installed by the person who administers your system. Programs that are obtained from bulletin boards and other unknown origins are particularly suspect. Even if the program includes source code, it is not always possible to examine the program carefully enough to determine if it is trustworthy.

Controlling Access to Your Files and Directories 521

Using Processes

This chapter explains the operating system processes. After completing this chapter, you will be able to: Understand programs and processes Redirect process input, output, and errors Run processes in the foreground and background Check the status of processes Cancel processes Display information about users and their processes

A good way to learn about the preceding topics is to try the examples in this chapter. You should do each example so that the information on your screen is consistent with the information in this book.

6.1 Understanding Programs and Processes


A program is a set of instructions that a computer can interpret and run. You may think of most programs as belonging to one of two categories: Application programs such as text editors, accounting packages, or electronic spreadsheets Programs that are components of the operating system such as commands, the shell (or shells), and your login procedure

While a program is running, it is called a process. The operating system assigns every process a unique number known as a process identier. The operating system can run a number of different processes at the same time. When more than one process is running, a scheduler built into the operating system gives each process its fair share of the computers time, based on established priorities.

6.2 Understanding Standard Input, Output, and Error


When a process begins executing, the operating system opens three les for the process: stdin (standard input), stdout (standard output), and stderr (standard error). Programs use these les as follows:

Standard input is the place from which the program expects to read its input. By default, processes read stdin from the keyboard. Standard output is the place to which the program writes its output. By default, processes write stdout to the screen. Standard error is the place to which the program writes its error messages. By default, processes write stderr to the screen.

In most cases, the default standard input, output, and error mechanisms will serve you well. However, there are times when it is useful to redirect the standard input, output, and error. The following sections describe these procedures.

6.2.1

Redirecting Input and Output


A command usually reads its input from the keyboard (standard input) and writes its output to the display (standard output). You may want a command to read its input from a le, write its output to a le, or both. You can select input and output les for a command with the shell notation shown in Table 6-1. This notation can be used in all shells. Table 6-1: Shell Notation for Reading Input and Redirecting Output
Notation < > >> Action Reads standard input from a le Writes standard output to a le Appends (adds) standard output to the end of a le Example wc <file3 ls >file3 ls >>file3

The following sections explain how to read input from a le and how to write output to a le. 6.2.1.1 Reading Input from a File All shells allow you to redirect the standard input of a process so that input is read from a le, instead of from the keyboard. You can use input redirection with any command that accepts input from stdin (your keyboard). You cannot use input redirection with commands, such as who, that do not accept input.

62 Using Processes

To redirect input, use the left-angle bracket (<), as the following example shows:
$ wc <file3 3 $ 27 129

The wc (word count) command counts the number of lines, words, and bytes in the named le. So file3 contains 3 lines, 27 words, and 129 bytes. If you do not supply an argument, the wc command reads its input from the keyboard. In this example, however, input for wc comes from the le named file3. Note that in the preceding example, you could have entered the following, and displayed the same output:
wc file3

This is because most commands allow the input le to be specied without the left-angle bracket (<). However, there are a few commands like mail that require the use of the left-angle bracket (<) for special functions. For example, note the following command:
$ mail juan <report

This command mails to the user juan the le report. For more information about mail, see the mail(1) reference page. 6.2.1.2 Redirecting Output All shells allow you to redirect the standard output of a process from the screen (the default) to a le. As a result, you can store the text generated by a command into a new or existing le. To send output to a le, use either a right-angle bracket (>) or two rightangle brackets (>>). The right-angle bracket (>) causes the shell to: Replace the contents of the le with the output of the command, if the le exists. Create the le, if the le does not exist and place the output of the command into the le.

Two right-angle brackets (>>) add (append) the output of the command to the end of a le that exists. If you use two right-angle brackets (>>) to write output to a le that does not exist, the shell creates the le containing the output of the command.

Using Processes 63

In the next example, the output of ls goes to the le named file:


$ ls >file $

If the le already exists, the shell replaces its contents with the output of ls. If file does not exist, the shell creates it. In the following example, the shell adds the output of ls to the end of the le named file:
$ ls >>file $

If file does not exist, the shell creates it. In addition to their standard output, processes often produce error or status messages known as diagnostic output. For information about redirecting diagnostic output, see the following section.

6.2.2

Redirecting Standard Error to a File


When a command executes successfully, it displays the results on the standard output. When a command executes unsuccessfully, it displays error messages on the default standard error le, the screen. However, the shell allows you to redirect the standard error of a process from the screen to a le. Redirection symbols and syntax vary among shells. The following sections describe standard error redirection for the Bourne, Korn, and C shells.

6.2.2.1

Bourne and Korn Shell Error Redirection The general format for Bourne and Korn shell standard error redirection is the following:
command 2> errorle

The command entry is an operating system command. The errorfile entry is the name of the le to which the process writes the standard error. The 2> is a le descriptor digit combined with the right-angle bracket (>), the output redirection symbol. The le descriptor digit tells the shell what standard le to access so that its contents may be redirected. The le descriptor digit 2 indicates that the standard error le is being redirected. In fact, for the Bourne and Korn shells, a le descriptor digit is associated with each of the les a command ordinarily uses: File descriptor 0 (same as the left-angle bracket [<]) species standard input (the keyboard). File descriptor 1 (same as the right-angle bracket [>]) species standard output (the screen).

64 Using Processes

File descriptor 2 species standard error (screen).

In the following example, an error is redirected to the error le when the ls command attempts to display the nonexistent le, reportx. The contents of error le are then displayed:
$ ls reportx 2> error $ cat error reportx not found $

Although only standard error is redirected to a le in the preceding example, typically you would redirect both standard error and standard output. See Section 6.2.3 for more information. For many commands, the difference between standard output and standard error is difcult to see. For instance, if you use the ls command to display a nonexistent le, an error message displays on the screen. If you redirect the error message to a le as in the previous example, the output is identical. 6.2.2.2 C Shell Error Redirection The general format for C shell standard error redirection is the following:
( command> outle)>& errorle

The command entry is any operating system command. The outfile entry is the name of the le to which the process writes the standard output. The right-angle bracket (>) redirects the standard error to a le. The errorfile entry is the name of the le to which the process writes the standard error. Note that in this command format, the parentheses are mandatory.

6.2.3

Redirecting Both Standard Error and Standard Output


In the preceding sections, you learned how to redirect standard output and standard error separately. Usually, however, you would redirect both standard output and standard error at the same time. Standard output and standard error can be written to different les or to the same le. For the Bourne and Korn shells, the general format for redirecting both standard output and standard error to different les is the following:
command> outle2> errorle

The command entry is an operating system command. The outfile entry is the le to which the process writes the standard output. The 2> symbol redirects the error output. The errorfile entry is the le where the process writes the standard error.

Using Processes 65

For the C shell, the general format for redirecting both standard output and standard error to different les is the following:
( command> outle)>& errorle

The command entry is an operating system command. The outfile entry is the le to which the process writes the standard output. The right-angle bracket and ampersand (>&) symbol redirects the error output. The errorfile entry is the le where the process writes the standard error. Note that in this command format, the parentheses are mandatory. See Section 6.2.2.2 for more information. For the Bourne and Korn shells, the general format for redirecting both standard output and standard error to the same le is the following:
command1> outle2>&1

The command entry is an operating system command. The 1> symbol redirects the standard output. The outfile entry is the le to which the process writes the standard output. The 2>&1 symbol tells the shell to write the standard error (le descriptor 2) in the le associated with the standard output (>&1), outfile. For the C shell, the general format for redirecting both standard output and standard error to the same le is the following:
command>& outle

The command entry is an operating system command. The outfile entry is the le to which the process writes the standard output. The right-angle bracket and ampersand (>&) symbol tells the shell to write the standard output and standard error to the same le specied by outfile.

6.3 Running Several Processes Simultaneously


The operating system can run a number of different processes at the same time. This capability makes it a multitasking operating system, which means that the processes of several users can run at the same time. These different processes can be from one or multiple users. As a result, you do not have to enter commands one at a time at the shell prompt. Instead, you can run both foreground and background processes simultaneously. The following sections describe both foreground and background processes.

6.3.1

Running Foreground Processes


Normally, when you enter a command on the command line, you wait for the results to display on your screen. Commands entered singly at the shell prompt are called foreground processes.

66 Using Processes

Most commands take a short time to execute perhaps a second or two. However, some commands require longer execution times. If a long-duration command runs as a foreground process, you cannot execute other commands until the current one nishes. As a result, you may want to run a longduration command as a background process. The following section describes background processes.

6.3.2

Running Background Processes


Generally, background processes are most useful with commands that take a long time to run. Instead of tying up your screen by entering a long-duration command as a foreground process, you can execute a command as a background process. You can then continue with other work in the foreground. To run a background process, you end the command with an ampersand (&). Once a process is running in the background, you can perform additional tasks by entering other commands at your workstation. After you create a background process, the following takes place: The Process Identication Number (PID) is displayed. The operating system creates and assigns PIDs so that all processes currently running on the system can be tracked. (In the Korn or the C shell, job numbers are assigned as well.) The prompt returns so that you can enter another command. In the C shell, a message is displayed when the background process is complete.

When you create a background process, note its PID number. The PID number helps you to monitor or terminate the process. See Section 6.4 for more information. Because background processes increase the total amount of work the system is doing, they may also slow down the rest of the system. This may or may not be a problem, depending upon how much the system slows and the nature of the other work you or others do while background processes run. Most processes direct their output to standard output, even when they run in the background. Unless redirected, standard output goes to your workstation. Because the output from a background process may interfere with your other work on the system, it is usually good practice to redirect the output of a background process to a le or to a printer. Then you can look at the output whenever you are ready. For more information about redirecting output, see the examples later in this chapter as well as Section 6.2.

Using Processes 67

The examples in the remainder of this chapter use a command that takes more than a few seconds to run:
$ find / type f print

This command displays the pathnames for all les on your system. You do not need to study the find command in order to complete this chapter it is used here simply to demonstrate how to work with processes. However, if you want to learn more about the find command, see the find(1) reference page. In the following example, the find command runs in the background (by entering an ampersand [&]) and redirects its output to a le named dir.paths (by using the right-angle bracket [>] operator):
$ find / type f print 24 $ >dir.paths &

When the background process starts, the system assigns it a PID (Process Identication) number (24 in this example), displays it, and then prompts you for another command. Your process number will be different from the one shown in this and following examples. If you use the Korn or C shell, job numbers are assigned as well. In the C shell, the preceding example looks like this:
%find / type f print [1] 24 % _ >dir.paths &

Note that the job number [1] is displayed to the left of the PID number. You can check the status of the process with the ps (process status) or the jobs command (Korn and C shell). You can also terminate a process with the kill command. See the following section for more information about these commands. In the C shell, when the background process is completed, a message is displayed:
[1] 24 Done find / type f print >dir.paths

The completion message displays the job number and the PID, the status Done, and the command executed.

6.4 Monitoring and Terminating Processes


Use the ps (process status) command to nd out which processes are running and to display information about those processes. In the Korn and C shells, you also can use the jobs command to monitor background processes. If you need to stop a process before it is nished, use the kill command.

68 Using Processes

The following sections describe how to monitor and terminate processes.

6.4.1

Checking Process Status


The ps command allows you to monitor the status of all active processes, both foreground and background. In the Korn and C shell, you also can use the jobs command to monitor background processes only. The following sections describe the ps and the jobs command.

6.4.1.1

The ps Command The ps command has the following form:


ps

In the following example, the ps command displays the status of all processes associated with your workstation under the following headings:
$ ps PID 29670 515 28476 790 $ TT p4 p5 p5 p6 STAT I S R I TIME 0:00.00 0:00.00 0:00.00 0:00.00 COMMAND -sh (csh) -sh (csh) ps -sh (csh)

You interpret the display under these entry headings as follows: PID Process identication. The system assigns a process identication number (PID number) to each process when that process starts. There is no relationship between a process and a particular PID number; that is, if you start the same process several times, it will have a different PID number each time. Controlling terminal device name. On a system with more than one workstation, this eld tells you which workstation started the process. On a system with only one workstation, this eld can contain the designation console or the designation for one or more virtual terminals. Symbolic process status. The system displays the state of the process, with a sequence of up to four alphanumeric characters. For more information, see the ps(1) reference page. Time devoted to this process by the computer is displayed in minutes, seconds, and hundredths of seconds starting when you enter ps. The name of the command (or program) that started the process.
Using Processes 69

TT

STAT

TIME

COMMAND

You can also check the status of a particular process by using the p ag and the PID number with the ps command. The general format for checking the status of a particular process is the following:
ps p PIDnumber

The ps command also displays the status of background processes. If there are any background processes running, they will be displayed along with the foreground processes. The following example shows how to start a find background process and then check its status:
$ find / type f print >dir.paths & 25 $ ps p25 PID TTY TIME COMMAND 25 console 0:40.00 find $

You can check background process status as often as you like while the process runs. In the following example, the ps command displays the status of the preceding find process ve times:
$ ps PID 25 $ ps PID 25 $ ps PID 25 $ ps PID 25 $ ps PID 25 $ ps PID $ p25 TTY console p25 TTY console p25 TTY console p25 TTY console p25 TTY console p25 TTY TIME 0:18:00 TIME 0:29:00 TIME 0:49:00 TIME 0:58:00 TIME 1:02:00 COMMAND find COMMAND find COMMAND find COMMAND find COMMAND find

TIME COMMAND

Notice that the sixth ps command returns no status information because the find process ended before the last ps command was entered. Generally, the simple ps command described here tells you all you need to know about processes. However, you can control the type of information that the ps command displays by using more of its ags. One of the most useful ps ags is e, which causes ps to return information about all processes, not just those associated with your terminal or workstation. For an explanation of all ps command ags, see the ps(1) reference page.

610 Using Processes

6.4.1.2

The jobs Command The Korn shell and the C shell display both a job and a PID when a background process is created. The jobs command reports the status of all background processes only, based upon the job number. The jobs command has the following form:
jobs

Adding the l ag displays both the job number and the PID. The following example shows how to start a find process and then check its status in the C shell with the jobs l command:
% find / type f print >dir.paths & [2] 26 % jobs l [2] +26 Running find / type f print >dir.paths & % _

The status message displays both the job ([2]) and the PID number (26), the status Running, and the command executed.

6.4.2

Canceling a Foreground Process (Ctrl/C)


To cancel a foreground process (stop an executing command), press Ctrl/C. The command stops executing, and the system displays the shell prompt. Note that canceling a foreground process is the same as stopping command execution (described in Chapter 1). Most simple operating system commands are not good examples for demonstrating how to cancel a process they run so quickly that they nish before you have time to cancel them. However, the following find command runs long enough for you to cancel it (after the process runs for a few seconds, you can cancel it by pressing Ctrl/C):
$ find / type f print /usr/sbin/acct/acctcms /usr/sbin/acct/acctcoN1 /usr/sbin/acct/acctcon2 /usr/sbin/acct/acctdisk /usr/sbin/acct/acctmerg /usr/sbin/acct/accton /usr/sbin/acct/acctprc1 /usr/sbin/acct/acctprc2 /usr/sbin/acct/acctwtmp /usr/sbin/acct/chargefee /usr/sbin/acct/ckpacct /usr/sbin/acct/dodisk
Ctrl/C

Using Processes 611

The system returns the shell prompt to the screen. Now you can enter another command.

6.4.3

Canceling a Background Process (kill)


If you decide, after starting a background process, that you do not want the process to nish, you can cancel the process with the kill command. Before you can cancel a background process, however, you must know its PID number. If you have forgotten the PID number of that process, you can use the ps command to list the PID numbers of all processes. If you are a C or Korn Shell user, it is more efcient to use the jobs command to list background processes only. The general format for terminating a particular process is the following:
kill PIDnumber

If you want to end all the processes you have started since login, use the kill 0 command. You do not have to know the PID numbers to use kill 0. Because this command deletes all of your processes, use this command carefully. The following example shows how to start another find process, check its status, and then terminate it:
$ find / type f print >dir.paths & 38 $ ps PID TT STAT TIME COMMAND 520 p4 I 0:11:10 sh 38 p5 I 0:10:33 find 1216 p6 S 0:01:14 qdaemon 839 p7 R 0:03:55 ps $ kill 38 $ ps 38 Terminated PID TT STAT TIME COMMAND 520 p4 I 0:11:35 sh 1216 p6 S 0:01:11 qdaemon 839 p7 R 0:03:27 ps $

The command kill 38 stops the background find process, and the second ps command returns no status information about PID number 38. The system does not display the termination message until you enter your next command. Note that in this example, kill 38 and kill 0 have the same effect because only one process was started from this terminal or workstation.

612 Using Processes

In the C shell, the kill command has the following format:


kill % jobnumber

The following example uses the C shell to start another find process, to check its status with the jobs command, and then to terminate it:
% find / type f print >dir.paths & [3] 40 % jobs l [3] +40 Running find / type f print >dir.paths & % kill %3 % jobs l [3] +Terminated find / type f print > dir.paths % _

6.4.4

Suspending and Resuming a Foreground Process (C Shell Only)


Stopping a foreground process and resuming it can be helpful when you have a long-duration process absorbing system resources and you need to do something quickly. Rather than waiting for process completion, you can stop the process temporarily (suspend it), perform your more critical task, and then resume the process. Suspending a process is available for C shell users only. To suspend a process, press Ctrl/Z. A message will display listing the job number, the status suspended, and the command executed. Once you are ready to resume the process, enter:
% n

To resume the process in the background, enter:


% n &

where the n entry is the number of the stopped job. The following example starts a find process, suspends it, checks its status, resumes it, and then terminates it:
% find / type f print >dir.paths & [4] 41 % jobs l [4] +41 Running find / type f print >dir.paths & % Ctrl/Z Suspended % jobs l [4] +Stopped find / type f print > dir.paths % 4 & [4] find / type f print >dir.paths & % kill %4 [4] +Terminated find / type f print > dir.paths

Using Processes 613

Once a process is suspended, you may also resume it by entering the fg command. If a currently running process is taking too long to run and is tying up your keyboard, you can use the bg command to place the process in the background and enter other commands. The following example starts a find process, suspends it, puts the process in the background, copies a le, and then resumes the process in the foreground:
% find / type f print >dir.paths
Ctrl/Z

Suspended % bg [5] find / type f print > dir.paths & % cp salary1 salary2 % fg find / type f print > dir.paths %

6.5 Displaying Information About Users and Their Processes


The operating system provides the following commands that can tell you who is using the system and what they are doing: who w ps au Displays information about currently logged in users. Displays information about currently logged in users and what they are currently running on their workstations. Displays information about currently logged in users and the processes they are running.

The who command allows you to determine who is logged into the system. It may be especially useful, for example, when you want to send a message and want to know whether the person is currently available. In the following example, information about all currently logged in users is displayed:
$ who juan chang larry tony lucy $ tty01 tty05 tty07 tty09 pts/2 Jan Jan Jan Jan Jan 15 15 15 15 15 08:33 08:45 08:55 07:53 11:24

(boston)

Note that the who command lists the username of each user on the system, the workstation being used, and when the person logged in. In addition, if a user is logged in from a remote system, the name of the system is listed. For

614 Using Processes

example, lucy logged in remotely from the system boston on Jan 15 at 11:24. The actual display format of who varies according to your current locale. See Appendix C for more information about locales. The who u command gives all the information of the who command and also displays the PID of each user and the number of hours and minutes since there was activity at a workstation. Activity for less than a minute is indicated by a dot (.). In the following example, all currently logged in users are displayed:
$ who u juan tty01 chang tty05 larry tty07 tony tty09 lucy pts/5 $ Jan Jan Jan Jan Jan 15 15 15 15 15 08:33 08:45 08:55 07:53 11:24 01:02 . . 01:20 . 50 52 58 60 65

(boston)

Note that in the preceding example, juan and tony have been inactive for over an hour, while chang, larry, and lucy have been inactive for less than a minute. Now that you know how to nd out who is active on your system, you may want to nd out what command each person is currently executing. The w command displays the command that is currently running at each users workstation. In the following example, information about all users (the User column) and their current commands (the what column) is displayed:
$ w 11:02 up 3 days, 2:40, 5 users, load average: 0.32, 0.20, 0.00 User tty login@ idle JCPU PCPU what juan tty01 8:33am 12 54 14 csh chang tty05 8:45am 6:20 26 mail larry tty07 8:55 1:58 8 csh tony tty09 7:53 3:10 22 4 mail lucy tty02 11:24 1:40 18 4 csh $

In addition, the w command displays the following information: The tty column: users workstation The login@ column: users login time The idle column: amount of time since the user entered a command The JCPU column: total CPU time used during the current login session The PCPU column: CPU time used by the command that is currently executing

Using Processes 615

On certain occasions, you may want to have a detailed listing of current processes (both foreground and background) and the users who are running them. To get such a listing, use the ps au command. In the following example, information about ve users and their active processes is displayed:
$ ps au USER PID %CPU %MEM juan 26300 16.5 0.8 chang 25821 7.0 0.2 larry 25121 6.1 0.2 tony 11240 4.5 0.6 lucy 26287 0.5 0.1 $ SZ 441 149 107 741 61 RSS 327 64 83 225 28 TT STAT TIME COMMAND p3 R 0:02:01 ps au p4 R 0:12:23 mail n p22 R 26:25:07 tip modem p19 R 1:57:46 emacs p1 S 0:00:00 more

The most important elds for the general user are the USER, PID, TIME, and COMMAND elds. For information on the remaining elds, see the ps(1) reference page.

616 Using Processes

Shell Overview

This chapter introduces you to the operating system shells. After completing this chapter, you will be able to: Understand the purpose and general features of the Bourne, C, and Korn shells Change your shell Use command entry aids common to all shells Understand your shell environment as well as the role of login scripts, environment variables, and shell variables Set and clear environment and shell variables Understand how the shell nds commands on your system Write logout scripts Write and run basic shell procedures

This chapter covers features common to all operating system shells, with some descriptions of shell differences. For detailed information on specic Bourne, C, and Korn shell features, see Chapter 8.

7.1 Purpose of Shells


The user interfaces to the operating system are called shells. The shells are programs that interpret the commands you enter, run the programs you have asked for, and send the results to your screen. The operating system provides the following shells: The Bourne shell (system default) The C shell The Korn shell

You may access any shell, depending upon the security restrictions in effect on your system as well as upon the licensing restrictions of the Korn shell. In any case, all shells perform the same basic function: they allow you to perform work on your system by executing commands.

In addition to interpreting commands, the shell can also be used as a programming language. You can create shell procedures that contain commands. Shell procedures are executed in the same way that you execute a program: on the command line after the shell prompt. When you run a shell procedure, your current shell creates or spawns a subshell. A subshell is a new shell your current shell creates to run a program. Thus, any command the shell procedure executes (for example, cd) leaves the invoking shell unaffected. Shell procedures provide a means of carrying out tedious commands, large or complicated sequences of commands, and routine or repetitive tasks. See Section 7.10 for more information on shell programming.

7.2 Summary of Bourne, C, and Korn Shell Features


The operating system provides the following shells that have both command execution and programming capabilities: The Bourne shell (sh) This is a simple shell that is easily used in programming. It is usually represented by a dollar sign ($) prompt. This shell does not provide either the interactive features or the complex programming constructs (arrays and integer arithmetic) of the C shell or the Korn shell. The Bourne shell also provides a restricted shell (Rsh). For more information, see Section 7.2.2. The C shell (csh) This shell is designed for interactive use. It is usually represented by a percent sign (%) system prompt. The C shell provides some features for entering commands interactively: A command history buffer Command aliases Filename completion Command line editing

For more information on these features, see Section 7.2.1. The Korn shell (ksh) This shell combines the ease of use of the C shell and the ease of programming of the Bourne shell. The system prompt is usually a dollar sign ($) prompt. The Korn shell provides these features: The interactive features of the C shell

72 Shell Overview

The simple programming syntax of the Bourne shell Command line editing The fastest execution time Upward compatibility with the Bourne shell (that is, most Bourne shell programs will run under the Korn shell)

For more information on these features, see Section 7.2.1.

7.2.1

More Information on C and Korn Shell Features


Both the C and the Korn shells offer the following interactive features: Command history The command history buffer stores the commands you enter and allows you to display them at any time. As a result, you can select a previous command, or parts of previous commands, and then reexecute them. This feature may save you time because it allows you to reuse long commands instead of retyping them. In the C shell, this feature requires some setup in the .cshrc le; in the Korn shell this feature is automatically provided. Command aliases The command aliases feature allows you to abbreviate long command lines or rename commands. You do this by creating aliases for long command lines that you frequently use. For example, assume that you often need to move to the directory /usr/chang/reports/status. You could create an alias status that could move you to that directory whenever you enter status on the command line. In addition, aliases allow you to make up more descriptive names for operating system commands. For example, you could dene an alias named rename for the mv command. Filename completion In the C shell, the lename completion feature saves typing by allowing you to enter a portion of the lename. When you press the Escape key, the shell will complete the lename for you. See Section 8.2.4 for more information about lename completion in the C shell. In the Korn shell, you can ask the shell to display a list of le names that match the partial name you entered. You may then choose among the displayed le names. See Section 8.4.5 for more information about lename completion in the Korn shell. The Korn shell provides an inline editing feature that allows you to retrieve a previously entered command and edit it. To use this feature, you must know how to use a text editor such as vi or emacs.

Shell Overview 73

For more information about these shell features, see Chapter 8.

7.2.2

The Restricted Bourne Shell


The operating system enhances system security by providing specied users a limited set of functions with a restricted version of the Bourne shell (Rsh). When these specied users log in to the system, they are given access to the restricted Bourne shell only. Your system administrator determines who has access to the restricted Bourne shell. A restricted shell is useful for installations that require a more controlled shell environment. As a result, the system administrator can create user environments that have a limited set of privileges and capabilities. For example , all users that are guests to your system might be allowed access under the username guest. When logging in to your system, user guest would be assigned a restricted shell. The actions of Rsh are identical to those of sh, except that the following actions are not allowed: Changing directories. The cd command is deactivated. Specifying pathnames or command names containing a slash ( / ). Setting the value of the PATH or the SHELL variables. For more information on these variables, see Section 7.5.2. Redirecting output with the right-angle brackets (> and >>).

For more detailed information on Rsh, see the sh(1) reference page. For information on how system administrators create restricted shells, see your system administrator.

7.3 Changing Your Shell


Whenever you log in, you are automatically placed in a shell specied by your system administrator. However, depending upon the security features in effect on your system, you can enter commands that will allow you to do the following: Determine which shell you are running Temporarily change your shell Permanently change your shell

The following sections describe these operations.

74 Shell Overview

7.3.1

Determining What Shell You Are Running


To determine what shell you are currently running, enter the following command:
echo $SHELL

The lename of the shell you are running will display. In the following example, assume that you are running the Bourne shell (sh):
$ echo $SHELL /usr/bin/sh $

Table 7-1 lists the lename that displays for each shell as well as the default system prompt (your system prompt may vary). Table 7-1: Shell Names and Default Prompts
Shell Bourne Restricted Bourne C Korn Shell Name sh Rsh csh ksh Default Prompt $ $ % $

7.3.2

Temporarily Changing Your Shell


You may experiment with using other shells if the security features on your system allow it. To temporarily change your shell, enter the following command:
shellname

The shellname is the lename of the shell you want to use. See Table 7-1 for valid shell le names to enter on the command line. Once the shell is invoked, the correct shell prompt is displayed. Once you are done using the new shell, you can return to your default shell by entering exit or by pressing Ctrl/D.

Shell Overview 75

For example, assume that the Korn shell is your default shell. To change to the C shell and then back to the Korn shell, enter the following commands:
$ /usr/bin/csh % exit $

Note If you are using the Bourne Restricted Shell, you cannot change to another shell.

7.3.3

Permanently Changing Your Shell


You may permanently change your default shell if the security features on your system allow it. If your current shell is the C shell, use the chsh command to change your default shell. If you dont use the C shell, change your default shell by contacting your system administrator. In the C shell, enter the following command to change the default shell:
% chsh Changing login shell for user. Old shell: /usr/bin/csh New shell:

Enter the name of the new shell. See Table 7-1 for valid shell names to enter on the command line. After entering the chsh command, you must log out and log in again for the change to take effect.

7.4 Command Entry Aids


The following features of all operating system shells help you do your work: The ability to enter multiple commands and command lists Pipes and lters The ability to group commands Quoting

76 Shell Overview

7.4.1

Using Multiple Commands and Command Lists


The shell usually takes the rst word on a command line as the name of a command and then takes any other words as arguments to that command. The shell usually considers each command line as a single command. However, you can use the operators in Table 7-2 to execute multiple commands on a single command line. Table 7-2: Multiple Command Operators
Operator ; && || | Action Causes commands to run in sequence. Runs the next command if the current command succeeds. Runs the next command if the current command fails. Creates a pipeline. Example

cmd1 ; cmd2 cmd1 && cmd2 cmd1 || cmd2 cmd1 | cmd2

The following sections describe running commands in sequence (;), running commands conditionally (|| and &&), and using pipelines ( | ). 7.4.1.1 Running Commands in Sequence with a Semicolon (;) You can enter more than one command on a line if you separate commands with the semicolon (;). In the following example, the shell runs ls and waits for it to nish. When ls is nished, the shell runs who, and so on through the last command:
$ ls ; who ; date ; pwd change file3 newfile amy console/1 Jun 4 14:41 Tue Jun 4 14:42:51 CDT 1996 /u/amy $

If any one command fails, the others still execute successfully. To make the command line easier to read, separate commands from the semicolon (;) with blanks or tabs. The shell ignores blanks and tabs used in this way.

Shell Overview 77

7.4.1.2

Running Commands Conditionally When you connect commands with two ampersand (&&) or pipe (||) operators, the shell runs the rst command and then runs the remaining commands only under the following conditions: && The shell runs the next command only if the current command completes (a command indicates successful completion when it returns a value of zero). The shell runs the next command only if the current command does not complete.

||

The syntax for the two ampersand (&&) operator follows:


cmd1 && cmd2 && cmd3 && cmd4 && cmd5

If cmd1 succeeds, the shell runs cmd2. If cmd2 succeeds, the shell runs cmd3, and on through the series until a command fails or the last command ends. (If any command fails, the shell stops executing the command line). The syntax for the pipe (||) operator follows:
cmd1 || cmd2

If cmd1 fails, then the shell runs cmd2. If cmd1 succeeds, the shell stops executing the command line. For example, suppose that the command mysort is a sorting program that creates a temporary le (mysort.tmp) in the course of its sorting process. When the sorting program nishes successfully, it cleans up after itself, deleting the temporary le. If, on the other hand, the program fails, it may neglect to clean up. To ensure deletion of mysort.tmp, enter the following command line:
$ mysort || rm mysort.tmp $

The second command executes only if the rst command fails.

7.4.2

Using Pipes and Filters


A pipe is a one-way connection between two related commands. One command writes its output to the pipe, and the other process reads its input from the pipe. When two or more commands are connected by the pipe ( | ) operator, they form a pipeline. Figure 7-1 represents the ow of input and output through a pipeline. The output of the rst command (cmd1) is the input for the second command (cmd2); the output of the second command is the input for the third command (cmd3).

78 Shell Overview

Figure 7-1: Flow Through a Pipeline


Cmd1 Cmd2 (filter) Cmd3 (filter)
ZK0537UR

A lter is a command that reads its standard input, transforms that input, and then writes the transformed input to standard output. Filters are typically used as intermediate commands in pipelines that is, they are connected by a pipe ( | ) operator. For example, to cause the ls command to list recursively the contents of all directories from the current directory to the bottom of the hierarchy, and then to display the results, enter the following command:
$ ls R | pg

In this example, the pg command is the lter because it transforms the output from the ls R command and displays it one screen at a time. Certain commands that are not lters have a ag that causes them to act like lters. For example, the diff (compare les) command ordinarily compares two les and writes their differences to standard output. The usual format for diff follows:
diff le1 le2

However, if you use the dash () ag in place of one of the le names, diff reads standard input and compares it to the named le. In the following pipeline, ls writes the contents of the current directory to standard output. The diff command compares the output of ls with the contents of a le named dirfile, and writes the differences to standard output one page at a time (with the pg command):
$ ls | diff dirfile | pg

In the following example, another kind of lter program (grep) is used:


$ ls l | grep r-x | wc l 12 $

In this example, the following takes place: The ls l command lists in long format the contents of the current directory. The output of ls l becomes the standard input to grep r-x, a lter that searches for the les in its standard input for patterns with permissions of r-x, and writes all lines that contain the pattern to its standard output.
Shell Overview 79

The standard output of grep r-x becomes the standard input to wc l, which displays the number of les matching the grep criteria in the standard input.

To get the same results without using a pipeline, you would have to do the following: 1. Direct the output of ls l /user to a le. For example:
$ ls l >file1

2. Use file1 as input for grep r-x and redirect the output of grep to another le. For example:
$ grep r-x file1 >file2

3. Use the output le of grep as input for wc l. For example:


$ wc l file2

As the preceding procedure demonstrates, using a pipeline is a much easier way to perform the same operations. Each command in a pipeline runs as a separate process. Pipelines operate in one direction only (left to right), and all processes in a pipeline can run at the same time. A process pauses when it has no input to read or when the pipe to the next process is full.

7.4.3

Grouping Commands
The shell provides two ways to group commands, as shown in Table 7-3. Table 7-3: Command Grouping Symbols
Symbols (commands) {commands} Action The shell creates a subshell to run the grouped commands as a separate process. The shell runs the grouped commands as a unit. Braces can only be used in the Korn and Bourne shells.

The following sections describe the command grouping symbols of Table 7-3 in greater detail.

710 Shell Overview

7.4.3.1

Using Parentheses ( ) In the following command grouping, the shell runs the commands enclosed in parentheses as a separate process:
$ (cd reports;ls);ls

The shell creates a subshell (a separate shell program) that moves to the reports directory and lists the les in that directory. After the subshell process is complete, the shell lists the les in the current directory (ls). If this command were written without the parentheses, the original shell would move to the reports directory, list the les in that directory, and then list the les in that directory again. There would be no subshell and no separate process for the cd reports;ls command. The shell recognizes the parentheses wherever they occur in the command line. To use parentheses literally (that is, without their command-grouping action), quote them by placing a backslash ( \ ) immediately before either the open parenthesis [(] or the close parenthesis [)], for example, \( . For more information on quoting in the shell, see Section 7.4.4. 7.4.3.2 Using Braces { } Using braces is valid only in the Bourne and Korn shells. When commands are grouped in braces, the shell executes them without creating a subshell. In the following example, the shell runs the date command, writing its output to the today.grp le, and then runs the who command, writing its output to today.grp :
$ { date; who }>today.grp $

If the commands were not grouped together with braces, the shell would write the output of the date command to the display and the output of the who command to the le. The shell recognizes braces in pipelines and command lists, but only if the left brace is the rst character on a command line.

7.4.4

Quoting
Reserved characters are characters such as the left-angle bracket (<), the right-angle bracket (.), the pipe (|), the ampersand (&), the asterisk (*), and the question mark (?) that have a special meaning to the shell. See Chapter 8 for lists of reserved characters for each operating system shell. To use a reserved character literally (that is, without its special meaning), quote it with one of the three shell quoting conventions, as shown in Table 7-4.

Shell Overview 711

Table 7-4: Shell Quoting Conventions


Quoting Convention \ " " Action Backslash Quotes a single character. Single quotes Quotes a string of characters (except the single quotation marks themselves). Double quotes Quotes a string of characters (except $, , and ).

The following sections describe the quoting conventions of Table 7-4 in greater detail. 7.4.4.1 Using the Backslash ( \ ) To quote a single character, place a backslash ( \ ) immediately before that character, as in the following:
$ echo \? ? $

This command displays a single question mark (?) character. 7.4.4.2 Using Single Quotes ( ) When you enclose a string of characters in single quotes, the shell takes every character in the string (except the itself) literally. Single quotes are useful when you do not want the shell to interpret: Reserved characters such the dollar sign ($), the grave accent (`), and the backslash (\) Variable names

The following example shows how single quotes are used when you want to display a variable name without having it being interpreted by the shell:
$ echo The value of $USER is $USER The value of $USER is amy $

The echo command displays the variable name $USER when it appears within single quotes, but interprets the value of $USER when it appears outside the single quotes. For information on variable assignments, see Section 7.7.1.

712 Shell Overview

7.4.4.3

Using Double Quotes (" ") Double quotes (" ") provide a special form of quoting. Within double quotes, the reserved characters dollar sign ($), grave accent (`), and backslash (\) keep their special meanings. The shell takes literally all other characters within the double quotes. Double quotes are most frequently used in variable assignments. The following example shows how double quotes are used when you want to display brackets (normally reserved characters) in a message containing the value of the shell variable:
echo "<<Current shell is $SHELL>>" <<Current shell is /usr/bin/csh>> $

For information on variable assignments, see Section 7.7.1.

7.5 The Shell Environment


Whenever you log in, your default shell denes and maintains a unique working environment for you. Your environment denes such characteristics as your user identity, where you are working on the system, and what commands you are running. Your working environment is dened by both environment variables and shell variables. Your default login shell uses environment variables and passes them to all processes and subshells that you create. Shell variables are valid only for your current shell and are not passed to subshells. The following sections discuss the shell environment, how it is congured, and how you can tailor it.

7.5.1

The login Program


Whenever you log in, the login program is run. This program actually begins your login session using data stored in the /etc/passwd le, which contains one line of information about each system user. The /etc/password le contains your username, your password (in encrypted form), your home directory, and your default shell. For more information on the /etc/passwd le, see Chapter 5. The login program runs after you enter your username at the login: prompt. It performs the following functions: Displays the Password: prompt (if you have a password). Veries the username and password you entered against what is contained in the /etc/passwd le. Assigns default values to the shell environment.

Shell Overview 713

Starts running the shell process. Runs system login scripts and your personal login scripts. See Section 7.6 for more information.

7.5.2

Environment Variables
Your shell environment denes and maintains a unique working environment for you. Most of the characteristics of your working environment are dened by environment variables. Environment variables consist of a name and a value. For example, the environment variable for your login directory is named HOME, and its value is dened automatically when you log in. Some environment variables are set by the login program, and some can be dened in the login script that is appropriate for your shell. For example, if you use the C shell, environment variables will typically be set in the .cshrc login script. For more information on login scripts, see Section 7.6. Table 7-5 lists selected environment variables that can be used by all operating system shells. Most of the values of these variables are set during the login process, and are then passed to each process that you create during your session. Table 7-5: Selected Shell Environment Variables
Environment Variable HOME

Description Species the name of your login directory, the directory that becomes the current directory upon completion of a login. The cd command uses the value of HOME as its default value. The login program sets this variable, and it cannot be changed by the individual user. Species your login name. Species the pathname of the le used by the mail system to detect the arrival of new mail. The login program sets this variable based upon your username. Species the directories and the directory order that your system uses to search for, nd, and execute commands. This variable is set by your login scripts. Species your default shell. This variable is set by login using the shell specied in your entry in the /etc/passwd le.

LOGNAME MAIL

PATH

SHELL

714 Shell Overview

Table 7-5:
Environment Variable TERM TZ

(continued)
Description Species the type of terminal you are using. This variable is usually set by your login script. Species the current time zone and difference from Greenwich mean time. This variable is set by the system login script. Species the locale of your system, which is comprised of three parts: language, territory, and character code set. The default value is the C locale, which implies English for language, U.S. for territory, and ASCII for code set. LANG can be set in a login script. Species the collating sequence to use when sorting names and when character ranges occur in patterns. The default value is the ASCII collating sequence. LC_COLLATE can be set in a login script. Species the character classication rules used in the ctype functions for the current locale. The default value is the classication for ASCII characters. LC_TYPE can be set in a login script. Species the language used for yes/no prompts. The default value is American English, but your system may specify another language. Species the monetary format for your system. The default value is the American format for monetary gures. LC_MONETARY can be set in a login script. Species the numeric format for your system. The default value is the American format for numeric quantities. LC_NUMERIC can be set in a login script. Species the date and time format for your system. The default value is the American format for dates and times. LC_TIME can be set in a login script.

LANG

LC_COLLATE

LC_CTYPE

LC_MESSAGES

LC_MONETARY

LC_NUMERIC

LC_TIME

Many of these environment variables can be set during the login process by the appropriate login script (see Section 7.6). However, you may reset them as well as set those for which no default values have been provided. See Section 7.7.1 for more information. For more information on the LANG, LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONETARY, LC_NUMERIC, and LC_TIME variables, refer to Appendix C which explains the variables in the context of other system features that support the languages and customs of different countries.

Shell Overview 715

You may also create your own environment variables. For example, some systems have more than one mail program available to users. Assume that mail and mh are available on your system and that each has its own pathname. As a result, you could dene a variable for the pathname of each mail program. For more information about environment variables specic to each operating system shell, see Chapter 8. For a complete list of operating system shell environment variables, see the sh(1), csh(1), and ksh(1) reference pages.

7.5.3

Shell Variables
Shell variables are valid only for your current shell and are not passed to subshells. Consequently, they can be used only in the shell in which they are dened. In other words, they may be thought of as local variables. Shell variables can be accessed outside of the current shell by becoming environment variables. For more information about environment variables, see Section 7.7.1. You may also create your own shell variables. For example, some mail programs use the pager variable to dene the program that displays mail. Suppose that your mail program is mailx. You could dene the PAGER variable to use the more program to display your mail. For information on how to set shell variables, see Section 7.7.1.

7.6 Login Scripts and Your Environment


A login script is a le that contains commands that set up your user environment. There are two kinds of login scripts: System login scripts for all users of a particular shell. These scripts create a default environment for all users and are maintained by your system administrator. The Bourne and Korn shells use a system login script called /etc/profile. The C shell uses a script called /etc/csh.login. See Table 7-6 for the pathnames of system login scripts. When you log in, the commands in this le are executed rst. Local login scripts in your default login directory. These scripts allow you to tailor your environment, and you maintain the appropriate le. For example, you could change the default search path or shell prompt. If your default shell (see Section 7.3) is the Bourne or Korn shell, the login script le is called .profile. The C shell uses the le

716 Shell Overview

called .login. A local login script is executed after the system login script. If the C shell is your default, your environment can be further tailored with the .cshrc le. It executes when you log in (before .login) and whenever you spawn a subshell. The .cshrc le is the C-shell mechanism that automatically makes variables available to subshells. On startup, the Korn shell will also execute any le pointed to by the ENV environment variable. This variable is typically set in the .profile le and is set to another le, usually in the $HOME directory. Some users prefer to call this le .kshrc or .envfile. To use such a le, place a line like this in your .profile le:
ENV=~/.kshrc

Such a le typically contains shell variables, alias denitions, and function denitions. This le will be referred to as .kshrc for the remainder of this document. Creating your own login script is not mandatory as the system login script for your shell provides a basic environment. Your system administrator may have created a local login script that you can modify with a text editor. When you are new to the system, you may want to use the default environment established for you. However, as you become more familiar with the system, you may want to create or modify your own login script. Table 7-6 lists the system login and local login scripts for each operating system shell. All scripts for a given shell run whenever you log in to your system. In addition, when you enter csh at any shell prompt or execute a C shell script, the .cshrc le executes and creates an environment for the C subshell. Table 7-6: System and Local Login Scripts
System Login Script /etc/profile /etc/profile /etc/csh.login Local Login Script .profile .profile ENV .login .cshrc

Shell Bourne Korn C

Pathname /usr/bin/sh /usr/bin/ksh /usr/bin/csh

To determine if you have any local login scripts in your home directory, use the ls a command. This command displays all les that begin with a dot

Shell Overview 717

(.) as well as all other entries. The following customization features are commonly set in the local login scripts: Terminal characteristics Search path and other environment variables Shell variables Maximum permissions for new les with umask (see Chapter 5) Allowing or stopping messages to your workstation The trap command (Bourne and Korn shells only) Command aliases, history variables (C and Korn shells only) Displaying system status information and other messages Checking for mail Checking for news

It is a good idea to check the contents of your system login script so that you can avoid duplication in your local login script. For example, if your system login script checks for news, there is no need to do the same in your local login script. See Chapter 8 for specic examples of Bourne, Korn, and C login scripts.

7.7 Using Variables


All operating system shells use environment and shell variables to dene user environment characteristics. As part of the set-up process, your system administrator has provided default environment and shell variable values in the appropriate login scripts. For most users, the default environment and shell variable values are sufcient. As you become more familiar with the system, however, you may want to modify some values. For example, you may want to reset the variable that denes your shell prompt so that it is more personalized. Or you may want to set a shell variable that species a very long directory pathname so that you can save time keying commands that use the directory (see the examples in Section 7.7.1). Or you may nd setting variables useful when writing shell procedures. You will nd that you may use variables creatively to enhance your work environment. Some environment variables may be reset and some are read-only and cannot be reset. That is, these variables can be used, but not modied. For more information on this topic, see the appropriate shell reference page; sh(1), csh(1), or ksh(1).

718 Shell Overview

To reset environment variables as well as dene your own shell variables, do one of the following: Edit the appropriate local login script if you want these values set for you whenever you log in. For more information, see Section 7.6. Set the environment variables on the command line if you want these values set only for the current login session.

At any time, you may reference the value of any variable as well as display its value. You may also clear the value of any variable.

7.7.1

Setting Variables
The following sections describe how to set, reference, display, and clear variable values.

7.7.1.1

Bourne and Korn Shell Variables In the Bourne and Korn shells, you set variables with an assignment statement. The general format for setting variables is the following:
name= value

The name entry species the variable name. The value entry species the value assigned to the variable. Be sure you do not enter spaces on the command line. For example, you can create a variable called place by assigning it a value of U. S. A. with the following statement:
$ place=U. S. A. $

From then on, you can use the variable place just as you would use its value. For a more useful example, assume that you are using the Bourne shell and that you temporarily want to personalize your shell prompt. The default Bourne shell prompt is a dollar sign ($) set by the PS1 environment variable. To set it to What Shall I Do Next? >, enter the following command:
$ PS1=What Shall I Do Next? > What Shall I Do Next? >

If you want to make the shell prompt available to subshells, enter the

Shell Overview 719

following command:
$ export PS1

This What Shall I Do Next? > prompt will be in effect throughout your session. If you want to make the new prompt more permanent, enter the same assignment statement and the export command in your .profile le. When you export a shell variable, it becomes an environment variable. As another example, to save keying time you want to dene a variable for a long pathname that you use often. To dene the variable reports for the directory /usr/sales/shoes/women/retail/reports, enter the following:
$ reports=/usr/sales/shoes/women/retail/reports

To reference the variable after setting it, enter a dollar sign ($) before the variable name. For more information on referencing variables, see Section 7.7.2. You can now use the variable reports in any commands you enter during this session. If you want to make this variable permanent, enter the same assignment statement in your .profile le. 7.7.1.2 C Shell Variables In the C shell, you set environment variables with the setenv command. The general format of the setenv command is the following:
setenv name value

The name entry species the variable name. The value entry species the value assigned to the variable. For a example of setting the PATH environment variable, see Section 7.8. You set shell variables with the set command. The format of the set command is:
set name= value

The name entry species the variable name. The value entry species the value assigned to the variable. If the value entry contains more than one part (has spaces), enclose the whole expression in single quotes (). For example, assume that you want to change your prompt. The default C shell prompt is a percent sign (%). To set it to Ready? >, enter the

720 Shell Overview

following on the command line:


% set prompt=Ready? > Ready? >

The Ready? > prompt will be in effect throughout your session. If you execute another shell from the Ready? > prompt, you will get the new shells prompt. To make the new prompt permanent, enter the same command in your .cshrc le. 7.7.1.3 Setting Variables in All Shells To set or reset environment or shell variables in any operating system shell, do one of the following: Edit the appropriate local login script if you want these values set for you whenever you log in. For more information about login scripts, see Section 7.6. Set them on the command line if you want these values set only for the current login session.

7.7.2

Referencing Variables (Parameter Substitution)


To reference the value of a variable in a command line, enter a dollar sign ($) before the variable name. The dollar sign ($) causes the shell you are using to substitute the value of the variable for the variable name. This is known as parameter substitution. For example, assume that you have previously dened the variable sales for the long pathname /user/reports/Q1/march/sales, and that you want to use this variable with the cd command. To do so, enter the cd command with the sales variable:
$ cd $sales $

Then, enter the pwd command to verify that the directory is changed:
$ pwd /user/reports/Q1/march/sales $

In this example, the shell substitutes the actual pathname of the directory /user/reports/Q1/march/sales for the variable name sales.

7.7.3

Displaying the Values of Variables


You can display the value of any variable currently set in your shell. Variable values can be displayed either singly or as a group.

Shell Overview 721

To display the value of a single variable, use the echo command in the following general format:
echo $variable

The variable entry identies the variable you want to display. For example, assume that you use the Korn shell and want to display the value of the SHELL environment variable. To do so, enter the following command:
$ echo $SHELL /usr/bin/ksh $

For the Bourne and Korn shells, to display the value of all currently set variables, use the set command without any options. For example, the following example lists the currently set values in the Bourne shell (your output will vary):
$ set EDITOR=emacs HOME=/users/chang LOGNAME=chang MAIL=/usr/mail/chang PATH=:/usr/bin:/usr/bin/X11 PS1=$ SHELL=/usr/bin/sh TERM=xterm $

For the C shell, to display the value of all currently set shell variables, use the set command without any options. To display the value of all currently set environment variables, use the setenv command or the printenv command without any options.

7.7.4

Clearing the Values of Variables


You may remove the value of most any current variable. Please note, however, that the following variables cannot be cleared:

PATH PS1 (Bourne and Korn shell) PS2 (Bourne and Korn shell) MAILCHECK (Bourne and Korn shell) IFS (Bourne and Korn shell)

For more information on these variables, see the appropriate shell reference pages; sh(1), csh(1), or ksh(1).

722 Shell Overview

In the Bourne and Korn shells, you can clear both environment and shell variables with the unset command. The format of the unset command is:
unset name

The name entry species the variable name. In the C shell, you clear environment variables with the unsetenv command. The format of the unsetenv command is:
unsetenv name

The name entry species the variable name. You clear shell variables with the unset command. The format of the unset command is:
unset name

The name entry species the variable name. For an example, assume that you use the Korn shell and have created a variable called place and have assigned it a value of U. S. A.. To clear the variable, enter the following:
$ unset place $

For more detailed information about setting and referencing variables, see the appropriate shell reference pages; sh(1), csh(1), or ksh(1).

7.8 How the Shell Finds Commands


Every time you enter a command, your shell searches through a list of directories to nd the command. This list of directories is specied by the PATH environment variable. At many installations, system administrators specify default PATH directories for new users. However, more experienced users may need to change these PATH directories. The PATH variable contains a list of directories to search, separated by colons (:). The order in which the directories are listed is the search order that the shell uses to search for the commands that you enter. To determine the value of PATH, use the echo command. For example,

Shell Overview 723

assume that you are using the C shell and have entered the following:
% echo $PATH /usr/bin:/usr/bin/X11 % _

This output from the echo command (your output may vary) tells you that the search order of the preceding example is the following: The /usr/bin directory is searched rst. The /usr/bin/X11 directory is searched second.

Typically, PATH is set as an environment variable in the appropriate login script. In the Bourne and Korn shells, the PATH variable is normally set in the .profile script. In the C shell, it is normally set in the .login script. If you want to change the search path, you can assign a new value to the PATH variable. For example, assume that you use the Bourne shell and that you have decided to use your own versions of some operating system commands. As a result, you want to add $HOME/usr/bin/ to the search path. To do so, enter the following on the command line if you want the new PATH variable value to be in effect for the current login session:
$ export PATH=$HOME/usr/bin:/usr/bin:/usr/bin/X11 $

If you want this new PATH variable value to be in effect for all future sessions, modify the PATH variable in your .profile script. When you next log in, the changes you have made in your .profile script will take effect.

7.9 Using Logout Scripts


You can create a logout script that automatically runs every time you end your session. Just like login scripts, the .logout le must reside in your home directory. You can use logout scripts for the following purposes: To clear your screen To display a logout message To run long background processes after you log out To run a le cleanup routine

To create a logout script, do the following: 1. Create a le called .logout in your home directory with a text editor. 2. Place the commands you want in the le. See Section 7.9.2 for ideas.

724 Shell Overview

3. Save the text and exit the editor. 4. Enter the following command to ensure that the .logout le has the appropriate executable permissions:
$ chmod u+x .logout $

Using a .logout le is not mandatory; it is a convenience that may enhance your work environment.

7.9.1

Logout Scripts and the Shell


If you are using the C shell, the .logout script executes automatically when you log out. If you are using the Bourne or the Korn shell and want to use a logout script, you must ensure that a special trap is set in your .profile script. A trap is a command sequence that looks for a specied signal from a terminal, and then runs a specied command or set of commands. If the following line is not set in your .profile script, you must add it with a text editor:
trap $HOME/.logout 0

This statement tells your system to run the .logout script whenever it receives a zero (0) signal, which occurs automatically when you log out.

7.9.2

A Sample .logout File


The following example .logout le does the following: Clears the screen Displays a logout message that provides the name of your system, your username, and the logout time Displays a parting message Runs a le cleanup routine in the background after you log out

Note that lines beginning with the number sign (#) are comment lines that describe the commands below them.
# Clear the screen clear # Display the name of your system, your username, # and the time and date that you logged out echo hostname : whoami logged out on date # Run the find command in the background. This command # searches your login directory hierarchy for all

Shell Overview 725

# temporary files that have not been accessed in # 7 days, and then deletes them. find ~ name *.tmp atime +7 exec rm {} \; & # A parting message echo "Good Day. Come Back Soon"

7.10 Using Shell Procedures (Scripts)


In addition to running commands from the command line, the shell can read and run commands contained in a le. Such a le is called a shell procedure or shell script. Shell procedures are easy to develop, and using them can help you work more efciently. For example, you may nd shell procedures useful because you can place frequently used commands in one le, and then execute them by entering only the name of the procedure. As a result, they are useful for doing repetitious tasks that would normally require entering many commands on the command line. Because shell procedures are text les that do not have to be compiled, they are easy to create and to maintain. Each shell has its own native programming language. The following are some programming language features that apply to all shells: Storing values in variables Testing for predened conditions Executing commands repeatedly Passing arguments to a program

For more information on specic programming features of your shell, see Chapter 8.

7.10.1

Writing and Running Shell Procedures


To write and run a shell procedure, do the following: 1. Create a le of the commands you need to accomplish a task. Create this le as you would any text le: with vi or another editing program. The le can contain any system command or shell command (described in the sh(1), csh(1), or ksh(1) reference pages). 2. Use the chmod +x command to give the le x (execute) status. For example, the command chmod g+x reserve gives execute status to the le named reserve for any user in your group (g). See Chapter 5 for information on using the chmod command.

726 Shell Overview

3. Run the procedure by entering its name. Enter the pathname if the procedure le is not in your current directory. The following example shows a simple shell procedure named lss that sorts ls l command output by le size:
# ! /usr/bin/csh # lss: sort and list ls l sort n +4

Table 7-7 describes each line in lss. Table 7-7: Description of Example Shell Script
Shell Command #! /usr/bin/csh #lss: list and sort ls l | sort n +4 Description Species the shell where the shell procedure should run.a Comment line that describes the purpose of the shell procedure. These are the commands in the shell procedure. This procedure lists the les in a directory (ls l). Output from the ls l command is then piped to the sort command ( | sort n +4). This command skips over the rst four columns of the ls l output, sorts the fth column (the le size column) numerically, and writes the lines to the standard output.

Table notes: a. See Section 7.10.2 for more information. To run the lss procedure, enter lss. Sample system output looks similar to the following:
$ lss -rw-rw-rw-rw-rw-rw-rw-rw-rw$ 1 larry 1 larry 1 larry system system system 65 Mar 13 14:46 file3 75 Mar 13 14:45 file2 101 Mar 13 14:44 file1

Shell Overview 727

7.10.2

Specifying a Run Shell


At times, you may want to specify the shell where a shell procedure should run. This is because of possible syntactic differences between the shells, but is especially true of differences between the C shell and the other shells. By default, the operating system assumes that any shell procedure you run should be executed in the same shell as your login shell. For example, if your login shell is the Korn shell, by default your shell procedures will run in that same shell. The ability to override the default is very useful for shell procedures that many users run because it ensures that the procedure executes in the correct shell, regardless of the users login shell. To change this default run shell, include the following command as the rst line of the shell procedure:
#!shell_path

The shell_path entry species the full pathname of shell where you want the procedure to run. For example, if you want a shell procedure to run under the C shell, the rst line of the procedure should be the following:
#!/usr/bin/csh

728 Shell Overview

Shell Features

This chapter functions as a reference source for C, Bourne, and Korn shell features. Unlike other chapters of this guide that present conceptual and/or tutorial information, the purpose of this chapter is to provide very brief reference information about each shell. To get the most out this chapter, you should already be familiar with the introductory shell overview information in Chapter 7. After completing this chapter, you should be able to: Understand the main differences between operating system shells Understand specic features of each operating system shell Understand the specics of local login scripts for each shell

8.1 Comparison of C, Bourne, and Korn Shell Features


Table 8-1 compares C, Bourne, and Korn shell selected features. Table 8-1: C, Bourne, and Korn Shell Features
Feature Shell programming Description A programming language that includes features such as loops, condition statements, and variables. Mechanisms for trapping interruptions and other signals sent by the operating system. A security feature that provides a controlled shell environment with limited features. C Yes Bourne Yes Korn Yes

Signal trapping

Yes

Yes

Yes

Restricted shells

No

Yes

No

Table 8-1:
Feature

(continued)
Description A feature that allows you to abbreviate long command lines or to rename commands. A feature that stores commands and allows you to edit and reuse them. A feature that allows you to enter a portion of a lename and the system automatically completes it or suggests a list of possible choices. A feature that allows you to edit a current or previously entered command line. The ability to group data and call it by a name. The ability to perform arithmetic functions within the shell. Facilities for monitoring and accessing background processes. C Yes Bourne No Korn Yes

Command aliases

Command history

Yes

No

Yes

Filename completion

Yes

No

Yes

Command line editing

Yes

No

Yes

Array Integer arithmetic

Yes Yes

No No

Yes Yes

Job control

Yes

No

Yes

For detailed information on shell features, see the appropriate shell reference pages; sh(1), csh(1), or ksh(1).

82 Shell Features

8.2 C Shell Features


This section describes the following C shell features: Sample .cshrc and .login scripts Metacharacters Command history and aliases Built-in variables and commands

8.2.1

Sample .cshrc and .login Scripts


The .cshrc login script sets up your C shell environment by dening variables and operating parameters for the local shell process. The .login script denes variables and operating parameters that you want executed at the beginning of your session, and that you want to be valid for all shell processes during the current login session. When you log in, the operating system executes the .cshrc le in your home directory rst, and the .login le second. The .login script is executed only when you log in. However, the .cshrc le is executed each time you create a subshell. In the following .cshrc script, shell variables, command aliases, and command history variables are set. Table 8-2 explains each part of the script.
# Set shell variables set noclobber set ignoreeof set notify # Set alias alias alias command aliases h history \!* | more l ls l c clear

# Set history variables set history=40 set savehist=40 # Set prompt set prompt = \! %

Shell Features 83

Table 8-2: Example .cshrc Script


Command Shell Variables set noclobber Stops les from being overwritten. If set, places restrictions on output redirection > to ensure that les are not accidentally destroyed, and that >> redirections refer to existing les. Species that you cannot use Ctrl/D to end your login session. Instead, you must use either the exit or the logout commands. Informs you when background processes have completed. Description

set ignoreeof

set notify Command Aliases alias h history \!* | more

Denes the contents of the command history buffer through the more command. The \!* string species that all the history buffer should be piped. Denes a short name, l, for the ls l command that lists directory les in the long format. Denes a short name, c, for the clear command that clears your screen.

alias l ls l

alias c clear History Variables history=40 savehist=40

Instructs the shell to store the last 40 commands in the history buffer. Instructs the shell to store the last 40 commands and use them as the starting history for the next login session.

Prompt Variable set prompt = \! % Changes your prompt so that it tells you the command number of the current command.

84 Shell Features

In the following .login script, the permissions for le creation are set, the PATH environment variable is set, and the editor and printer are specied. Table 8-3 explains each part of the script.
# Set file creation permissions umask 027 # Set environment variables setenv PATH=/usr/bin:/usr/local/bin: set cdpath=.:..:$HOME setenv EDITOR emacs setenv MAILHOST boston setenv PRINTER sales

Table 8-3: Example .login Script


Command File Permissions umask 027 Species the permissions to be subtracted from the default permissions set by the creating program for all new les created. The umask value is subtracted from 777 (for executable programs) or from 666. For an executable program, a umask value of 027 results in all permissions for the owner, read and execute permissions for members of the same group, and no permissions for all others. Description

Environment Variables setenv PATH /usr/bin:/usr/local/bin: Species the search path. In this case, /usr/bin is searched rst, and /usr/local/bin is searched second.

set cdpath=.:..:$HOME

cdpath is a variable that sets the search path for the cd command. This variable assignment species that the cd command should search for the named directory in the current directory (.) rst, in the parent directory (..) second, and the home directory ($HOME) third.

Shell Features 85

Table 8-3:
Command

(continued)
Description Species the emacs editor as the default editor when running a program that allows you to edit a le. For example, various mail programs allow you to use an editor to compose and edit messages. Species boston as your mail handling system. Species the printer sales as your default printer.

setenv EDITOR emacs

setenv MAILHOST boston setenv PRINTER sales

8.2.2

Metacharacters
Table 8-4 describes C shell metacharacters (characters that have special meaning to the shell). Table 8-4: C Shell Metacharacters
Metacharacter Syntactic ; | && || () & Filename / ? * [ ] Separates the parts of a les pathname. Matches any single character except a leading dot (.). Matches any sequence of characters except a leading dot (.). Matches any of the enclosed characters. Separates commands that should be executed sequentially. Separates commands that are part of a pipeline. Runs the next command if the current command succeeds. Runs the next command if the current command fails. Groups commands to run as a separate process in a subshell. Runs commands in the background. Description

86 Shell Features

Table 8-4:

(continued)
Description Species a home directory when used at the beginning of lenames.

Metacharacter ~ Quotation ...

Species that any of the enclosed characters should be interpreted literally; that is, without their special meaning to the shell. Provides a special form of quoting. Species that the $ (dollar sign), `(grave accent), and \ (backslash) characters keep their special meaning, while all other enclosed characters are interpreted literally; that is, without their special meaning to the shell. Double quotes are useful in making variable assignments.

"..."

Input/Output < > << >> >& >>& >! Redirects input. Redirects output to a specied le. Redirects input and species that the shell should read input up to a specied line. Redirects output and species that the shell should add output to the end of a le. Redirects both diagnostic and standard output and appends them to a le. Redirects both diagnostic and standard output to the end of an existing le. Redirects ouput and species that if the noclobber variable is set (prevents overwriting of les); it should be ignored so that the le can be overwritten.

Substitution $ ! : ^ Species variable substitution. Species history substitution. Precedes substitution modiers. Used in special kinds of history substitution. Species command substitution.

Shell Features 87

8.2.3

Command History
The command history buffer stores the commands you enter and allows you to display them at any time. As a result, you can select a previous command, or parts of previous commands, and then reexecute them. This feature may save you time because it allows you to reuse long commands instead of reentering them. You may want to enter the following three commands in your .cshrc le: set history=n Creates a history buffer that stores the command lines you enter. The n entry species the number of command lines you want to store in the history buffer. set savehist=n Saves the command lines you entered during the current login session and makes them available for the next login session. The n entry species the number of command lines you want to store in the history buffer when you log out. set prompt=[\!] % Causes your C shell prompt to display the number of each command line. To see the contents of the history buffer, use the history command. The displayed output will be similar to the following (your output will vary):
[18] % history 3 set history=15 4 pwd 5 cd /usr/sales 6 ls l 7 cp report report5 8 mv /usr/accounts/new . 9 cd /usr/accounts/new 10 mkdir june 11 cd june 12 mv /usr/accounts/new/june . 13 ls l 14 cd /usr/sales/Q1 15 vi earnings 16 cd /usr/chang 17 vi status 18 history [19] % _

To reexecute any command in the command history buffer, use the commands listed in Table 8-5. Each command starts with an exclamation point (!), which tells the C shell that you are using commands in the history buffer.

88 Shell Features

Table 8-5: Reexecuting History Buffer Commands


Command !! !n Description Reexecutes the previous command. Reexecutes the command specied by n. For example, using the history buffer shown in the previous display, !5 reexecutes the cd /usr/sales command. Reexecutes a previous command relative to the current command. For example, using the history buffer shown in the previous display, !-2 invokes command number 17, vi status. Reexecutes the most recent command that has rst characters matching those specied by string. For example, using the history buffer shown in the previous display, !cp invokes command number 7, cp report report5. Reexecutes the most recent command line that has any characters matching those specied by string. For example, using the history buffer shown in the previous display, !?Q1 invokes command number 14, cd /usr/sales/Q1.

!n

!string

!?string

The command history buffer also allows you to reuse previous command arguments as well as to modify previous command lines. For information on these features, see the csh(1) reference page.

8.2.4

Filename Completion
The C shell allows you to enter a portion of a lename or pathname at the shell prompt, and the shell will automatically match and complete the name. This feature saves you time when you are trying to display long, unique lenames. For example, assume that you have the le meetings_sales_status in your current directory. To display a long listing of the le, enter the following command:
% ls l meetings Escape

The system displays the following on the same command line:


% ls l meetings_sales_status

You can now execute the command by pressing Return. For more detailed information on lename completion, see the csh(1) reference page.

Shell Features 89

8.2.5

Aliases
The command aliases feature allows you to abbreviate long command lines or rename commands. You do this by creating aliases for long command lines that you frequently use. For example, assume that you often need to move to the directory /usr/chang/reports/status. You can create an alias status, which will move you to that directory whenever you enter it on the command line. In addition, aliases allow you to make up more descriptive names for commands. For example, you could dene an alias named rename for the mv command. To create aliases, use the alias command. The format of the alias command is:
alias aliasname command

The aliasname entry species the name you want to use. The command entry species either the original command or a series of commands. If the command has more than one part (has spaces), enclose the whole expression in single quotes ( ).

810 Shell Features

For example, to create the alias status that moves you to the directory /usr/chang/reports/status, enter the following command:
% alias status cd /usr/chang/reports/status

The usual way to dene aliases is to make them a permanent part of your environment by including them in your .cshrc le. As a result, you can use the aliases whenever you log in or start a new shell. See Section 8.2.1 for an example. To display all alias denitions, enter the following command:
% alias

To display the denition of a particular alias, enter the following command:


% alias aliasname

The aliasname entry species the particular alias for which you are requesting a denition. To remove an alias for the current login session, use the unalias command. The general format of the unalias command is the following:
unalias aliasname

The aliasname entry species the alias you want to remove. To remove an alias for the current and all future login sessions, do the following: 1. Enter the following command:
% unalias aliasname

The aliasname entry species the alias you want to remove. 2. Edit the .cshrc le and remove the alias denition. Then, save the le. 3. Enter the following command to reexecute the .cshrc le:
% source .cshrc

For complete information on using aliases with the C shell, see the csh(1) reference page.

8.2.6

Built-In Variables
The C shell provides variables that can be assigned values. These variables can be very useful for storing values that can be later used in commands. In addition, you can directly affect shell behavior by setting those variables to which the shell itself refers. Table 8-6 describes selected C shell built-in variables that are of the most interest to general users. For a complete list of C shell built-in variables, see the csh(1) reference page.

Shell Features 811

Table 8-6: Built-In C Shell Variables


Variable Description Contains a value or values that can be used by the shell or shell scripts. Contains the pathname to your current directory. The value of this variable changes every time you use the cd command. Contains the pathname of your home directory. The default value for this variable is specied in the /etc/passwd le. Species whether Ctrl/D can be used to log out from the system. If set, you must use either logout or exit to log out. If unset, you may use Ctrl/D to log out. This variable is usually set in the .cshrc le. Species alternative directories to be searched by the system when locating subdirectories with the cd, chdir, or pushd commands. This variable is usually set in the .login le. Species whether a le can be overwritten. If set, places restrictions on output redirection > to ensure that les are not accidentally destroyed, and that >> redirections refer to existing les. If set, a le cannot be overwritten. This variable is usually set in the .cshrc le. Species whether you want to be notied when a background process has completed. If set, you are notied; if unset, you are not notied. This variable is usually set in the .cshrc le. Species the search path that the shell uses to nd commands. This variable is usually set in the .login le. Can be used to customize your C shell prompt. This variable is usually set in the .cshrc le. Species the shell to create when a program creates a subshell. This variable is usually set in the .login le. Species whether the most recently executed command completed without error (a value of zero is returned) or with an error (a nonzero value is returned).

argv cwd home ignoreeof

cdpath

noclobber

notify

path prompt shell status

8.2.7

Built-In Commands
Table 8-7 describes selected C shell commands that are of the most interest to general users. For a complete list of C shell built-in commands, see the csh(1) reference page.

812 Shell Features

Table 8-7: Built-In C Shell Commands


Command alias bg echo Description Assigns and displays alias denitions.a Puts a suspended process in the background.b Writes arguments to the shells standard output. For more information and the command format, see the csh(1) reference page. Puts a currently running background process in the foreground.b Displays the contents of the command history buffer.c Displays the job number and the PID number of current background processes.b Terminates the login session. Tells the shell to recompute the hash table of command locations. Use this command if you add a command to a directory in the shells search path and want the shell to be able to nd it. If you do not use rehash, the command cannot be executed because it was not in the directory when the hash table was originally created. Repeats a command a specied number of times. For more information and the command format, see the csh(1) reference page. Assigns and displays shell variable values.d Assigns environment variable values.d Executes commands in a le. This can be used to update the current shell environment.e Displays the execution time of a specied command. For more information, see the csh(1) reference page. Removes alias denitions.a Removes values that have been assigned to variables.d Removes values that have been assigned to environment variables.d

fg history jobs logout rehash

repeat

set setenv source time unalias unset unsetenv

Table notes: a. For more information about the alias and unalias commands, see Section 8.2.5. b. For more information about the bg, fg, and jobs commands, see Chapter 6.

Shell Features 813

c. For more information about the history command, see Section 8.2.3. d. For more information about the set, setenv, unset, and unsetenv commands, see Chapter 7. e. For more information about the source command, see Section 8.2.5, and the csh(1) reference page.

8.3 Bourne Shell Features


This section describes the following Bourne shell features: A sample .profile login script Metacharacters Built-in variables and commands

8.3.1

Sample .prole Login Script


If your login shell is the Bourne shell, the operating system executes the .profile login script to set up your environment. The .profile login script variables that are exported are passed to any subshells and subprocesses that are created. Variables that are not exported are used only by the login shell. In the following .profile login script, shell variables are set and exported, a trap is set for the logout script, and the system is instructed to display information. Table 8-8 explains each part of the script.
# Set PATH PATH=/usr/bin:/usr/local/bin: # Export global variables export PATH # Set shell variables PS1=$LOGNAME $ CDPATH=.:..:$HOME # Set up for logout script trap "echo logout; $HOME/.logout" 0 # Display status information date echo "Currently logged in users:" ; users

814 Shell Features

Table 8-8: Example Bourne Shell .prole Script


Command Set Search Path Description

PATH=/usr/bin:/usr/local/bin:

Species the search path. In this case, /usr/bin is searched rst and /usr/local/bin searched second.

Export Search Path

export PATH

Species that the search path is to be passed to all commands that you execute.

Set Shell Variables

PS1=$LOGNAME $

PS1 is the variable that species the Bourne shell prompt, and its default value is $. However, this variable assignment species that your prompt should be changed to the following: username $. For example, if your username were amy, your prompt would be the following: amy $. CDPATH is a variable that sets the search path for the cd command. This variable assignment species that the cd command should search for the named directory in the current directory (.) rst, in the parent directory (..) second, and the home directory ($HOME) third.

CDPATH=.:..:$HOME

Set Up Logout Script

trap "echo logout; $HOME/.logout" 0

Species that your shell should display logout and execute your .logout script when the trap command captures the exit signal (0).a

Shell Features 815

Table 8-8:
Command

(continued)
Description

Display Status Information

date

Displays the date and time.

Table notes: a. For more information about the trap command, see Section 7.9.1.

8.3.2

Metacharacters
Table 8-9 describes Bourne shell metacharacters (characters that have special meaning to the shell). Table 8-9: Bourne Shell Metacharacters
Metacharacter Syntactic | && || ; ;; & ( ) Filename / ? Separates the parts of a les pathname. Matches any single character except a leading dot (.). Separates commands that are part of a pipeline. Runs the next command if current command succeeds. Runs the next command if the current command fails. Separates commands that should be executed sequentially. Separates elements of a case construct. Runs commands in the background. Groups commands to run as a separate process in a subshell. Description

816 Shell Features

Table 8-9:

(continued)
Description Matches any sequence of characters except a leading dot (.). Matches any of the enclosed characters.

Metacharacter * [ ] Quotation \ ...

Species that the following character should be interpreted literally; that is, without its special meaning to the shell. Species that any of the enclosed characters (except for the quote character ) should be interpreted literally; that is, without their special meaning to the shell. Provides a special form of quoting. Species that the $ (dollar sign), `(grave accent), and \ (backslash) characters keep their special meaning, while all other enclosed characters are interpreted literally; that is, without their special meaning to the shell. Double quotes are useful in making variable assignments.

"..."

Input/Output < > << >> 2> Substitution ${...} ... Species variable substitution. Species command output substitution. Redirects input. Redirects output to a specied le. Redirects input and species that the shell should read input up to a specied line. Redirects output and species that the shell should add output to the end of a le. Redirects diagnostic output to a specied le.

8.3.3

Built-In Variables
The Bourne shell provides variables that can be assigned values. The shell sets some of these variables, and you can set or reset all of them. Table 8-10 describes selected Bourne shell built-in variables that are of most interest to general users. For complete information on all Bourne Shell builtin variables, see the sh(1) reference page.

Shell Features 817

Table 8-10: Built-In Bourne Shell Variables


Variable Description Species the name of your login directory, the directory that becomes the current directory upon completion of a login. The cd command uses the value of HOME as its default value. HOME is set by the login command. Species the directories through which your system should search to nd and execute commands. The shell searches these directories in the order specied here. Usually, PATH is set in the .profile le. Species the directories that the cd command will search to nd the specied argument to cd. If cds argument is null, or if it begins with a slash ( / ), dot (.), or dot dot (..), then CDPATH is ignored. Usually, CDPATH is set in your .profile le. The pathname of the le where your mail is deposited. You must set MAIL, and this is usually done in your .profile le. Species in seconds how often the shell checks for mail (600 seconds is the default). If the value of this variable is set to 0, the shell checks for mail before displaying each prompt. MAILCHECK is usually set in your .profile le. Species your default shell. This variable should be set and exported by your .profile le. Species the default Bourne shell prompt, and its default value is $. PS1 is usually set in your .profile le. If PS1 is not set, the shell uses the standard primary prompt string. Species the secondary prompt string the string that the shell displays when it requires more input after you enter a command line. The standard secondary prompt string is a > symbol followed by a space. PS2 is usually set in your .profile le. If PS2 is not set, the shell uses the standard secondary prompt string.

HOME

PATH

CDPATH

MAIL

MAILCHECK

SHELL PS1

PS2

8.3.4

Built-In Commands
Table 8-11 describes selected Bourne shell commands that are of the most interest to general users. For a complete list of Bourne shell built-in commands, see the sh(1) reference page.

818 Shell Features

Table 8-11: Built-In Bourne Shell Commands


Command cd Description Allows you to change directories. If no directory is specied, the value of the HOME shell variable is used. The CDPATH shell variable denes the search path for this command.a Writes arguments to the standard output.b Marks the specied variable for automatic export to the environments of subsequently executed commands. Displays the current directory.c Assigns and displays variable values.d Displays the accumulated user and system times for processes run from the shell. Runs a specied command when the shell receives a specied signal.d Species the permissions to be subtracted for all new les created.e Removes values that have been assigned to variables.d

echo export pwd set times trap umask unset

Table notes: a. For more information about the cd command, see Chapter 4, and the sh(1) reference page. b. For more information about the echo command, see Section 8.3.1 and the sh(1) reference page. c. For more information about the pwd command, see Chapter 2. d. For more information about the set, trap, and unset commands, see Chapter 7. e. For more information about the umask command, see Chapter 5 and Section 8.2.1.

8.4 Korn Shell Features


This section describes the following Korn shell features: Sample .profile and .kshrc login scripts Metacharacters Command history

Shell Features 819

Editing command lines Filename completion Aliases Built-in variables and commands

8.4.1

Sample .prole and .kshrc Login Scripts


If your login shell is the Korn shell, the operating system processes the .profile login script in your home directory. The .profile login script denes environment variables. These variables are used by your login shell as well as any subshells and subprocess that are created. The .profile login script is executed only when you log in. The .kshrc login script sets up your Korn shell environment by dening variables and operating parameters for the local shell process. It is executed each time you create a subshell. Note Before creating a .kshrc le in your home directory, make sure that the ENV=$HOME/.kshrc environment variable is set and exported in your .profile. Once this is done, the .kshrc login script will execute each time you log in and each time you create a subshell. In the following .profile login script, global environment variables are set and exported, and shell variables are set. Table 8-12 explains each part of the script.
# Set environment variables PATH=/usr/bin:/usr/local/bin: ENV=$HOME/.kshrc EDITOR=vi FCEDIT=vi PS1="hostname [!] $ " # Export global variables export PATH ENV EDITOR FCEDIT PS1 # Set mail variables MAIL=/usr/spool/mail/$LOGNAME MAILCHECK=300

820 Shell Features

Table 8-12: Example Korn Shell .prole Script


Command Set Environment Variables PATH=/usr/bin:/usr/local/bin Species the search path. In this case, /usr/bin is searched rst and /usr/local/bin searched second. Species $HOME/.kshrc as the login script. Species vi as the default editor for command line editing at the shell prompt and for lename completion. Species vi as the default editor for the fc command.a Description

ENV=$HOME/.kshrc EDITOR=vi

FCEDIT=vi

PS1="hostname [!] $ "

PS1 is the variable that species the Korn shell prompt, and its default value is $. However, this variable assignment species that your prompt should be changed to the following: the output of the hostname command, followed by the command number of the current command, followed by the dollar sign ($). For example, if the name of your system is boston, and the current command is numbered 30, your prompt would be the following: boston[30] $.

Export Global Variables export PATH ENV EDITOR FCEDIT PS1 Species that the values of the PATH, ENV, EDITOR, FCEDIT, and PS1 variables should be exported to all subshells.

Shell Features 821

Table 8-12:
Command

(continued)
Description

Set Mail Variables MAIL=/usr/spool/mail/$LOGNAME Species the pathname of the le used by the mail system to detect the arrival of new mail. In this case, the mail system would look in your username subdirectory under the /usr/spool/mail directory. Species that the shell should check for mail every 300 seconds (5 minutes).

MAILCHECK=300

Table notes: a. For information on the fc command, see Section 8.4.4. In the following .kshrc login script, shell variables, command aliases, and command history variables are set, as well as the permissions for le creation. Table 8-13 explains each part of the script.
# Set shell variables set o monitor set o trackall # Set alias alias alias alias alias command aliases rm=rm i rename=mv h history \!* | more l ls l c clear

# Set history variables HISTSIZE=40 # Set file creation permissions umask 027

822 Shell Features

Table 8-13: Example .kshrc Script


Command Shell Variables set o monitor Species that the shell should monitor all background processes and display a completion message when the process nishes. Species that the shell should track all commands that you execute. Once a command is tracked, the shell stores the location of the command and nds the command more quickly the next time you enter it. Description

set o trackall

Command Aliases alias rm=rm i Species the use of the i option (which prompts you for le deletion) with the rm command. Species rename as a new name for the mv command. Denes a command that pipes the contents of the command history buffer through the more command. The \!* string species that all of the history buffer should be piped. Denes a short name for the ls l command that lists directory les in the long format. Denes a short name for the clear command that clears your screen.

alias rename=mv alias h=history \!* | more

alias l=ls l

alias c=clear History Variables HISTSIZE=40

Instructs the shell to store the last 40 commands in the history buffer.

Shell Features 823

Table 8-13:
Command

(continued)
Description

Set File Creation Permissions umask 027 Species the maximum permissions for all new les created. This command provides all permissions for the owner, read, and execute permissions for members of the same group, and no permissions for all others. The umask is not inherited by subshells.

8.4.2

Metacharacters
Table 8-14 describes Korn shell metacharacters (characters that have special meaning to the shell). Table 8-14: Korn Shell Metacharacters
Metacharacter Syntactic | && || ; ;; & ( ) { } Filename / ? * [ ] ~ Separates the parts of a les pathname. Matches any single character except a leading dot (.). Matches any character sequence except a leading dot (.). Matches any of the enclosed characters. Species a home directory when it begins a lename. Separates commands that are part of a pipeline. Runs the next command if the current command succeeds. Runs the next command if the current command fails. Separates commands that should be executed sequentially. Separates elements of a case construct. Runs commands in the background. Groups commands in a subshell as a separate process. Groups commands without creating a subshell. Description

824 Shell Features

Table 8-14:
Metacharacter Quotation \ ...

(continued)
Description

Species that the following character should be interpreted literally; that is, without its special meaning to the shell. Species that any of the enclosed characters (except for the ) should be interpreted literally; that is, without their special meaning to the shell. Provides a special form of quoting. Species that the dollar sign ($), (grave accent), \ (backslash), and ) (close parenthesis) characters keep their special meaning, while all other enclosed characters are interpreted literally; that is, without their special meaning to the shell. Double quotes (" ") are useful in making variable assignments.

"..."

Input/Output < > << >> >& Substitution ${...} % ... Species variable substitution. Species job number substitution. Species command output substitution. Redirects input. Redirects output to a specied le. Redirects input and species that the shell should read input up to a specied line. Redirects output and species that the shell should add output to the end of a le. Redirects both diagnostic and standard output and appends them to a le.

8.4.3

Command History
The command history buffer stores the commands you enter and allows you to display them at any time. As a result, you can select a previous command, or parts of previous commands, and then reexecute them. This feature may save you time because it allows you to reuse long commands instead of reentering them.

Shell Features 825

To see the contents of the history buffer, use the history command. The displayed output will be similar to the following (your output will vary):
[18] $ history 3 ls l 4 pwd 5 cd /usr/sales 6 ls l 7 cp report report5 8 mv /usr/accounts/new . 9 cd /usr/accounts/new 10 mkdir june 11 cd june 12 mv /usr/accounts/new/june . 13 ls l 14 cd /usr/sales/Q1 15 vi earnings 16 cd /usr/chang 17 vi status [19] $

To reexecute any command in the command history buffer, use the commands listed in Table 8-15. Each command starts with the letter r. Table 8-15: Reexecuting History Buffer Commands
Command r r n Description Reexecutes the previous command Reexecutes the command specied by n. For example, using the history buffer shown in the previous display, r 5 reexecutes the cd /usr/sales command. Reexecutes a previous command relative to the current command. For example, using the history buffer shown in the previous display, r-2 invokes command number 16, cd /usr/chang. Reexecutes the most recent command that has rst characters matching those specied by string. For example, using the history buffer shown in the previous display, r cp invokes command number 7, cp report report5.

r n

r string

For more information on reexecuting history buffer commands, see the ksh(1) reference page. If you want to increase or decrease the number of commands stored in your history buffer, set the HISTSIZE variable in your .profile le. This variable has the following format:

826 Shell Features

HISTSIZE=n

The n entry species the number of command lines you want to store in the history buffer. For example, to store 15 commands in the history buffer, use the following command:
HISTSIZE=15

The Korn shell also allows you to edit current command lines as well as reuse those already entered in the command history buffer. To use this feature, you must know how to use a text editor such as vi or emacs. For information on these features, see the following section.

8.4.4

Command Line Editing Using the fc Command


The Korn shell allows you to list and/or edit the command lines in your command history buffer. As a result, you may modify any element of a previous command line and then reexecute the command line. The command line editing functions for the Korn shell are extensive. This section covers only the most basic functions. For more detailed information, see the ksh(1) reference page. To display the command history buffer and/or to edit its contents, use the built-in command fc (x command). The fc command has the following two formats: 1.
fc [-e editor ] [-nlr] [ rst ] [ last ]

This command format allows you to display and/or edit any number of command lines in your buffer. The e editor entry species the editor (usually vi or emacs) you want to use in editing the command line. If you do not specify e, the fc command displays the lines, but does not allow you to edit them. The n ag species that you want to list the command lines in the buffer without numbers. The l ag species that you want to list the command lines in the buffer with numbers. If you do not specify a line number or a range of line numbers, the last 16 lines you entered will be listed. The r ag species that you want to list the command in the buffer in reverse order. The first and last entries specify a range of command lines in the buffer. You may specify them either with numbers or with strings.

Shell Features 827

If you want to specify a default editor for the e ag, dene the FCEDIT variable in your .profile script. For example, if you want to make emacs your default editor, enter the following variable denition:
FCEDIT=emacs

2.
fc -e - [ old=new ] [ string ]

This command allows you to immediately replace an old string with a new string within any previous command line. The e entry species that you want to make a replacement. The old=new entry species that you want to replace the old string with the new string. The string entry species that the Korn shell should make the edit to the most recent command line in the buffer containing the string.

The following section contains some examples of fc use. The Korn shell also allows you to edit individual command lines at the shell prompt by using a command set similar to the vi or the emacs editors. For more information on this feature, see the ksh(1) reference page. 8.4.4.1 Examples of Command Line Editing Example 1: Displaying Command Lines in the Command History Buffer To display command lines 15 to 18, enter the following command:
$ fc l 15 18 15 ls la 16 pwd 17 cd /u/ben/reports 18 more sales $

You may also list the same command lines by specifying command strings instead of line numbers, as in the following example:
$ fc l ls more 15 ls la 16 pwd 17 cd /u/ben/reports 18 more sales $

828 Shell Features

Example 2: Editing and Executing Command Lines To display and edit command lines 15 to 18 with the vi editor, enter the following command:
$ fc e vi 15 18 ls la pwd cd /u/ben/reports more sales ~ ~ ~ ~

After making your edits, write and exit the le with the :wq! command. The command lines in the le are then reexecuted. Example 3: Replacing and Reexecuting Command Lines Assume that you have just entered the echo hello command, and now want to replace hello with goodbye. To do the replacement and reexecute the command line, enter the following command:
$ echo hello hello $ fc e hello=goodbye echo echo goodbye goodbye

For more detailed information on the fc command and command line editing, see the ksh(1) reference page.

8.4.5

Filename Completion
The Korn shell allows you to enter a portion of a lename or pathname at the shell prompt and the shell will automatically match and complete the name. If there is more than one lename or pathname that matches the criterion, the shell will provide you with a list of possible matches. To activate the lename completion mechanism, dene the EDITOR variable in your .profile le. For example, if you want to use the vi editor, enter the following variable denition in your .profile le:
EDITOR=vi

To demonstrate how lename completion works, assume that your editor is vi and that you have the salesreport1, salesreport2, and salesreport3 les in your current directory. To display a long listing

Shell Features 829

and to activate lename completion, enter the following command:


$ ls l salesreport Escape = 1) salesreportfeb 2) salesreportjan 3) salesreportmar $ ls l salesreport

The system redisplays your command, and the cursor is now at the end of salesreport. If you want to choose salesreportjan, type a (the vi append command) followed by jan, then press Return. The listing for salesreportjan will be displayed. For more detailed information on lename completion, see the ksh(1) reference page.

8.4.6

Aliases
The command aliases feature allows you to abbreviate long command lines or rename commands. You do this by creating aliases for long command lines that you frequently use. For example, assume that you often need to move to the directory /usr/chang/reports/status. You can create an alias status, which will move you to that directory whenever you enter it on the command line. In addition, aliases allow you to make up more descriptive names for commands. For example, you could dene an alias named rename for the mv command. To create aliases, use the alias command. The general format of the alias command is the following:
alias aliasname=command

The aliasname entry species the name you want to use. The command entry species either the original command or a series of commands. If the command has more than one part (has spaces), enclose the whole expression in single quotes. For example, to create the alias status that moves you to the directory /usr/chang/reports/status, enter the following command:
alias status=cd /usr/chang/reports/status

The usual way to dene aliases is to place them in your .kshrc le so that you can use them whenever you log in or start a new shell. See Section 8.4.1 for an example.

830 Shell Features

To display all alias denitions, enter the following command:


$ alias

To display the denition of a particular alias, enter the following command:


$ alias aliasname

The aliasname entry species the particular alias for which you are requesting a denition. The Korn shell allows you to export the aliases you create. Variables that are exported are passed to any subshells that are created so that when you execute a shell procedure or new shell, the alias remains dened. (Variables that are not exported are used only by the login shell.) To export an alias, use the following form of the alias command:
alias x aliasname=command

The x ag species that you want to export the alias. The aliasname entry species the name you want to use. The command entry species either the original command or a series of commands. If the command has more than one part, enclose the whole expression in single quotes. For example, to export an alias denition for the rm command, enter the following:
alias x rm=rm i

You can enter the preceding command in one of two ways: Edit the .kshrc or .profile le if you want an alias exported whenever you log in. Export an alias on the command line if you want the alias exported only for the current login session.

To remove an alias for the current login session, use the unalias command. The general format of the unalias command is the following:
unalias aliasname

The aliasname entry species the alias you want to remove. To remove an alias for the current and all future login sessions, do the following: 1. Enter the following command:
$ unalias aliasname

The aliasname entry species the alias you want to remove. 2. Edit the .kshrc le (or the le on your system that contains alias denitions) and remove the alias denition. Then, save the le.

Shell Features 831

3. Enter the following command to reexecute the .kshrc le:


$ source .kshrc

The Korn shell provides additional aliasing features. For complete information on using aliases with the Korn shell, see the ksh(1) reference page.

8.4.7

Built-In Variables
The Korn shell provides variables that can be assigned values. The shell sets some of these variables, and you can set or reset all of them. Table 8-16 describes selected Korn shell built-in variables that are of the most interest to general users. For complete information on all Korn shell built-in variables, see the ksh(1) reference page. Table 8-16: Built-In Korn Shell Variables
Variable Description Species the name of your login directory. The cd command uses the value of HOME as its default value. In Korn shell procedures, use HOME to avoid having to use full pathnames something that is especially helpful if the pathname of your login directory changes. HOME is set by the login command. Species the directories through which your system should search to nd and execute commands. The shell searches these directories in the order specied here. Usually, PATH is set in the .profile le. Species the directories that the cd command will search to nd the specied argument to cd. If the cd argument is null, or if it begins with a slash ( / ), dot (.), or dot dot (..), then CDPATH is ignored. Usually, CDPATH is set in your .profile le. The pathname of the le where your mail is deposited. MAIL is usually set in your .profile le. Species in seconds how often the shell checks for mail (600 seconds is the default). If the value of this variable is set to 0, the shell checks for mail before displaying each prompt. MAILCHECK is usually set in your .profile le. Species your default shell. This variable should be set and exported by your .profile le.

HOME

PATH

CDPATH

MAIL MAILCHECK

SHELL

832 Shell Features

Table 8-16:
Variable

(continued)
Description Species the default Korn shell prompt, and its default value is the dollar sign ($). PS1 is usually set in your .profile le. If PS1 is not set, the shell uses the standard primary prompt string. Species the secondary prompt string the string that the shell displays when it requires more input after entering a command line. The standard secondary prompt string is a > symbol followed by a space. PS2 is usually set in your .profile le. If PS2 is not set, the shell uses the standard secondary prompt string. Species the pathname of the le that will be used to store the command history. This variable is usually set in your .profile le. Species the default editor for command line editing at the shell prompt and for lename completion. This variable is usually set in your .profile le. Species the default editor for the fc command. This variable is usually set in your .profile le. Species the number of previously entered commands that are accessible by this shell. The default is 128. This variable is usually set in your .kshrc le.

PS1

PS2

HISTFILE

EDITOR

FCEDIT HISTSIZE

8.4.8

Built-In Commands
Table 8-17 describes selected Korn shell commands that are of the most interest to general users. For a complete list of Korn shell built-in commands, see the ksh(1) reference page. Table 8-17: Built-In Korn Shell Commands
Command alias cd Description Assigns and displays alias denitions.a Allows you to change directories. If no directory is specied, the value of the HOME shell variable is used. The CDPATH shell variable denes the search path for this command.b Writes arguments to the standard output. For more information and the command format, see the ksh(1) reference page.

echo

Shell Features 833

Table 8-17:
Command export fc history jobs pwd set times trap umask

(continued)
Description Marks the specied variable for automatic export to the environments of subsequently executed commands.c Allows you to display, edit, and reexecute the contents of the command history buffer.d Displays the contents of the command history buffer.a Displays the job number and the PID number of current background processes. Displays the current directory.e Assigns and displays variable values.f Displays the accumulated user and system times for processes run from the shell. Runs a specied command when the shell receives a specied signal.f Species the permissions to be subtracted from the default permissions set by the creating program for all new les created.g Removes alias denitions.a Removes values that have been assigned to variables.f

unalias unset

Table notes: a. For more information about the alias, history, and unalias commands, see Section 8.4.6. b. For more information about the cd command, see Chapter 4 and the ksh(1) reference page. c. For more information about the export command, see Section 8.4.1 and the ksh(1) reference page. d. For more information about the fc command, see Section 8.4.4. e. For more information about the pwd command, see Chapter 2. f. For more information about the set, trap, and unset commands, see Chapter 7. g. For more information about the umask command, see Chapter 5 and Section 8.4.1.

834 Shell Features

Using the System V Habitat

The System V habitat consists of alternate versions of commands, subroutines, and system calls that support the source code interfaces and runtime behavior for all components of the Base System and Kernel Extension as dened in the System V Interface Denition (SVID). This implementation of the System V habitat supports all SVID 2 functions and SVID 3 functions. Note that the System V habitat does not contain alternate versions of default system commands, subroutines, and system calls that already meet the SVID requirement. Using the System V habitat lets you override the default system commands and functions with corresponding System V commands and functions (system calls and subroutines). You can access the habitat in two ways: Specify the absolute paths of the System V commands and libraries. Dene the PATH environment variable to search the System V habitat for commands before it searches the default system locations. To set your PATH environment variable, modify your .profile or .login and .cshrc les as described in Section 9.1.

Because the System V system calls are not layered over the system calls in the default system, applications that are built using the system calls in the System V habitat run with virtually no performance overhead. Figure 9-1 illustrates the System V habitat placement within the default operating system and shows that the System V system calls reside at the kernel level.

Figure 9-1: System V Habitat


other applications and commands

standard C library

Kernel Level User Level


default system calls SVID system calls SVID library SVID commands

other habitats

ZK0849UR

The following sections describe how to set up your environment to access the System V habitat and how it works.

9.1 Setting Up Your Environment


To automatically access the System V habitat when you log on, you must add a command line to your .profile le if you use the Bourne or Korn shell, or to your .login and .cshrc les if you use the C shell. The command line modies the PATH environment variable, which causes the System V habitat to be searched before the standard default locations on the system are searched, such as /bin or /usr/bin. The System V habitat scripts are as follows:

92 Using the System V Habitat

/etc/svid2_profile (for the Bourne or Korn shells) /etc/svid2_login (for the C shell) Species SVID 2 behavior when placed in either your .profile or .login and .cshrc les, respectively.

/etc/svid3_login (for the Bourne or Korn shell) /etc/svid3_profile (for the C shell) Species SVID 3 behavior when placed in either your .profile or .login and .cshrc les, respectively.

For example, if you use the Bourne or Korn shell and you want to specify SVID 2 behavior, edit the .profile le and add the following command line:
if [ -f /etc/svid2_profile ] then . /etc/svid2_profile fi

If you use the C shell and you want to specify SVID 2 behavior, edit the .login and .cshrc les and add the following command line:
if ( -e /etc/svid2_login ) then source /etc/svid2_login endif

The dot (.) and source commands are shell-specic. See the appropriate reference page for more information.

9.2 How the System V Habitat Access Works


Whether you choose the script that species SVID 2 behavior or the script that species SVID 3 behavior, both establish the System V habitat as follows: Denes SVID2PATH, a System V only variable, to be the contents of /etc/svid2path or /etc/svid3path. The /etc/svid2path and /etc/svid3path les contain the path denition for SVID 2 and SVID 3, respectively. Adds SVID2PATH/bin and SVID2PATH/sbin to the beginning of your current PATH. Exports both SVID2PATH and PATH. Sets the TZ variable to the appropriate value. If you select SVID 2 behavior, it also sets the TZC variable to the appropriate value. See the System Administration guide for more information on time zone formats.

Hence, if you need to determine the location of the System V habitat on your system, run the cat(1) command on the /etc/svid2path or
Using the System V Habitat 93

/etc/svid3path le. By using the System V habitat scripts to alter the PATH environment variable, the System V habitat path can be changed without an administrator updating each users .profile or .login and .cshrc les. The administrator simply updates the /etc/svid2path and /etc/svid3path les to enable global denitions. To further illustrate how the System V habitat script sets a PATH, look at the following .profile le which species the System V habitat script for SVID 2:
stty erase DEL kill ^U intr ^C quit ^X echo TERM=vt100 PATH=:$HOME/bin:/usr/lib:/bin MAIL=/usr/mail/$LOGNAME EDITOR=vi export MAIL PATH TERM EDITOR if [ -f /etc/svid2_profile ] then . /etc/svid2_profile fi

In this example, assume that the path of the System V habitat is /usr/opt/s5 as reected by the contents of /etc/svid2path and that your login directory is /usr/users/xxx. When you display the PATH after logging in with the preceding .profile le, the result would show that the path to the System V habitat has been prepended to the PATH set in the third line of the .profile le as follows:
% echo $PATH Return /usr/opt/s5/bin:/usr/opt/s5/sbin:/usr/users/xxx/bin:/usr/lib:/bin

Hence, when you issue a shell command, the System V habitat is searched rst. If the command is not found, the specied paths are searched.

9.3 Compatibility for Shell Scripts


Compatibility for your shell scripts is achieved by altering your shells PATH environment variable (as explained in Section 9.1). Therefore, the System V habitat is searched before the default system locations. If your PATH variable is set for the System V habitat, your scripts are System V compatible regardless of whether you use the Bourne, Korn, or C shell.

9.4 System V Habitat Command Summary


Table 9-1 summarizes the behavior of user commands in the System V habitat that have options or features that differ from the default system versions. For a complete explanation of the commands in the habitat, refer to the reference page for each command.

94 Using the System V Habitat

Table 9-1: User Commands Summary


Command chmod(1) System V Behavior Ignores the umask value when the who string is omitted, behaving as though a is the who value when you use the symbolic form of this command. Accepts the -t option, which prints space totals, and accepts an optional le system name or device name. Accepts the -f option, which silently removes existing destination pathnames before creating the specied link. Produces multicolumn output only if the -C option is specied. Also, the -s option causes le sizes to be reported in 512 byte units instead of 1024 byte units.

df(1) ln(1) ls(1)

mailx(1) and Mail(1) Includes the capabilities of the System V mailx command. sum(1) Uses the word-by-word algorithm by default; uses the byte-by-byte algorithm if the -r option is specied. The default use of the checksum algorithms for the System V sum command is the reverse of the default system version of the sum command. Includes the -A option whenever you specify the -c option. The -A option causes only the characters in the octal range of 1 to 377 to be complemented.

tr(1)

Using the System V Habitat 95

Obtaining Information About Network Users and Hosts

10

This chapter describes how to nd information about local and remote users and hosts before you begin communication or le transfer tasks. The commands described in this chapter will enable you to: Learn about your own network connection ( who am i command, Section 10.1) Determine who is currently logged in to the local system and from where they are logged in ( who command, Section 10.1) Find additional information about another user, if available; for example, full name, ofce location, phone number, projects, or plans ( finger command, Section 10.2.1) Determine whether a user can be reached using either the talk or write commands ( finger command, Section 10.2.1 and Section 10.2.2) Analyze and sort information about remote host usage ( ruptime command, Section 10.3) Determine who is currently logged in to a remote host ( rwho command, Section 10.4) Determine whether a remote host is online ( ping command, Section 10.5) Note The commands described in this chapter are, like all TCP/IP operations, subject to the security features on the local and remote hosts. If they do not work as stated here or in the related reference pages, see your system administrator.

10.1 Identifying Users on the Local Host


When you log in to a host computer by providing a user name and password, you have a unique identity. To verify this information for your own network connection, you can use a version of the who command called who am i to display the following information about you:

Login name Terminal name (line) Time of login Computer from which the network connection came

For example, user lennon might enter the who am i command at the system prompt (%) and read the following output:
% who am i lennon ttyp0 Jul 15 14:17 (walrus)

In this example, user lennon logged in from host walrus at 2:17 in the afternoon of July 15. The line is ttyp0, and walrus is the name for this line, from which the network connection came. The who am i command can help you keep track of the sessions you have running on your workstation. Some sessions may be remote logins to another host by yourself or by someone with whom you are working. See the who(1) reference page for more information about the who am i command. To nd out if other users are logged in to the same local host, use the who command. In the following example, lennon enters the who command at the prompt of local host london, and learns that three other users are currently logged in to london from different nodes:
london% who lennon ttyp0 elvis ttyp2 burdon ttyp1 sarjan ttyp4 Jul Jul Jul Jul 15 15 15 14 08:17 07:55 09:02 16:47 (walrus) (velvet) (animal) (pepper)

The output from the who command is the same as that from the who am i command.

10.2 Obtaining Information About Network Users


The finger command and its options enable you to display information about users with accounts on local or remote hosts. The specied host must be running a fingerd daemon server or have the inetd daemon congured to start fingerd. See your system administrator if the finger command does not work as described in the finger(1) reference page. The finger command has the following syntax:
nger [[ option... ] [ user... ] [ user@host_name... ] ]

If you use the finger command without specifying an option or a user name, it lists the following information about all users on the local host
102 Obtaining Information About Network Users and Hosts

where you are logged in, if the information is in the /etc/passwd le for a given user: Login name Full name Terminal line name and whether it can receive messages from other users through write (see Section 11.8) or talk (see Section 11.9) Idle time Login time Users ofce location

10.2.1

Obtaining Information About a Specic User


If you specify the login name of a user on your local host, the finger command displays more information than if you entered the finger command without specifying a user name. The following additional information about the user is displayed: Users home directory and login shell Contents (if any) of the .plan and .project les in the users home directory

The following example shows how to use the finger command to nd information about user smith, who has an account on your local host:
% finger smith Login name: smith (messages off) In real life: John Smith Office: LV05-3/T24 Directory: /usr/netd/r2/smith Shell: /bin/csh On since Apr 9 16:20:56 on ttypb from wombat.lv5.dec.c 18 seconds Idle Time Project: book, "Communicating with Network Users" Plan: Get information in the following areas: network commands mailx

In the rst line of output, messages off means that user smith has put the mesg n command in his .login le to prevent his terminal from receiving messages from other users through the write or talk commands, which can be distracting. The preceeding example also displays the contents of the .project le and the .plan le that user smith created in his home directory. The .project le can contain only one line. The .plan le can contain as many lines as the le system allows; finger will print all the lines until the end-of-le (EOF) is reached.

Obtaining Information About Network Users and Hosts 103

10.2.2

Obtaining Information About Users on a Remote Host


In the following example, the finger command displays information about users on the remote host boston:
% finger @boston [boston] Login Name amy Amy Wilson chang Peter Chang TTY Idle When p0 4 Thu 10:00 *p1 2:58 Thu 10:16 Office 345 103

The rst output line lists the remote host name, boston, and the second line describes the type of information in each column of the remaining output, each line allocated to one user. The asterisk (*) indicates that user chang has put the command, mesg n in his .login le to prevent his terminal from receiving messages from other users through the write or talk commands.

10.2.3

Obtaining Information About an Individual User on a Remote Host


To display information about user luis on remote host havana use the following finger command:
% finger luis@havana Login name: luis Directory: /users/luis On since May 24 10:16:07 on ttyp2 58 minutes Idle Time Project: baseball game simulation Plan: Distribute with linked statistics In real life: Luis Aguilera Shell: /bin/csh from :0.0 software module.

10.2.4

Customizing Output from the nger Command


There are several options to the finger command that enable you to modify the output according to the data you need. Table 10-1 lists and describes each option. Table 10-1: Options to the nger Command
Option b f h Description Produces a brief version of output Suppresses display of titles of each eld Suppresses printing of users .project les

104 Obtaining Information About Network Users and Hosts

Table 10-1:
Option i l m p q s w

(continued)

Description Displays list of users with idle times Produces long format of output despite other options Assumes that user is an account name Suppresses printing of users .plan les Displays only users login and terminal names and login time Produces brief format of output despite other options Produces narrow, brief format of output despite other options

For more information on the finger command, see the finger(1) reference page.

10.3 Obtaining Information about Remote Hosts and Users


Before you send messages or transfer les over the network using the commands described in this book, you should know whether or not the recipient host is currently online. To do this, use the ruptime command which works for hosts that are running the rwhod daemon on the local network. The ruptime command displays the following information: Host name Online status ( up for online or down for ofine) The length of time the host has been on line (or off line) The number of users currently logged in to the host (optionally including those whose sessions have been idle for an hour or longer) Load average statistics in 5-, 10-, or 15-minute intervals prior to the ruptime request

The syntax of the ruptime command is:


ruptime [[ option... ] [ sort_option ] ]

Obtaining Information About Network Users and Hosts 105

If you use the ruptime command without options, a status report about the hosts on your local network, sorted alphabetically by host name, is displayed. For example:
% ruptime apple up byblos up carpal up dull down eager down foobar up garlic up hiccup up jackal up starry up travel up trekky down tribbl up trubbl up tunnel down warp9 up 102+05:07 4 users, 3+03:17, 3 users, 2:28, 0 users, 9+21:59 23+22:45 3+01:44, 9 users, 14+01:35, 1 user, 4+22:14, 19 users, 13+10:32, 26 users, 16+21:08, 1 user, 13+23:44, 7 users, 23+03:53 8+21:43, 0 users, 14+02:34, 0 users, 14+02:34 8+01:24, 9 users, load 0.09, 0.04, 0.04 load 0.08, 0.07, 0.04 load 7.01, 5.02, 3.03

load load load load load load

0.01, 0.06, 6.37, 0.70, 0.22, 1.01,

0.02, 0.12, 3.90, 0.92, 0.14, 1.19,

0.03 0.11 2.71 0.95 0.07 0.5

load 0.00, 0.00, 0.00 load 0.00, 0.00, 0.00 load 0.01, 0.02, 0.03

Often, you need to determine only whether a single host is currently online. To do this, enter the ruptime command with the host name, as shown in the following example, for host trekky:
% ruptime trekky trekky down 23+03:53

This output shows that host trekky is not currently online. You can also determine whether a host is online by using the ping command described in Section 10.5; ping works for any host in a TCP/IP network conguration. If you plan to run commands on a remote host (as described in Chapter 13), use the ruptime command with the -l option to determine whether the host resources will be adequate. This command sorts the hosts by load average in descending order. The following example shows partial output from the ruptime -l command:
% ruptime -l carpal up 2:28, hiccup up 4+22:14, travel up 13+23:44, jackal up 13+10:32, 0 19 7 26 users, users, users, users, load load load load 7.01, 6.37, 1.01, 0.70, 5.02, 3.90, 1.19, 0.92, 3.03 2.71 0.5 0.95

. . .

In this example, usage is low on all hosts except carpal and hiccup. Therefore, you may decide to remotely log in to either travel or jackal, if either host is suitable for your purpose.

106 Obtaining Information About Network Users and Hosts

If you need to use a remote host for a long period of time, you should know the total number of users there, not just the number whose sessions have been active for an hour or longer. Use the ruptime command with the -a option to display the total number of users on a remote host. The following two examples use the ruptime -a command to determine the total number of users rst on host travel, and then on host jackal:
% ruptime -a travel travel up 13+23:44, % ruptime -a jackal jackal up 13+10:32, 32 users, load 1.01, 1.19, 0.5

29 users,

load 0.70, 0.92, 0.95

From the results of the ruptime command using the -a and -l options (in the preceding example), you can determine that both hosts have nearly the same number of users, but the current usage on host travel is calculated from only the 7 (from a total of 32) users whose sessions have been active for an hour or longer. By contrast, usage on host jackal is less, but is calculated from 26 of the total of 29 users. You could conclude that, over a period of time, usage on host travel may increase as more users log in, but that usage on host jackal may either decrease or stay nearly the same, because most of its users are already logged in. The remaining options (except for -r) sort by different output elds, and in descending alphabetical order. To reverse this order, put the -r option after the other option on the command line. You should not combine other ruptime command options; if you do, only the last option on the command line will be used. Table 10-2 describes each option. Table 10-2: Options to the ruptime Command
Option a l r t u Description Provides information for all users, including those whose sessions have been idle for an hour or longer Sorts output by load average over 5-, 10-, and 15-minute intervals Reverses the sort order Sorts output by length of time host is online Sorts output by number of users

For more information, see the ruptime(1) reference page.

Obtaining Information About Network Users and Hosts 107

10.4 Obtaining Information About Users on Remote Hosts


Before using a command that sends a message or transfers a le, you often need to know if the recipient user is logged in. To determine whether a user is logged in to a remote host on the local network, you can use the rwho command, specifying the name of one or more users. The rwho command operates only for hosts running the rwhod daemon. See your system administrator if necessary. The rwho command displays the following information: User name Host name Start date and time Number of minutes a users session has been inactive.

The rwho command has the following syntax:


rwho [[-a] [ user... ] ]

Without options, the rwho command lists all users currently logged in to hosts on the local network, except those who have been idle for an hour or longer. A typical local network has several dozen users, so you should specify only the users about whom you need information. Although the -a option displays all users, including those idle for more than an hour, you can still use it while specifying only certain users. This enables you to determine whether or not a remote user is logged in, regardless of whether that user has been inactive for an hour or longer. The following example uses rwho with the -a option to determine this information for users wally, becky, and smith:
% rwho -a wally becky smith becky cygnus:pts0 Jan 17 11:20 :12 smith aquila:ttyp0 Jan 15 09:52 :22 wally lyra:pts7 Jan 17 13:15 1:32 wally lyra:pts8 Jan 17 14:15 1:01

As shown, the output from the rwho command displays in alphabetical order by user name, then by host name. The amount of idle time greater than one hour is shown in the last column, after the starting time and date of each session. Without the -a option, the information for user wally would not have displayed. For more information on the rwho command, see the rwho(1) reference page.

108 Obtaining Information About Network Users and Hosts

10.5 Determining Whether a Remote Host Is Online


The ping command is used by system administrators to x network transmission problems and works for any host congured in a TCP/IP network. As a network user, you can use it to determine whether a remote host is currently online. For example, to determine whether remote host moon is online, enter the ping command at your local system prompt. The output, which veries that the remote host is online, will continue to display until you press Ctrl/c, as shown in the following example:
% ping moon PING moon (130.180.4.108): 56 data bytes 64 bytes from 130.180.4.108: icmp_seq=0 ttl=255 time=42 ms 64 bytes from 130.180.4.108: icmp_seq=1 ttl=255 time=0 ms 64 bytes from 130.180.4.108: icmp_seq=2 ttl=255 time=0 ms 64 bytes from 130.180.4.108: icmp_seq=3 ttl=255 time=0 ms \*(KbCtrl/c\*(Ke ----moon PING Statistics---9 packets transmitted, 9 packets received, 0% packet loss round-trip (ms) min/avg/max = 0/4/42 ms

Obtaining Information About Network Users and Hosts 109

Sending and Receiving Messages

11

This chapter describes how to send or receive messages over the network by using one of the following commands: mailx or Mail write Message Handling (MH) program talk

Examples in this chapter use the mailx program rather than Mail. Using mailx, you can do the following tasks: Send a message to a user Edit a message before sending it Include les within messages Save or organize incoming messages

Using mailx you can also send entire les, a task described in Chapter 12, "Copying Files to Another Host." The Digital UNIX mailx or Mail command accesses the same mail program as the ULTRIX mail (that is, /usr/ucb/mail) command. Refer to Chapter 2 of the ULTRIX to Digital UNIX Migration Guide for more information. The write and talk commands work interactively; the recipient must be logged in. Before using these interactive commands, you can verify the name and availability of a user or host by using the following commands described in Chapter 10: finger or who to nd a user on the local host finger, rhwo, or ruptime to nd a user on a remote host ping or ruptime to nd a currently reachable host

11.1 Addressing Mail Messages


Using mailx, you can send a message to one or more users at the following locations:

On your local host On a remote host connected to your local host via TCP/IP On a host in another network, through either TCP/IP, DECnet, or UUCP addressing

Use the following syntax for the mailx command:


mailx user[@{ host | domain | host.domain } ] ...

To send mail to users on the local host, enter the mailx command and specify a user parameter for each user. For users on remote hosts, you must specify additional information about the location of the host after the at sign (@). For example, to send mail from host orange to users smith and jones on the same local host, you would enter the following command:
orange% mailx smith jones

To send mail to user hobbes on a different host, pluto, in the same domain, you would enter the following command:
orange% mailx hobbes@pluto

In the preceding example, if user hobbes were in another domain called planets, you would add the name of the remote network domain, as shown in the following command:
orange% mailx [email protected]

The domain is sometimes split into further subdivisions with the name of each separated by a period (.) in the destination name. Depending on how the network has been congured by the network administrator, you can address a user on a remote host in another domain by specifying only the domain name, as in the following command:
orange% mailx hobbes@planets

If necessary, see your system administrator for help addressing a mail message.

11.2 Sending a Mail Message Using mailx


This section explains how to use mailx to send a message to a user on a local host and a copy of the message to other users.

112 Sending and Receiving Messages

To begin the example, user Jones enters the mailx command at the local system prompt, orange%:
orange% mailx suzuki Return

After pressing the Return key, the Subject: prompt displays. Pressing the Return key again immediately would leave the subject blank. Instead, user Jones enters the subject of the message before pressing the Return key, and then begins writing the message:
Subject: Baseball question Return Are there any Japanese baseball simulation games? I want to compare Sadharu Ohs hitting statistics to those of Hank Aaron. To do this, I need to set up a simulated baseball season having each hitter play for one year in the other players league.

User Jones ends the message by typing a period (.) on a blank line, followed by the Return key, as shown:

. . .
play for one year in the other players league. . Return Cc:

In this example, the message has not yet been sent; instead the Cc: (that is, carbon copy) prompt appears because user Jones has customized his mail session by adding set askcc to the .mailrc le in his home directory. (See Section 11.6 and Appendix D on customizing your mail session.) The Cc: prompt enables you to send copies to other users. If you choose not to, press the Return key to exit mailx, and send the message; the endof-text message (EOT), then appears followed by the system prompt. In this example, user Jones sends copies to local user cranton and remote users gillis and vincep by entering the appropriate address for each at the Cc: prompt and pressing the Return key to exit mailx and send the message:

. . .
Cc: cranton gillis@strato [email protected] Return EOT orange%

Sending and Receiving Messages 113

The mailx program enables you to recover from addressing errors. For example, if your intended recipient on the local host is cranton, but you mistakenly type crantom, the following message appears immediately on your screen,
crantom... User unknown

and the message is mailed back to you. You can then save and resend it to the right person. If you send a message to an unknown person on a remote host, it may take as long as three days before mailx sends it back to you. Section 11.3.3.1 explains how to save and resend a returned message.

11.2.1

Editing a Message
To edit a mail message before sending it, after replying to the Subject: prompt, enter one of the following escape commands to activate an editor within mailx: Enter ~v to activate the screen editor that you set with the set VISUAL entry in your .mailrc le. Enter ~e to activate the text editor that you set with the set EDITOR entry in your .mailrc le.

To use the ~e command from within mailx to activate a text editor, enter ~e as the rst two characters on a new line you may need to type the tilde (~) a few times before it displays. For example:
Subject: network documentation meeting at 2 PM Everyone, please bring the Table of Contents for your book so that we can look for areas of overlapping subject matter and ~e

If your .mailrc le contains set EDITOR=/usr/ucb/vi, you can now use the vi editor to correct the spelling mistake in the rst line and nish writing the message. When you end the editing session, you are back in mailx. You can end the message and exit mailx or re-invoke vi and continue writing.

11.2.2

Aborting a Message
You may decide not to send a message that you have started. There are two ways to abort a message before sending it.

114 Sending and Receiving Messages

11.2.2.1

Aborting a Message with Ctrlc

You can abort a mail message by pressing Ctrl/c twice, anywhere within a message. When you rst press Ctrl/c, the following message is displayed:
(Interrupt -- one more to kill letter)

You can now reconsider your decision to abort the message. If you decide not to abort it, continue entering text. If you decide to abort the message, press Ctrl/c again, and the following message will be displayed:
(Last Interrupt -- letter saved in dead.letter)

The message is aborted, you exit mailx, and the system prompt is displayed. By default, the aborted message is saved in the dead.letter le in your home directory. If you choose not to save aborted messages, you can enter set nosave in your .mailrc le. See Section 11.6 and Appendix D for more information. Only the most-recently aborted message is saved in the dead.letter le. You can edit and resend it by including it within a mail message. (See Section 11.2.3 for information on including les within messages.) The following example shows how to abort a mail message by pressing Ctrl/c:
orange% mailx sally Subject: Update to reference page files What should the mailx(1) reference page include about sending to remote users? Ctrl/C (Interrupt -- one more to kill letter)
Ctrl/C

(Last Interrupt -- letter saved in dead.letter) orange%

11.2.2.2

Aborting a Message with an Escape Command

You can abort a mail message by entering either the ~q or the ~x escape command on a blank line. Unlike aborting a message by pressing Ctrl/c, these commands abort the message immediately, without prompting you to reconsider. The ~q escape command saves the aborted text in the dead.letter le in your home directory, but ~x does not, even if you have set save in your .mailrc le.

Sending and Receiving Messages 115

The following example shows how to abort a mail message by using the ~x escape command:
orange% mailx sally Subject: Update to reference page files What should the mailx(1) reference page include ~x orange%

You may need to enter the beginning tilde character (~) a few times before it appears.

11.2.3

Including a File within a Message


You can include any le (except an unconverted binary le) within a mail message. You will do this often when you save and resend any incorrectlyaddressed mail that is returned to you (See Section 11.3.3.1) or when you edit and resend an aborted message saved in the dead.letter le in your home directory. From an example in the previous section, the dead.letter le contains the following text:
What should the mailx(1) reference page include about sending to remote users?

Suppose that you want to resend this le to user sally after adding additional information. While in mailx, use the ~d escape command to automatically add the text of dead.letter to the mail message, regardless of the current working directory. The following example starts a message to user sally before adding the text of dead.letter through the ~d command:
orange% mailx sally Subject: the mailx(1) reference page The uucp(1) reference page has formatting information for sending to remote users. ~d "/usr/staff/r2/sally/dead.letter" 2/76

After including the le, its full path name is displayed, with the number of lines (2) and characters (76) that the le contains (including the Return key or a control character at the end of each line). After the display, you can exit or continue writing your message, but you may want to enter a text editor (for example, by typing ~v for vi) to look at the included le, which is not displayed otherwise.

116 Sending and Receiving Messages

Note The dead.letter le contains only the most-recently aborted mail message. You may want to verify that it contains the text that you want to send. To include a le (including dead.letter) within a mail message, you can use either of two escape commands, ~< or ~r followed by the le name. These commands work in the same way. If the le is not in the same directory from which you entered mailx, you must precede the le name with a full path name or one that is relative to the current directory. The following example uses the ~< escape command to include a le called strato_prob from the environ directory below the current working directory:
orange% mailx sally Subject: Dan, heres the stratosphere data file ~< environ/strato_prob "environ/strato_prob" 41/1309

See Section 12.1.3 for information about transferring a le non-interactively through the mailx utility.

11.3 Receiving a Mail Message


When you receive a mail message, you have the following options: Delete or read any message waiting for you. Reply to the sender and any other recipients to whom the mail was sent. Save the message in a le. Organize the message by topic in a le of saved messages called a folder.

The mailx program noties you if you have new mail when you log in, enter any operating system command, or press the Return key. You can also enter the mailx command at the system prompt to see if you have new mail. In the following example, user Jones on host orange enters mailx and nds two messages waiting:
orange% mailx Mail $Revision: 1.1.9.4 $ Type ? for help. "/usr/spool/mail/jones": 2 messages 1 new 1 unread U 1 root Mon Jul 20 10:39 14/438 "System news" >N 2 root Mon Jul 20 11:30 11/292 "Welcome" ?

Sending and Receiving Messages 117

In this example, two messages are waiting from the system administrator (root); one is unread (denoted by U in column 1) from a previous mailx session and the other is new (denoted by N). The question-mark (?) at the end of the message is the mailx prompt. You can type another questionmark at this prompt to display a list of available mailx commands, as indicated in the header and as described in Section 11.4. You can press the Return key at the mailx prompt to read message 2, which is indicated by the right-angle bracket (>), in the list of waiting messages. For example,
? Return Message 2: From root Wed Aug 4 11:17:36 1996 Date: Wed, 4 Aug 1996 11:17:29 -0400 From: root (system administrator) To: jones Subject: Welcome Welcome to the company computer network. Im the person who manages this system. If you have questions or problems, send mail to root. You can also send mail to manager or admin; messages will be forwarded to me. I will be on vacation for the next two weeks after this week... starting Monday, August 10. Ill be stdin Space back on Monday, August 24. ?

In this example, stdin displays because the PAGER variable has been set to more (the default). If the PAGER variable had been set to pg nothing would have been displayed. Also in the above example stdin appears after the 15th line of the message because user Jones has customized the mailx environment by adding set crt=15 to the .mailrc le. In the .mailrc le, set crt= species the number of lines to display at one time before invoking the pager (either pg or more). to display the remainder of the message. As shown in the example, because the message is more than 15 lines long, set crt instructs mailx to invoke the pager after 15 lines. By pressing the Space bar, the next screen full of the message are displayed. You should customize your mailx environment by using set crt=. Otherwise, long mail messages will scroll rapidly, requiring you to quickly press the Hold Screen key. See Section 11.6.2 and Appendix D for more information on customizing mailx.

118 Sending and Receiving Messages

To read another message, enter the message number at the mailx prompt. To list the messages again, enter h at the mailx prompt. In the following example, user Jones uses the h command to list the mail messages, sees that the rst message is still unread, and enters a 1 at the ? prompt to read it:
? h U 1 root Mon Jul 20 10:39 14/438 > 2 root Mon Jul 20 11:30 11/292 ? 1 Message 1: From root Mon Jul 20 11:30:07 1996 Date: Mon, 20 Jul 1996 11:30:04 -0400 From: root (system administrator) To: jones Subject: System news "System news" "Welcome"

The newest release of the text processing software will be installed after 5 oclock today. Send mail if you have questions or concerns before or after the installation. ?

The message you are reading is called the current message. To reread it, enter a period (.) at the mailx prompt. To read the next message, press the Return key. This message becomes the current message. You can read all your messages in succession by pressing the Return key after each message.

11.3.1

Deleting a Message
Your messages stay in mailx until you delete them before or after storing them in a le or in a folder. To delete the current message after reading it, enter the d (delete) command at the mailx prompt. To delete a different message, enter the d command at the mailx prompt, followed by the message number. You can delete several messages by listing their numbers after the d command. For example, enter the following command at the mailx prompt to delete messages 7 and 9:
? d 7 9

You can also delete a range of messages by using a hyphen between the rst and last message. For example, to delete messages 7 through 11, enter the following command at the mailx prompt:
? d 7-11

Sending and Receiving Messages 119

If you accidentally delete a message, you can recover it with the u (undelete) command. For example, to undelete message 7, enter the following command at the mailx prompt:
? u 7

If you exit mailx by entering q or quite instead of x, any previously read messages that you do not delete are added to the end of a le of previously undeleted messages named mbox, in your home directory.

11.3.2

Replying to a Message
Replying to a mail message is similar to sending a mail message. You have the same options to edit, abort, or include a le in a message, as described in Section 11.2. To reply to the sender of a message that you have just nished reading, enter an uppercase R (reply) command at the mail prompt, as shown in the following example:
Message 3: From deedee Mon Jul 20 14:13:32 1996 Date: Mon, 20 Jul 1996 14:13:05 -0400 From: deedee (DeeDee Smith) To: jones, [email protected], susannah@artwrk Subject: Testing text-processing software I think we should test the new text processing software on the older machines as well as the newer. Remember that many customers still have the older models. ? R To: deedee Subject: Re: Testing text-processing software I agree. Also, we should test different machine configurations to determine if, for example, it performs satisfactorily when run remotely. . EOT ?

After you enter R, the recipient and subject line display, enabling you to verify that you are replying to the intended recipient. If you enter a lowercase r, the reply will be sent to the recipients of the original mail as well as to the sender; in this example, to [email protected] and susannah@artwrk, as well as to deedee.

1110 Sending and Receiving Messages

Note To reply only to the sender of a mail message, enter an uppercase R at the mail prompt. To reply to the sender and all recipients of a mail message, enter a lowercase r at the prompt. In the preceding example, the Cc: prompt does not appear because user jones .mailrc le does not contain the set askcc command.

11.3.3

Saving a Message
If you leave mailx by entering the q command (instead of the x or exit command), the messages that you have just read are stored in the mbox le in your home directory. You can read this le by using the more or cat command, or you can use a text editor to read and edit it. To store mail messages in a more useful way, you can save them in individually-named les or in folders as described in the following sections.

11.3.3.1

Saving a Message in a File A brief but important message that you read An incorrectly-addressed mail message that mailx returns to you A long message that you want to print and read later

There are several kinds of mail that you might want to save in a le:

To save a brief message that you read or a mail message that mailx returned to you, enter the s command at the mailx prompt and supply a name for the le. A returned message is shown in the second item in the following output from the mailx command:
orange% mailx Mail $Revision: 1.1.9.4 $ Type ? for help. "/usr/spool/mail/jones": 2 messages 1 new 1 unread U 1 root Mon Jul 20 10:39 14/438 "System news" >N 2 MAILER-DAEMON Wed Aug 5 09:39 19/498 "Returned mail: User unknown" ?

As shown below, user Jones decides to save the returned message from the previous example in the le, verify-resend, as a reminder to nd the

Sending and Receiving Messages 1111

correct address before resending it.

. . .
>N 2 MAILER-DAEMON Wed Aug unknown" ? s verify-resend 5 09:39 19/498 "Returned mail: User

The le, verify-resend, is saved in the current directory unless an explicit pathname is specied. For example, user Jones could have saved it in a subdirectory called fix-later by entering the following command:
? s fix-later/verify-resend

To save a long message without reading the entire text online, press Ctrl/c to stop the message from scrolling and to display the mailx prompt. You can now save (or delete) the message. In the following example, user Jones receives a 20-page report in message 1, and presses Ctrl/c to access the mailx prompt where a command is entered for saving the le:
? 1 Message 1: From smith Wed Aug 5 16:43:42 1996 Date: Wed, 5 Aug 1996 16:43:41 -0400 From: smith (Cassandra Smith) To: jones Subject: 20-page report: host configuration results Mortimer, Heres the report on host configuration that the
Ctrl/c

Interrupt ?s sys-config-report "sys-cong-report" [New le] 2147/48353 ?dp

. . .

In the example above, after creating the le, user Jones enters dp (that is, delete-proceed) at the mailx prompt to prevent the large 20-page le from being saved in mbox, and to start reading the next mail message. Otherwise, the mailx command, d (delete) and pressing the Return key could have been entered to do the same. 11.3.3.2 Saving a Message in a Folder

To organize messages for easier reference and to minimize the size of the mbox le (which is a folder itself) you can save messages in les called folders. Before using a folder other than mbox, you must create a folder sub-directory in your home directory and add a pointer to the folder in your .mailrc le. For example, if you make a directory named folder in
1112 Sending and Receiving Messages

your home directory, you must add the following line to your .mailrc le: set folder=folder. To add a message to a folder, use the mailx command, s and the folder name. For example, to save a message about host conguration with other messages on that topic, write it to a folder named sys-config, as follows:
Message 7: From smith Thu Aug 6 09:32:09 1996 Date: Thu, 6 Aug 1996 09:32:08 -0400 From: smith (Cassandra Smith) To: jones Subject: host configuration testing According to the report that each LAN ... ? s sys-config "sys-config [New file] 11/235

When you save the rst message in a folder, mailx stores it and displays the message, New file. If you save more messages in that folder, mailx appends them to the end of the le and displays the message, Appended. There are two ways to read messages stored in a folder other than mbox: From the shell prompt you can start mailx with the -f option and the folder name. For example, to read the sys-config folder, enter the following command:
orange% mailx -f sys-config

If you are already in mailx, use the folder command to switch to a different folder. For example, if you are reading the sys-config folder and you want to read the meetings folder, enter the following command:
? folder meetings

When you switch folders, mailx makes any changes to the folder you are leaving before it opens the new folder. You can use the folder command without arguments to nd out what folder you are in. For example:
? folder "sys-config": 17 messages

Sending and Receiving Messages 1113

11.3.4

Forwarding a Message
You can use the m command and the ~f escape command in mailx to forward to other users any message that you receive. The following example shows how to start message forwarding to user deedee:
? m deedee Subject: forwarding a message

For example, to forward message number 3 to user deedee and include a subject line and introductory note, you would enter the following text:
I received this note from Gary. ~f 3 Interpolating: 3 (continue) . EOT Do you agree?

As shown, after you enter the ~f command, you can continue writing or end the message. To forward the current message, do not enter a number after the ~f.

11.4 Getting Help from mailx


When you enter mailx and messages are waiting, the following line is displayed at the top of the header:
Mail $Revision: 1.1.9.4 $ Type ? for help.

This is a reminder that you can type a question mark (?) at the mailx prompt to display a brief description of available mailx commands, as shown in Example 11-1. Example 11-1: Output from mailx Help Command
? ? Control Commands: q Quit - apply mailbox commands entered this session. x Quit - restore mailbox to original state. ! <cmd> Start a shell, run <cmd>, and return to mailbox. cd [<dir>] Change directory to <dir> or $HOME. Display Commands: t [<msg_list>] Display messages in <msg_list> or current message. n Display next message. f [<msg_list>] Display headings of messages. h [<num>] Display headings of group containing message <num>. Message Handling: e [<num>] Edit message <num> (default editor is e). d [<msg_list>] Delete messages in <msg_list> or current message. u [<msg_list>] Recall deleted messages. s [<msg_list>] <file> Append messages (with headings) to <file>. w [<msg_list>] <file> Append messages (text only) to <file>. pre [<msg_list>] Keep messages in system mailbox. Creating New Mail:

1114 Sending and Receiving Messages

Example 11-1:

(continued)

m <addrlist> Create/send new message to addresses in <addrlist>. r [<msg_list>] Send reply to senders and recipients of messages. R [<msg_list>] Send reply only to senders of messages. a Display list of aliases and their addresses. ============================ Mailbox Commands ==========================

11.5 Exiting Mail


There are three commands you can use to exit from mailx: The q command returns you to the shell prompt and saves in the mbox le in your home directory, any messages you read but did not delete. The x and exit commands are the same. Each returns you to the shell prompt without changing your mailbox.

11.6 Customizing Mail Sessions


When setting up an account for a new user, the system manager denes certain mailx default settings in the /usr/share/lib/Mail.rc le. As supplied by the operating system, this le contains the following mailx settings, which a user can override: The set ask setting activates the Subject: prompt. The set noaskcc setting deactivates the Cc: prompt. The set dot setting means that a single dot on a blank line (.) terminates the mail message. The set nokeep setting means that the system mailbox is deleted when it becomes empty. This setting is unimportant to most users. The set save setting means that aborted messages are saved in the dead.letter le.

You can customize your mailx session by dening aliases and setting variables in the .mailrc le in your home directory. Here is a sample .mailrc le:
alias sue susannah alias wombats tom, jeff, craig, jim, ken set ask set askcc set prompt=> unset dot set record=/usr/users/hale/outgoing set folder=folder set crt=20

Sending and Receiving Messages 1115

11.6.1

Creating Mail Aliases


You can use the alias command in mailx to create alternate names for users or user groups. Note The mailx alias is not the same alias command used by the shell; you cannot use it to modify mail commands. To dene a permanent mail alias, enter the alias command in the .mailrc le, specifying the alias name and one or more login names. The following .mailrc le denes two aliases:
alias sue susannah alias wombats tom, jeff, craig, jim, ken

The rst alias denes the name sue to mean user susannah. This enables you to send mail to susannah by using the name sue. The second alias enables you to send mail to members of a team called the Wombats tom, jeff, craig, jim, and ken, by addressing your message to wombats. Another way to enter this line in .mailrc is this:
alias wombats tom,\ jeff,\ craig,\ jim,\ ken

The backslash (\) enables you to write a single long command on several lines. While in mailx, you can see what aliases are dened by using the alias command without arguments. You can also dene temporary aliases at the mailx prompt that are in effect during that mailx session.

11.6.2

Setting Mail Variables


Mail variables are similar to variables in your .login le. They can be binary, string, or numeric. To set a binary mail variable in your .mailrc le, enter the set command followed by the option name. The sample .mailrc le includes these binary variables:
set ask set askcc unset dot

Setting ask makes mailx prompt you for the subject line of messages you send.

1116 Sending and Receiving Messages

Setting askcc makes mailx prompt you for carbon-copy recipients. Unsetting dot makes mailx refuse to end a message when you type a line with just a period on it; you would have to end a message by pressing Ctrl/d instead.

String mail variables accept characters or numbers as values. The sample .mailrc le includes the following three string variables:
set folder=folder set record=/usr/users/hale/folder/outgoing set crt=20

The folder variable denes a subdirectory to contain your mail folders. If you set this variable, the mailx utility creates folders as les in this directory when you save messages using the save folder command. The mailx utility interprets the le name as a subdirectory of your home directory. The record variable tells mailx to put a copy of each message you send in the le you specify. If you do not set this variable, no automatic record is kept. In this example, weve specied a le that will be treated as an ordinary folder by mailx. To select the record le, use the following command:
orange% mail -f outgoing

The crt variable tells mailx how many lines of a message should be displayed before invoking the pager program.

11.7 The Message Handling (MH) Program


An alternative to the mailx program is the Message Handling program (MH). The MH program is a set of small mail-handling programs that you use by entering the command you want to execute from the shell prompt. The MH program is optional; it may not be installed on your host. To determine if MH is available, look for the /usr/bin/mh directory. To use MH, you must add the /usr/bin/mh directory to your path by editing the set path line in your .cshrc or .login le. Then, notify the shell about the change in your path by logging out and logging back in, or by entering the following command (for the C shell):
orange% source .login

If your path is set in .cshrc, use .cshrc instead of .login in this command.

Sending and Receiving Messages 1117

For either the Bourne or Korn shell, you would add this information to the .profile le and notify the shell by entering the following command:
orange$ . .profile

With the MH program, folders are organized differently from mailx folders. New and unread mail is kept in a folder called inbox, into which you move the mail that arrives in your system mailbox by using the inc command. You must enter the inc command every time you want to include new mail. You select a folder with the folder command. If you enter it without a folder name, folder displays the currently selected folder. You can enter the folder command with the -all option to display a list of your folders and the number of messages in each. You use the show, prev, and next commands to read the current, previous, and next messages in your current folder. If you enter a message number with the show command, that message becomes your current message. For example:
orange% show 7 Message 7: From deedee Mon Jul 23 10:02:10 1996 Date: Mon, 23 Jul 96 10:01:25 edt To: hale Subject: Cafeteria hours Cc: Status: R Im sorry you didnt ask that sooner. The cafeteria closes its breakfast service at 10. Lunch starts at 11:30.

The rmm command removes messages from your current folder. If you use the rmm command with no argument, it deletes the current message. If you specify one or more message numbers, the messages you specify are removed. For example, to remove messages 2, 5, and 7, enter the following command:
orange% rmm 2 5 7

Table 11-1 lists most of the MH commands. For a complete list, see the mh(1) reference page. For more information about each MH command, see the reference page for each.

1118 Sending and Receiving Messages

Table 11-1: Commands for the MH Message-Handling Program


Command ali anno burst Description Searches the specied alias les and displays the addresses corresponding to the specied aliases. Annotates messages to keep track of distribution, forwarding, and replies for your messages. Extracts the original messages from a forwarded message, discards the forwarding header, and places the original messages at the end of the current folder. Creates a new mail message, providing a template for you to ll in and invoking an editor to nish the message. Redistributes the current message to addresses that are not on its original distribution list. Selects a folder or displays the contents of your current folder. Lists all your folders and the number of messages each one contains. Forwards messages to recipients who were not the original addressees. The message is encapsulated (included with a Forwarded message notice) and a header is added. Incorporates mail from your system mailbox into your inbox folder. Assigns a name to a sequence of messages in your current folder. You can then use the pick command to select messages marked in this way. Lists formatted MH messages. You can use this command as a replacement for the more command to display messages. Sends mail to the specied users. If you do not specify any users, mhmail works like the inc command. Checks your system mailbox and any other les that can receive new mail for you, looking for new messages. If any new messages are found, msgchk reports. Displays the next message in the current folder or in the specied folder. Compresses a folder into a single le. (Each message is normally stored as a separate le.) Do not confuse the packf command with the pack command. Selects messages based on content, sequence name, or other criteria.

comp dist folder folders forw

inc mark

mhl mhmail msgchk

next packf

pick

Sending and Receiving Messages 1119

Table 11-1:
Command prev prompter

(continued)
Description Displays the previous message in the current folder. Invokes a simple editor designed for composing messages. The prompter command is invoked by comp, dist, forw, and repl; you do not need to call prompter directly. Incorporates a message from the standard input directly into a folder. Moves messages from the current folder to one or more other folders. Replies to either the current message or the message you specify. Removes all of the messages in a folder and then removes the folder itself. Removes messages from a folder. The message les are not actually destroyed; instead, rmm renames them by inserting a number sign (#) as the rst character of the le names. On most hosts, les whose names begin with a number sign are deleted once a day by an automatic process. Until they are actually deleted, you can recover removed messages by using the mv command to rename the les. Displays a list of the messages in a folder. Sends a message that you have created by using comp, prompter, or another editor. Displays the contents of a message. Sorts messages in a folder into chronological order according to the Date: eld of the message header. Prompts you for what to do with a message you have just composed. You can reexamine an original message to which you are replying, resume editing the new message, or do other tasks associated with sending the message. Expands the header of a message into a set of addresses and optionally checks to see that the message can be delivered to those addresses.

rcvstore refile repl rmf rmm

scan send show sortm whatnow

whom

1120 Sending and Receiving Messages

The following example shows how the MH msgchk command reports new messages:
orange% msgchk You have new mail waiting, last read on date

You can tailor the features of MH by creating a .mh_profile le in your top-level directory. The MH reference pages describe the features that you can modify.

11.8 Sending and Receiving Messages With write


The write command enables two users on the same or different hosts to communicate on either a video display terminal or on nonvideo devices (for example, a teletypewriter) that print messages on paper. You can use write to send a message immediately to someone who you cannot reach by telephone, especially if you do not require a reply. (See also the talk command in Section 11.9.) The write command displays a message on the terminal screen of the recipient. You can prevent users from communicating with you through write and talk by entering the mesg n command in the .login le in your home directory. You cannot disable incoming messages from those with superuser privileges. To determine whether a user on a local host has disabled messages from write and talk, use the finger command and look at the rst line of output for the phrase messages off. For example:
Login name: smith

. . .

(messages off)

In real life: John Smith

For users on a remote host, the disabling of write and talk is denoted by an asterisk (*) in the TTY eld of the output line, for example:

. . .
Login chang Name Peter Chang TTY Idle When *p1 2:58 Thu 10:16 Office 103

See Section 10.2.2 and finger(1) for more information.

Sending and Receiving Messages 1121

Your intended recipient may be running a command that temporarily disables write to prevent its interference, and the sender would receive the message,
Write: Permission denied

just as if the recipient had explicitly disabled write. You can use the write command only when the recipient is logged in. Use the who command, as described in Section 10.1, to list current users. If, for example, user smith is not logged on when you send a message through write, the following message is displayed on your terminal screen:
smith is not logged on

The following steps show how user wang sends a message to user chung, both of whom are logged in on local host dancer: 1. User wang enters the write command at the system prompt:
dancer% write chung

The write program rings a bell and sends the following message to the terminal screen of chung:
message from wang tty04 Feb 14 10:32:45

A bell rings user wangs terminal when the connection is made. 2. User wang types the message, pressing the Return key after each line, and ends the message by pressing Ctrl/d. For example, wang sends the following message in two lines to user chung:
The double-sided lab printer is working. Return Re-send your job, and Ill check it. Return
Ctrl/d

3. After wang presses Ctrl/d, the EOF (end of le) signal is displayed on the screen of user chung to indicate the end of the message. Note See your system administrator if pressing Ctrl/d does not produce the EOF signal on the recipients screen or if a bell does not ring on the senders terminal.

1122 Sending and Receiving Messages

You can use the exclamation point (!) at the beginning of a new message line to access the shell prompt and execute any operating system command (including write). For example, if wang forgot the name of the current directory from which chung is to retrieve certain les, wang can enter the !pwd command to remind himself, as shown:
dancer% write chung You can copy the network user files from: Return !pwd Return /ufs/usr/staff/r0/net-dir/network_comm ! /ufs/usr/staff/r0/net-dir/network_comm
Ctrl/d

dancer%

The write command can be used interactively, but it is difcult for both sender and receiver to determine when the other has nished and is waiting for a reply. For example, wang can enter the following command:
dancer% write chung

Wang will then wait for chung to reply, but chung might also wait, thinking that wang intends to continue the message. To minimize problems, it is a good idea to establish a simple, temporary protocol each time you want to use write interactively. For example, user wang can start his message to chung as follows:
dancer% write chung Ill mark the end of each message with ZZZ Return and wait for a reply. Please do the same. Return Ill install a driver for the new printer. Return Do you want to test it? ZZZ Return

For more information, see the write(1) reference page.

11.9 Sending and Receiving Messages with talk


The talk command enables a user to send a message to another user on the same or on a remote host, interactively and more easily than through write. However, talk works only on video display terminals. Like write, you can use talk to send a message immediately to someone who you cannot reach by telephone. Also, like write, the talk command may disrupt the receiver because it sends a notication message directly to a terminal and continues doing so until a reply is entered. To disable incoming messages (except from those with superuser privileges) from talk (and from write, as described earlier) you can put the command, mesg n in your .login le. To determine whether a user has done this, use the finger command as described in Section 10.2.1 or in the finger(1) reference page.
Sending and Receiving Messages 1123

During an online talk session, a send window and a receive window are opened on each users terminal. Each can type into the send window while talk displays in the receive window what the other user is typing. For example, to send a message to user hoover on the same local host apple, user coolidge enters the following talk command:
apple% talk hoover

The program then divides the terminal screen of coolidge into two parts; the top half assigned to coolidge and the bottom half assigned to hoover. Next, the following message is displayed in the top of the screen:
[No connection yet]

When the connection is established, the following message is displayed:


[Waiting for your party to respond]

After this message, a bell rings on hoovers terminal and the following message is displayed:
Message from Talk_Daemon@apple at 16:18 ... talk: connection requested by coolidge@apple talk: respond with: talk coolidge@apple

If hoover does not respond quickly, the following message is displayed on coolidges screen:
[Ringing your party again]

When hoover responds, a message about the established connection appears on coolidges screen. Each user can now enter text. If the screen lls up, talk overwrites the text at the beginning of the screen. Either user can end the conversation by pressing Ctrl/c. The end of the talk session is marked as follows:
[Connection closing. Exiting]

1124 Sending and Receiving Messages

Copying Files to Another Host

12

This chapter explains how to use operating system commands to perform the following tasks: Copy les between a local and a remote host Copy entire directories (including subdirectories) of les between a local and a remote host Copy les between two remote hosts

To determine the host name or online status of a remote host before copying les, use the finger, who, rwho, ping, or ruptime commands described in Chapter 10. In addition to the information in this chapter, Chapter 14 provides information on using the UNIX-to-UNIX Copy Program (UUCP) to copy les to and from remote systems. Note The security features on the remote host determine whether or not you can copy a le. See your system administrator if you cannot copy a le.

12.1 Copying Files Between a Local and a Remote Host


You can use the following commands to copy les between a local and a remote host: rcp, described in Section 12.1.1; see Section 12.2 to copy entire directories of les. A host running the Digital UNIX operating system can use rcp with a host running any other UNIX based operating system. ftp, described in Section 12.1.2. You can use ftp to copy les between hosts using the following operating systems that also support ftp: Digital UNIX and VMS (with UCX). mailx, described in Section 12.1.3 write, described in Section 12.1.4

12.1.1

Using rcp to Copy Files Between Local and Remote Hosts


When using rcp to copy les from a local to a remote host or from a remote to a local host, name the le to be copied rst, followed by the destination le, as shown in this rcp syntax statement:
rcp [ option... ] localle hostname:le

The localfile variable identies the local le you want to copy. The hostname:file variable identies the remote host (hostname) followed by a colon (:) and the name of the le (file) to which the local le is copied. The following example uses rcp to copy the local le, YTD_sum from the directory /usr/reports on the local host to the le year-end in the directory /usr/acct on the remote host moon:
% rcp /usr/reports/YTD_sum moon:/usr/acct/year-end

You can also send a le on the local host to a user at a remote host. The following example shows how to copy the le YTD_sum from the directory /usr/reports on the local host to the le acct_summaries in the home directory of user jones on the remote host moon:
% rcp /usr/reports/YTD_sum jones@moon:acct_summaries

As used in the preceding examples, the rcp command assigns a new creation date and time to the le created from the original. It also assigns le readwrite-execute permissions according to the host or user directory containing the newly created le. You may need to preserve the original creation date and access permission mode of the copied le in the new le. As shown in the following example, the -p option enables you to preserve the original creation date and time and le access permission of YTD_sum in the le, year-end:
% rcp -p /usr/reports/YTD_sum moon:/usr/acct/year-end

If the -p option was not entered, a new date and time would have been assigned, and the le access permission would be set to the default assigned by the system administrator for remote host, moon.

122 Copying Files to Another Host

In the next example, the -p option preserves the same le creation and access permissions in the le acct_summaries as in the original le, YTD_sum:
% rcp -p /usr/reports/YTD_sum jones@moon:acct_summaries

If the -p option was not entered, a new date and time would have been assigned, but unlike the previous example, the le access permission would be the default set by user jones through the umask command (if any) in the .login le. If the umask is not set in the .login le, the default for remote host moon determines the le access permission mode. See umask(2) for more information about setting umask. To copy a le from a remote host to a local host, follow the rcp syntax statement shown below. The command syntax is the same as copying a local le to a remote host with the exception that localfile is the destination le, so it is placed last on the command line:
rcp [ option... ] hostname:le localle

12.1.2

Using ftp to Copy Files Between Local and Remote Hosts


The ftp command is the interface to the File Transfer Protocol (FTP) and has an extensive set of subcommands (described in Table 12-1, Table 12-2, and Table 12-3) that support the main task of copying les. You can use the ftp command to copy les between any two hosts that use the following operating systems: Digital UNIX, UNIX, and VMS (with UCX). See the ftp(1) reference page for a description of the ftp command options, which are used primarily for network administration tasks. Copying les through FTP consists of the following steps: 1. Establishing a session on the remote host 2. Copying the les 3. Disconnecting the session The ftp command has the following syntax:
ftp host_name

The host_name variable species the name of the host you want to reach. If you do not specify a host_name on the command line, you must use the ftp subcommand, open (described in Table 12-1) to connect with a remote host.

Copying Files to Another Host 123

After you type ftp, the ftp> prompt is displayed and you are logged in to the remote host. You can then use ftp subcommands to perform the following tasks: Copy les (See Table 12-1) List the contents of a remote directory (See Table 12-2) Change the current directory on the remote host (See Table 12-2) Append a local le to a remote le (See Table 12-1) Copy multiple les (See Table 12-1) Escape to the local shell to run commands (See Table 12-3) Delete les on remote hosts (See Table 12-2)

Example 12-1 shows how user alice on local host earth logs on to remote host moon, and uses ftp subcommands to check the current working directory, list its contents, copy a binary le, and end the session. Example 12-1: Using ftp to Copy a File
earth% ftp moon 1 Connected to moon. 220 moon FTLPServer systemname ready. 2 Name(moon:alice): Return 3 Password: 4 230 User alice logged in 5 ftp> binary 6 200 Type set to I ftp> pwd 7 257 "u/alice" is current directory ftp> ls -l 8 200 PORT command successful. 150 Opening data connection for /bin/ls (192&.9&.200&.1,1026) (0 bytes) total 2 -rw-r--r--rw-r--r-1 alice 1 alice system system 101 Jun 171 Jun 5 10:03 file1 5 10:03 file2

-rw-r--r-1 alice system 1201 Jun 5 10:03 sales ftp> get sales newsales 9 200 PORT command successful. 150 Opening data connection for testfile (192&.9&.200&.1,1029) (1201 bytes) 226 Transfer complete. local:tmp.testfile remote:testfile ftp> quit 10 221 Goodbye. earth%

124 Copying Files to Another Host

1 2 3 4 5 6

User alice enters the ftp command at the prompt of local host, earth to begin an ftp session with remote host, moon. A message verifying the connection is displayed on the local host. User alice presses Return at the prompt because her login name is the same on the remote host. At the Password: prompt, user alice enters a valid password that is not displayed. The login to the remote host is veried and the ftp> prompt appears, establishing the ftp session with the remote host. User alice enters the binary subcommand at the ftp> prompt to set the le transfer type to binary and FTP veries it with the message 200 Type set to I. User alice enters the pwd subcommand to identify the current working directory, and FTP veries it with the message u/alice is current directory. User alice enters the ls -l subcommand to list the contents of the current working directory, file1, file2, and sales. User alice copies the le sales from the remote host to a le called newsales on the local host through the get subcommand. returns to the local system prompt.

8 9

10 User alice enters the quit subcommand to end the ftp session and

Table 12-1 describes the ftp subcommands that copy les and exit ftp. The binary, get, and quit subcommands were used in Example 12-1. Table 12-1: ftp Subcommands for Connecting to a Host and Copying Files
Subcommand account [password] Description Sends a supplemental password that a remote host other than a Digital UNIX host may require before granting access to its resources. If the password is not specied, the user is prompted for it. The password does not appear on the screen. Sets the le transfer type to network ASCII, which is the default. For example, a PostScript le is an ASCII le. Sets the le transfer type to binary image. This may be more efcient when copying non-ASCII les. For example, a DECwrite le is non-ASCII.

ascii

binary

Copying Files to Another Host 125

Table 12-1:
Subcommand bye

(continued)
Description Ends the le copying session and exits FTP; same as quit. Copies the remote le, remfile to the le, locfile on the local host. If locfile is not specied, the remote le name is used locally. See also the runique subcommand. Copies one or more specied les (remfile) from the remote host to locfile in the current directory on the local host (supports wildcard or patternmatching metacharacter expansion). Copies one or more specied les (locfile) from the local host to remfile on the remote host (supports wildcard or pattern-matching metacharacter expansion). Establishes a connection with the specied host, if you did not specify it on the command line. If port is specied, FTP attempts to connect to a server at that port. If the autologin feature is set (the default), FTP tries to log the user in to the remote host. Stores a le, locfile on the local host, in the le remfile on the remote host. If you do not specify remfile, FTP uses the local le name to name the remote le. See also the sunique subcommand. Ends the le copying session and exits FTP; same as bye. Copies the remote host le, remfile to the le, locfile on the local host; recv works like get. Toggles, creating unique le names for local destination les during get operations. If the unique local le name feature is off (the default), FTP overwrites local les. Otherwise, if a local le has the same name as one specied for a local destination le, FTP appends a .1 extension to the specied name of the local destination le. If a local le already has the new name, FTP appends a .2 extension to the specied name, and so on up to a value of 99. If FTP still cannot nd a unique name, it reports an error and the le is not copied. Note that runique does not affect local le names generated from a shell command.

get remfile locfile

mget remfile [locfile]

mput locfile [remfile]

open host [port]

put locfile [remfile]

quit recv remfile [locfile] runique

126 Copying Files to Another Host

Table 12-1:
Subcommand send locfile [remfile] sunique

(continued)
Description Stores a local le, locfile in the le, remfile on the remote host; send works like put. Toggles, creating unique le names for remote destination les during put operations. If the unique remote le name feature is off (the default), FTP overwrites remote les. Otherwise, if a remote le has the same name as specied for a remote destination le, the remote FTLPserver modies the name of the remote destination le in the same way that runique does, and it must be supported on the remote host.

Table 12-2 describes the ftp subcommands that enable you to verify, change, or create the current directory and list its contents before you copy les, if necessary. The pwd and ls subcommands were used in Example 12-1. Table 12-2: ftp Subcommands for Directory and File Modication
Subcommand cd remotedir cdup delete remfile dir [remdir] [locfile] lcd [directory] Description Changes the working directory on the remote host to remotedir. Changes the working directory on the remote host to the parent of the current directory. Deletes the specied remote le. Lists the contents of remote directory remdir to the le, locfile on the local host. Changes the working directory on the local host. If you do not specify a directory, FTP uses your home directory. Writes an abbreviated le listing of a remote directory, remdir to a local host le, locfile. Creates specied directory on remote host. Displays the name of the current directory on the remote host. Renames a le on the remote host.

ls [remdir] [locfile] mkdir [remdir] pwd rename from to

Copying Files to Another Host 127

Table 12-2:
Subcommand rmdir remdir

(continued)
Description Removes the remote directory remdir from the remote host.

Table 12-3 describes the ftp subcommands that provide help or status information directly or by invoking the shell from within ftp. Table 12-3: ftp Subcommands for Help and Status Information
Subcommand !command [option] ?[subcommand] Description Invokes an interactive shell on the local host. Displays a help message describing the subcommand. If you do not specify subcommand, FTP displays a list of known subcommands. See also the help subcommand. Displays help information. See also the ? subcommand. Displays current status of ftp, including the current transfer mode (ASCII or binary), connection status, time-out value, and so on. Toggles verbose mode. When verbose mode is on (the default), FTP displays all responses from the remote FTLPserver. Also, FTP displays statistics on all completed le transfers.

help [subcommand] status

verbose

The tftp command, which is the interface to the Trivial File Transfer Protocol (TFTP), provides another way of copying les. Unlike ftp, it does not provide subcommands for any other tasks and is recommended only for tasks performed by the superuser or the installer of the operating system (e.g., copying the operating system kernel). Limited le access privileges are given to the remote tftp server daemon, tftpd. See the tftp(1) reference page for more information.

128 Copying Files to Another Host

12.1.3

Using mailx to Copy ASCII Files Between Local and Remote Hosts
The mailx command copies ASCII les to a local or remote host, although mailx is most often used to send and receive mail messages as described in Chapter 11. You can copy an ASCII le to one or more users through mailx by using the left-angle bracket redirection symbol (<) as shown in the following syntax:
mailx [ option... ] recipient... < lename

The recipient variable species one or more user names or a mailx alias to whom you want to send the le, filename. For example, to send the le schedule to several users, you could use the mailx command, as shown with its -s option that indicates the subject of the message:
% mailx -s "games" tom jeff craig jim ken < schedule

If you create a mail alias of wombats (See Section 11.6.1) for these ve members of a team called Wombats, you can send the le to that alias, as shown:
% mailx -s "games" wombats < schedule

12.1.4

Using write to Copy Files Between Local and Remote Hosts


The write command copies les to a local or remote host, although write is most often used to write messages to other users as tasks described in Section 11.8. After you type write, enter the user name of the recipient, a left-angle bracket redirection symbol (<), and the name of the le you want to send. For example, to send a le named letter in your current directory to user maria, enter the following command at the host prompt:
% write maria < letter

12.2 Copying Directories of Files Between a Local and a Remote Host


The -r option of the rcp command enables you to copy entire directories of les recursively (that is, including les within any subdirectories) between a local and a remote host.

Copying Files to Another Host 129

To copy a directory recursively from your local host to a remote host, use the following syntax:
rcp -r localdirectory hostname:directory

The localdirectory variable identies the local directory that you want to copy recursively. The hostname:directory variable identies the remote host (hostname) followed by a colon (:) and the name of the remote directory (directory) to which the local directory is copied. The following example uses rcp -r to copy recursively the directory /usr/reports from the local host to the directory /user/status/newdata on remote host moon:
% rcp -r /usr/reports moon:/user/status/newdata

You can also copy recursively a directory on your local host to a user at a remote host. The following example shows how to copy the directory /usr/reports on the local host to the directory /user/status/newdata in the home directory of user smith on the remote host moon. This example also uses the -p option, as explained in Section 12.1.1, to preserve the original creation date and access permission mode of the directory that is copied in the new directory:
% rcp -p -r /usr/reports smith@moon:/user/status/newdata

To copy a directory recursively from a remote host to your local host, follow the rcp syntax statement shown below. The command syntax is the same as copying a directory recursively from a local to a remote host with the exception that localdirectory is the destination le, so it is placed last on the command line:
rcp -r hostname:directory localdirectory

12.3 Copying Files Between Two Remote Hosts


From your local host, rcp can copy a le on one remote host to a le on another remote host. To do this, use the following rcp syntax:
rcp remhost1:lesend remhost2:le-recv

The remhost1 variable identies the remote host containing the le you want to send, followed by a colon (:) and the le, filesend that you want to send. The last part of the statement identies the second remote host, remhost2, and the le name, file-recv, to which the le from remhost1 will be copied. If only a directory name is given in filerecv (as in the example below), filesend will be copied there with the same le name.
1210 Copying Files to Another Host

The following example uses rcp to copy the le spark from the directory /u/cave/fred on remote host flint to the directory /u/hut/barney on remote host stone:
% rcp flint:/u/cave/fred/spark stone:/u/hut/barney

Copying Files to Another Host 1211

Working on a Remote Host

13

The chapter explains how to use commands which enable you to: Log in to a remote host from your local terminal Execute a specied command at a remote host, or if no command is specied, logs in to the remote host Log in to a remote host using the Telnet protocol. If rlogin is not supported, use telnet as an alternative. Note Any remote login is subject to the security features on the remote host. If you have difculty logging in to a remote host, see your system administrator. Before using any of these commands you might need to know the correct host name or whether a remote host is currently reachable. Use the finger, who, rwho, ruptime, and ping commands, described in Chapter 10 to nd this information.

13.1 Using rlogin to Log in to a Remote Host


You can log in to a remote host with rlogin, using the following command syntax:
rlogin [-l user ] host_name

The -l option enables you to specify a remote username other than your local username. The host_name variable species the remote host. The following steps show how to log in to a remote host boston where the login name is the same as that on the local host:

1. Enter the following rlogin command followed by the name of the remote host. For example:
% rlogin boston Password:

2. Enter your password. When the system prompt is displayed, you are logged in to the remote host and can enter any command. 3. To close the connection and return to your local host, press Ctrl/d. If you have an account on a remote host where your login name is different from that on the local host, you must use the -l option to log in to the remote host, as shown in the following steps. 1. Enter the rlogin -l command followed by the remote login name and the name of the remote host. For example:
% rlogin -l celtic boston Password:

2. Enter the password corresponding to the login name, celtic. When the system prompt is displayed, you are logged in to the remote host and can enter any command. 3. To close the connection and return to your local host, press Ctrl/d. In the following situations, rlogin will not prompt for a password: If your local host is listed in the /etc/hosts.equiv le on the remote host If the name of your host (and optionally, your username) is listed in the .rhosts le in your home directory on the remote host

For more information on rlogin, see the rlogin(1) reference page.

13.2 Using rsh to Run Commands on a Remote Host


The rsh command enables you to run a single command on a remote UNIX based host without logging in there. If you need to run several commands successively, you must log in to the remote host using either rlogin or telnet. The rsh command has the following syntax:
rsh [-l user ] host_name command

The -l option enables you to log in to a remote host where your login name, user, is different from that on the local host. If you do not specify the -l

132 Working on a Remote Host

option, rsh assumes that your login name is the same on both the local and remote hosts. The host_name variable species the name of the remote host. The command variable species the command you want to run. Note If you do not specify a command to run remotely, rsh prompts you for login information to the remote host. To use rsh, one of the following must be true: Your local host is listed in the /etc/hosts.equiv le on the remote host. Your host is listed in the .rhosts le in your home directory on the remote host.

In the following example, rsh appends a le located on a remote host to a le on the local host. The remote le, remfile2, on host remhost2 is appended to a local le called locfile:
% rsh remhost2 cat remfile2 >> locfile

13.3 Using telnet to Log Into a Remote Host


You can log into a remote host by using the telnet command, which implements the Telnet protocol. Using telnet you can: Log in to a remote host Execute any operating system command on the remote host Enter telnet subcommands (see Table 13-1) for managing the remote session

The telnet command has the following syntax:


telnet [ host_name[ port ] ]

The host_name variable species the remote host. If you omit the host name, you can use the open subcommand to create a connection after you activate the Telnet utility. If you do not specify a port, the Telnet protocol attempts to contact a Telnet server at the default port. The following steps show how to use the telnet command to log in to a remote host named host3, and to use the telnet subcommand status:

Working on a Remote Host 133

1. Enter the telnet command and specify the host_name as host3 (the default port is used):
% telnet host3 Trying... Connected to host3 Escape character is ^]. host3 TCLPTelnet service. login:

2. Enter your login name; enter your password. 3. Press Ctrl/] (the default escape sequence) to access the telnet subcommand prompt, telnet>. 4. Enter the status subcommand at the prompt, to display status information similar to the following:
telnet> status Connected to host3. Operating in character-at-a-time mode. Escape character is ^].

5. To use another subcommmand, press Ctrl/] to redisplay the telnet> prompt. To display the remote host prompt, press the Return key. 6. To quit the Telnet session from the host prompt, press Ctrl/d. To quit the Telnet session from the telnet> subcommand prompt, enter q, or press Ctrl/d. You can enter the telnet command without any arguments to access the telnet subcommand mode, indicated by the telnet> prompt. The telnet subcommands are described in Table 13-1. Before entering a subcommand, you must enter the escape sequence, Ctrl/]. This sequence noties the telnet program that the following information is not text; otherwise, telnet would interpret subcommands as text. For each subcommand, you only need to type enough letters to uniquely identify the command. For example, q is sufcient for the quit command. For a complete list of telnet subcommands, see the telnet(1) reference page. Table 13-1: telnet Subcommands
Subcommand ? [subcommand] Description Displays help information. If a subcommand is specied, information about that subcommand is displayed.

134 Working on a Remote Host

Table 13-1:
Subcommand close

(continued)
Description Closes the connection and returns to telnet command mode. Displays all of the set and toggle values if no argument is specied; otherwise, lists only those values that match argument. Opens a connection to the specied host. The host specication can be either a host name or an Internet address in dotted decimal form. If no port is given, telnet attempts to contact a telnet server at the default port. Closes a connection and exits the telnet program. Pressing a Ctrl/d in command mode also closes the connection and exits. Shows the status of telnet, including the current mode and the currently connected remote host. Opens a shell on the local host as specied by the SHELL environment variable. When you exit the shell by pressing Ctrl/d, telnet returns to the remote session.

display [argument]

open host [port]

quit

status z

Working on a Remote Host 135

The UUCP Networking Commands

14

This chapter describes the UNIX-to-UNIX Copy Program (UUCP). Using UUCP enables you to: Perform tasks on a remote host Transfer les between a local and remote host Work in background mode Switch back and forth between the local and remote host, performing tasks on either or both, concurrently

For an overview of UUCP system management and tasks, see the Network and Communications Overview. With UUCP, you can connect over a hardwired asynchronous line, a network, or a telephone line (using modems at both ends) to: Another workstation Another computer running a UNIX based operating system A computer running an operating system that is not UNIX based (this requires certain hardware and software)

14.1 UUCP Pathname Conventions


UUCP pathnames follow the same conventions as the operating system with the following exceptions: Relative pathnames may not work with all UUCP commands. If not, reenter the command and use the full pathname. On hosts that support UUCP, the /usr/spool/uucppublic directory is set up for transferring data among other hosts. The brief form of this directory is ~uucp. The pathname for a le on a remote host requires an exclamation point, (!) after the host name. For example, sea!/research/new species the le new in the directory /research on host sea. Sometimes to specify a le, you must provide the names of the remote hosts along the network path. To do so in the Bourne or Korn shells, put an exclamation point (!) after each host name. For example,

gem!car!sea!/research/cells species the le cells in directory /research on host sea, which is reachable through host car; car is reachable through host gem. In the C shell, the exclamation point (!) will be mistranslated unless you precede it with a backslash (\), for example:
gem\!car\!sea\!/research/cells

14.2 Finding Hosts that Support UUCP


To communicate with a remote host by using UUCP commands from your local host, you must determine which other hosts support the UUCP protocol. The UUCP uuname utility displays a list of all hosts with which you can communicate using UUCP from your local host. The following example shows the uuname command with output.
% uuname elvis fab4

This example shows that two remote hosts are accessible to the local host through UUCP. To identify the local host, use the -l option to the uuname command. For example:
% uuname -l music

By using UUCP commands among compatible hosts, a user on host music can send to or receive les from hosts elvis or fab4. For more information, refer to the uuname(1) reference page.

14.3 Connecting to a Remote Host


Before you can use UUCP commands, you must connect your local host to the remote host. There are three commands you can use to connect to a remote host: The cu and tip commands establish a full-duplex connection, giving the appearance of being directly logged in to the remote host. This connection enables the simultaneous transfer of data between the hosts. The ct command establishes a remote connection by letting you dial an attached terminal and log in via a modem and telephone line.

142 The UUCP Networking Commands

Note A remote connection is subject to the security features on the local and remote host. See your system administrator for more information.

14.3.1

Using cu to Connect to a Remote Host


The cu command and its options enable you to connect to a remote host, log in to it, and perform tasks there from your local host. You can perform tasks on each host by switching back and forth between the two. If both hosts use the operating system, you can enter commands on the remote host from your local host.

14.3.1.1

Using cu to Connect by Name to a Remote Host

The following steps show how to use the cu command to connect from local host earth to remote host moon, log in to moon, and enter a command there: 1. Enter the following cu command at the local system prompt; a message veries the connection:
earth% cu moon Connected

The login prompt for the remote host will be displayed. When connecting to some remote hosts, you may need to press the Return key several times before a login prompt is displayed. 2. Log in to host moon at the login prompt. The system prompt for host moon is displayed. 3. At the system prompt, enter any command that host moon supports. For example, to list the contents of the /usr/geog/crater/earthside directory, enter the following command at the system prompt:
moon% ls /usr/geog/crater/earthside copernicus.dat tycho.dat moon%

The UUCP Networking Commands 143

Note The preceding example may not work for all cu connections. It is used here as a brief, general example. See your system administrator for more information. After logging in to the remote host, you can switch back and forth between it and the local host because they run concurrently. To return to your local host and enter a command there, type a tilde and an exclamation point (~!) followed directly by the command, or wait for the local host prompt to display and then enter the command. To return to the remote host, press Ctrl/d. 14.3.1.2 Using cu to Specify a Directly-Connected Remote Host

To connect to a directly-connected remote host, use the cu command with the -l option to name the hardwired line that connects the two computers. Most of these communication lines have names that are variations of the standard device name, tty. To use the cu command with the -l option to connect to a remote host with an unknown name, but which uses hardwired device ttyd0, enter the following command:
earth% cu -lttyd0 Connected

After the connection is made, you can log in to and execute commands on remote host moon. Refer to the steps in Section 14.3.1.1. To return to the local host, type a tilde and an exclamation point (~!) followed directly by the command or wait for the local system prompt to display before entering the command. To return to the remote host, press Ctrl/d. Note If you use the -l option, but still enter the name of a remote host, no error message will be generated. Instead, cu will try to connect to the rst available line for the requested host name, ignoring the specied line. If it makes the connection, it may not be the one you intend.

14.3.1.3

Using cu to Connect by Telephone to a Remote Host

You can use cu to connect by telephone to a remote host whenever the remote host has not been set up to communicate with the local host through UUCP. To do so, the following conditions must be met:

144 The UUCP Networking Commands

Both the local and the remote host are connected to modems. You know the telephone number of the remote modem and have a valid login on that host.

The following example shows how to use the cu command to connect to a remote host that has a long-distance telephone number of 1-612-555-6789. The -s option species a transmission rate of 300 baud. Assuming that dialing 9 is necessary for an outside dial tone, enter the following cu command at the local system prompt:
earth% cu -s300 9=16125556789 Connected

After the connection is made, you can log in to and execute commands on the remote host. (Refer to the steps in Section 14.3.1.1). To return to the local host, type a tilde and an exclamation point (~!) followed directly by the command or wait for the local system prompt to display before entering the command. To return to the remote host, press Ctrl/d. If you do not use the -s option to specify a transmission speed, an appropriate rate is selected by default from data in /usr/lib/uucp/Devices. For added security use the -n option, which prompts you for the telephone number. This suppresses the display of the phone number with the ps command, which would otherwise display the number with the cu command that you enter. Table 14-1 summarizes the cu command options and entries. See the cu(1) reference page for more information. Table 14-1: Options to the cu Command
Option sspeed Description Species the rate at which data is transmitted to the remote host. The default rate, set during UUCP installation and based on data in /usr/lib/uucp/Devices, should be sufcient for most of your work. Most modems operate at 300, 1200, or 2400 baud, while most hardwired lines are set to 9600 baud or higher. When transferring a large le, you may want to specify the low 300-baud rate for less interference on the line. e | o Specify -e for even or -o for odd parity for data sent to the remote host.

The UUCP Networking Commands 145

Table 14-1:
Option h d n lline

(continued)

Description Specify -h to emulate local echoing, to support calls to other hosts that expect terminals to be set to half-duplex mode. Specify -d to print diagnostic traces. Specify -n to have cu prompt for a telephone number (for added security) Species the name of a device (line) for the communication between two computers. The default is either a hardwired asynchronous line, or a telephone line with an automatic dialer such as a modem. If your site has several communication lines, you may want to specify a particular line for your cu link. Usually, you do not have to specify a line or device; the default established during UUCP installation should be sufcient. If you want to connect to a remote computer but do not know its name, you can enter the cu command with the -l option and a variation of the standard device name tty (for example, -ltty1). Ask your system administrator for the device names at your site.

Dials a terminal that has been set to auto-answer, and maps carriage return to carriage return/linefeed. Species the name of the remote host with which you want to establish a connection. Species the telephone number in a remote connection using a modem.

host_name telno

14.3.1.4

Local cu Commands

While connected by cu to a remote host, you can use local cu commands to perform the following tasks: Go back and forth between the local and remote hosts Change directories on the local host Copy les between local and remote hosts Terminate a remote connection

146 The UUCP Networking Commands

To return temporarily to the local host to work, type a tilde and an exclamation point (~!) at the remote system prompt; wait for the local system prompt to be displayed in the following form, where local is the name of the local host:
~[ local ] !

Instead of waiting for the local system prompt to display, you can enter the command immediately after typing the ~! that accesses the local host. For example, while connected by cu to remote host moon, you can enter the following command to return to local host earth and use the cat command to read the /usr/crew/r2/asimov/AI le:
moon% ~!cat /usr/crew/r2/asimov/AI

There are three cu local commands for tasks that are performed very often. You enter these commands from the remote host to perform tasks on the local host while you continue working on the remote host. These commands are preceded by a tilde and a percent symbol:
~%cd directory_name ~%take from [to] ~%put to [from] changes the directory on the local host copies a le from the remote host to the local host copies a le from the local host to the remote host

For example, while connected by cu to remote host moon, you can change the current directory on local host earth from /usr/geog/ocean to /usr/geog/ocean/pacific by entering the following command:
moon% ~%cd pacific

While connected by cu to remote host moon, you can copy the le, /usr/ETI/clavius to the le /usr/NASA/decode on local host earth by entering the ~%take local command at the remote system prompt:
moon% ~%take /usr/ETI/clavius /usr/NASA/decode

While connected by cu to remote host moon, you can copy the local le /usr/NASA/jupiter to the le, /usr/ETI/clavius/hal9 on the remote host by entering the ~%put local command at the remote system prompt:
moon% ~%put /usr/NASA/jupiter /usr/ETI/clavius/hal9

The UUCP Networking Commands 147

Note Before using the ~%take and ~%put commands, verify that the destination directory exists. Unlike the uucp command, these cu local commands do not create intermediate directories during le transfers. You can transfer only ASCII les with ~%take and ~%put. (For example, a PostScript le is an ASCII le, but a DECwrite le is not.)

14.3.1.5

Using cu to Connect a Local Computer to Several Remote Computers

You can enter the cu to connect host X to host Y, log on to host Y, and then enter the cu command there to connect to host Z. You then have one local host computer, X, and two remote host computers, Y and Z. You can run an operating system command on host Z after you log in there. Then, from Z, you can run commands on the other hosts as follows: To run a command on host X, prex the command with a single tilde (~) To run a command on host Y, prex the command with two tildes (~~)

Table 14-2 summarizes the most common cu local commands. For information about other cu local commands, refer to cu(1). Table 14-2: Local cu Commands
Command ~. Description Logs you off the remote computer and terminates the remote connection. When connected to the remote host over a telephone line using a modem, this command does not always work. In such cases, press Ctrl/d to log off; then type a tilde and a period (~.) at the prompt and press the Return key to terminate the remote connection. ~! Returns the session from the remote host to the local host. Type a tilde and an exclamation point (~!) at the prompt and enter any command. To return to the remote host, press Ctrl/d. After establishing the cu connection, you can go back and forth between the two hosts by typing ~! (to go from remote to local) and pressing Ctrl/d (to go from local to remote).

148 The UUCP Networking Commands

Table 14-2:
Command

(continued)
Description Changes the current directory on the local host to that specied by the directory_name variable. If no directory name is specied, cu changes it to your home directory. Copies a le from the remote to the local host. If you do not specify a dest destination le on the local host, the ~%take command copies the remote le to the local host and assigns the same le name. Copies a le from the local to the remote host. If you do not specify a dest destination le on the remote host, the ~%put command copies the local le to the remote host and assigns the same le name. Execute the cmd command on the local host and sends the output to the remote host for execution by the remote shell.

~%cd directory_name

~%take source [dest]

~%put source [dest]

~$cmd

14.3.2

Using tip to Connect to a Remote Host


The tip command and its options enable you to connect to a remote host, log in to it, and perform tasks there from your local host. You can do tasks on each by switching back and forth between the two. If both hosts use the operating system, you can enter commands on the remote host from your local host.

14.3.2.1

Using tip to Connect by Name to a Remote Host

The following steps show how to use the tip command to connect from local host earth to remote host moon, log in to moon, and enter a command there: 1. Enter the following tip command at the local system prompt; a message veries the connection:
earth% tip moon Connected

The login prompt for the remote host will be displayed. When connecting to some remote hosts, you may need to press the Return key several times before a login prompt is displayed.

The UUCP Networking Commands 149

2. Log in to host moon at the login prompt. The system prompt for host moon is displayed. 3. At the system prompt, enter any command that host moon supports. For example, to list the contents of the /usr/geog/crater/darkside directory, enter the following command at the system prompt:
moon% ls /usr/geog/crater/darkside copernicus.dat tycho.dat moon%

Note The preceding example may not work for all tip connections. It is used here as a brief, general example. See your system administrator if necessary. After logging in to the remote host, you can switch back and forth between it and the local host because they run concurrently. To return to your local host and enter a command there, type a tilde and an exclamation point (~!) followed directly by the command, or wait for the local host prompt to display and then enter the command. To return to the remote host, press Ctrl/d. 14.3.2.2 Using tip to Connect by Telephone to a Remote Host

You can use the tip command to connect by telephone to a remote host if the following conditions are met: Both the local and the remote host are connected to modems. You know the telephone number of the remote modem or there is an entry for the remote host in /etc/remote.

The following steps show how to use the tip command to connect to a remote host that has the local telephone number 555-1234 and species a transmission rate of 300 baud: 1. Enter the following tip command at the local prompt, jupiter; a message veries the connection:
jupiter% tip -300 5551234 Connected

2. Press the Return key. When connecting to some remote hosts, you may need to press the Return key several times before the remote hosts login prompt is displayed.

1410 The UUCP Networking Commands

3. Log in at the remote host login prompt. The connection to your local host is still open, so you can work on the local or remote host. Note If you do not specify a transmission speed, the tip command uses a 1200-baud rate by default. When transferring a large le, you may want to specify the low 300-baud rate for less interference on the line. The following steps show how to use the tip command to connect, with a 300-baud transmission rate, to a remote host that has a long-distance telephone number of 1-612-555-9876. 1. Assuming that you must dial 9 for an outside dial tone, enter the following tip command at the prompt of local host earth; a message veries the connection:
earth% tip -300 9,16125559876 Connected

2. Press the Return key. When connecting to some remote hosts, you may need to press the Return key several times before the remote hosts login prompt is displayed. 3. Log in at the remote hosts login prompt. The connection to your local host is still open, so you can work on the local or remote host. For information about customizing the /etc/remote and /etc/phones les, refer to the book, Network Administration and the remote(4) and phones(4) reference pages. Table 14-3 summarizes the tip command options and entries. See the tip(1) reference page for more information.

The UUCP Networking Commands 1411

Table 14-3: Options to the tip Command


Option -baud_rate Description Species data transmission rate to the remote host. The default rate is 1200 baud. Most modems operate at 300, 1200, or 2400 baud, while most hardwired lines are set to 9600 baud or higher. When transferring a large le, you may want to specify the low 300-baud rate for less interference on the line. The baud rate, set when UUCP is installed and customized for your site, is congured according to the hardware used to establish connections. v host_name Displays any variables as they are read (verbose) from the .tiprc le. Species the remote host to which you want to connect; the tip command connects over a hardwired line or a telephone line using a modem, depending on how your system communications is set up between the local and remote hosts. Species the telephone number in a remote connection, using a modem. Use this method when the remote host name is not recognized by tip (that is, there is no entry in the /etc/remote le).

telno

14.3.2.3

Local tip Commands

While connected by tip to a remote host, you can use local commands to perform the following tasks: Go back and forth between the local and the remote host Change directory on the local host from the remote host Copy les between local and remote hosts Terminate a remote connection

To return temporarily to the local host and enter commands there, type a tilde and exclamation point (~!) at the remote system prompt. The local system prompt will display in the following form, where shell is the name of the local shell and pmt is the prompt for the local shell, either % for the C shell or $ for the Bourne or Korn shell:
~[ shell ] pmt!

1412 The UUCP Networking Commands

To return to the remote host, press Ctrl/d at the local system prompt. To terminate the tip process, type a tilde and press Ctrl/d (~^D). You can use the following tip commands from the remote host to perform tasks on the local host while you continue working on the remote host. These commands are preceded by a tilde:
~c directory_name ~t from [to] ~< ~p from [to] ~> change the local directory copy a le from the remote host to a le on the local host copy a le from the remote host to a le on the local host copy a le from the local host to a le on the remote host copy a le from the local host to a le on the remote host

For example, while connected by tip to remote host moon, you can change the current directory on local host earth, from /usr/geog/polar to /usr/geog/polar/arctic by entering the following command:
moon% ~c arctic

While connected by tip to remote host moon, you can copy the /usr/darkside/temp/dat le to the /usr/NASA/bios/temp le on local host earth by entering the following command:
moon% ~t /usr/darkside/temp/dat /usr/NASA/bios/temp

While connected by tip to remote host moon, you can copy the local /usr/NASA/bios/warn le to the /usr/darkside/temp/change le on the remote host by entering the following command:
moon% ~p /usr/NASA/bios/warn /usr/darkside/temp/change

Note You can only transfer ASCII les with the ~t and ~p commands. (For example, a PostScript le is an ASCII le, but a DECwrite le is not.) Neither ~t nor ~p checks for le transfer errors; the uucp command provides this verication.

14.3.2.4

Using tip to Connect a Local Computer to Several Remote Computers

You can enter the tip command to connect host X to host Y, log on to host Y, and then enter the tip command there (if Y supports tip) to connect to host Z. You then have one local host computer, X, and two remote host computers, Y and Z.

The UUCP Networking Commands 1413

You can run an operating system command on host Z (if Z is a Digital UNIX host) after you log in there. Then, from Z, you can run commands on the other hosts as follows: To run a command on host X, prex the command with a single tilde (~) To run a command on host Y, prex the command with two tildes (~~) Note A command sequence that begins with a tilde (~) can be interpreted by tip only if it is at the beginning of the command line. Table 14-4 summarizes the most common tip local commands. For information about other tip local commands, refer to tip(1). Table 14-4: Local tip Commands
Command ~ Ctrl/d Description Logs you off the remote computer and terminates the remote connection. When connected to the remote host over a telephone line using a modem, this does not always work. In such cases, press Ctrl/d to log off; then enter ~ Ctrl/d at the prompt and press the Return key to terminate the remote connection. ~. Logs you off the remote computer and terminates the remote connection. When connected to the remote host over a telephone line using a modem, this does not always work. In such cases, press Ctrl/d to log off; then enter ~. at the prompt and press the Return key to terminate the remote connection. ~! Returns the session from the remote host to a shell on the local host. Type a tilde and an exclamation point (~!) at the prompt to enter any command. To return to the remote host, press Ctrl/d. After establishing the tip connection, you can go back and forth between the two hosts by typing ~! (to go from remote to local) and press Ctrl/d (to go from local to remote).

1414 The UUCP Networking Commands

Table 14-4:
Command

(continued)
Description Changes the current directory on the local host to that specied by the directory_name variable. If no directory name is specied, tip changes it to your home directory. Copies a le from the remote to the local host. If you do not specify a dest destination le on the local host, the ~t command copies the remote le to the local host and assigns the same le name. Copies a le from the local to the remote host. If you do not specify a dest destination le on the remote host, the ~p command copies the local le to the remote host and assigns the same le name. Copies a le from the remote to the local host; the tip command prompts for the command string that will be used on the remote host to display the remote le, and the name of the local le, for example, cat filename. Copies a le from the local to the remote host; the tip command prompts for the name of the local le and sends the the le to the remote host as if it were standard input. The user should set up a command on the remote host to accept this input before executing the ~> command. For example, remote% cat > destination-file.

~c directory_name

~t source [dest]

~p source [dest]

~<

~>

Note The tip program uses system prompts and character sequences that match a systems interrupt sequence to signal the end of le transfers through the ~< or ~> command. These values are congured in the /etc/remote le. See the remote(4) reference page for more information.

14.3.3

Using ct to Connect to a Remote Terminal with a Modem


The ct command enables a user on a remote ASCII terminal with a modem to communicate with a local host with a modem over a telephone line. The remote terminal user can then log in and work on the local host. If there are no available telephone lines, the ct command displays a message and asks if you want to wait for one. The ct command is useful in the following situations:

The UUCP Networking Commands 1415

When secure communications are necessary: Because the local host contacts the remote terminal, the remote user does not need to know the telephone number of the local host. The local user entering ct can monitor the work of the remote user.

When the cost for the telephone connection should be charged either to the local site or to a specic account on the remote terminal (like a collect call). The -h option can be omitted to emulate making a collect call. The user on a remote terminal enters the ct command without the -h option.

The following ct features are useful under certain circumstances: You can instruct ct to continue dialing a number until the connection is established or until a set length of time has elapsed. You can specify more than one telephone number, and ct can dial each modem until a connection is established. Note Usually, a user on the remote terminal calls the user on the local host to request a ct session. If such connections occur often, your system manager may want to set up UUCP so that a local host automatically enters ct to one or more specied terminals at a designated time. For information about customizing UUCP, see your system administrator. For example, to connect to a remote terminal modem at the same site as yours, enter the following command. The remote modem has a telephone number of 7-6092:
earth% ct 76092 Allocated dialer at 1200 baud Confirm hang_up? (y to hang_up)

After entering the command, a message veries the connection and prompts you to either hang up any other phone lines currently in use or cancel the command.

1416 The UUCP Networking Commands

The following example shows how to use the ct command to connect to a remote terminal modem with a local telephone number of 555-0043, specifying 9 for an outside line and the -w option for a 2-minute wait for the modem line:
earth% ct -w2 9=5550043 Allocated dialer at 1200 baud Confirm hang_up? (y to hang_up)

As before, you are prompted to either hang up any other phone lines currently in use or cancel the command. The following example shows how to use the ct command to connect from local host earth, to a remote terminal modem with a long-distance number of 1-201-555-7824, specifying 9 for an outside line and the -w option for a 5-minute wait for the modem line:
earth% ct -w5 9=12015557824 Allocated dialer at 1200 baud Confirm hang_up? (y to hang_up)

You are prompted to either hang up any other phone lines currently in use or cancel the command. See the ct(1) reference page for more information. Table 14-5 summarizes ct command options and required entries. Table 14-5: Options to the ct Command
Option wminutes Description Species the maximum length of time in minutes that the ct command waits for a line. The ct command dials the remote modem at one-minute intervals until the connection is established or the specied number of minutes has passed. Using the -w option suppresses the messages that ct normally displays if it cannot make the connection. xnumber Produces detailed information about the commands execution on standard error output on the local host, to be used for debugging. The debugging level, number, is a single digit in the range from 0-9; the recommended default is 9. Enables the ct command to send a running narrative to standard error output. Prevents ct from breaking the current connection. Species the data transmission rate of ct; the default is 1200baud. Set the baud rate to the baud rate of the terminal to which you are connecting.

v h sspeed

The UUCP Networking Commands 1417

Table 14-5:
Option telno

(continued)

Description Species the phone number of the remote modem. You can enter a local or a long-distance number, and specify secondary dial tones such as 9 for an outside line, or an access code. Use an equal sign (=) following a secondary dial tone (9=), and a hyphen (-) for delays, as in 555-5092. Telephone numbers may contain up to 31 characters, including digits 0-9, and any of the following: a hyphen or dash (-), an equal sign (=), an asterisk (*), and a number or pound sign (#).

14.4 Using uux to Run Commands on Remote Hosts


The uux command enables you to run commands on a remote host while you work on your local host. If the command does not exist on the remote host, uux does not execute, and the remote host will notify you by mail. If the command executes and produces output (for example, cat or diff), you can specify that uux place that output in a le on any specied host. Note For security reasons, certain sites may restrict the use of some commands through uux. Also, enhanced security features on the local host may affect whether certain commands can be run on remote hosts through uux. See your system administrator for more information. The uux command syntax depends on how the command interpreter of a given shell treats special characters. The syntax is the same for the Bourne and Korn shells, but different for the C shell. Regardless of the shell from which you use uux, there are two ways to specify the destination:
uux [ option... ] " commandstring > destination" uux [ option... ] commandstring \{ destination\ }

In the rst syntax statement, the right-angle bracket (the redirection symbol) (>) directs the output of the remote command to a destination directory or le. A pair of double quotation marks (" ") encloses the entire command because the redirection symbol, the right-angle bracket (>), is a special character. Whenever you use any of the following characters in a command

1418 The UUCP Networking Commands

line, you must enclose that character or the entire command in double quotation (" ") marks: left-angle bracket (<) right-angle bracket (>) semi-colon (;) vertical bar or pipe (|) plus sign (+) left-bracket ([) right-bracket (]) question-mark (?)

In the second syntax statement, enclose the destination name within braces ({ }). You must type a backslash (\) before each brace because braces are special characters to the shell command interpreter. Without backslashes, the braces would be misinterpreted. When specifying the pathname of a destination le, you can use a full name, or a pathname preceded by ~user/ where user is the name of the users login directory. Output les must be writable. If you are uncertain about the permission status of a specic target output le, direct the results of the command to the /usr/spool/uucppublic directory; ~uucp is a brief way of specifying this directory from a shell.

14.4.1

Using uux from the Bourne or Korn Shells


The following example shows how the uux command uses the operating system cat command to concatenate the /u/doc/F1 le located on host gem, with the /usr/doc/F2 le located on host sky. The result is placed in the /u/doc/F3 le on host gem.
uux "gem!cat gem!/u/doc/F1 sky!/usr/doc/F2 > gem!/u/doc/F3"

In the following example, the task is the same as in the previous command, but braces ({ }) are used instead of the redirection symbol to specify the destination in the uux command line. The task is the same as in the previous command, but the destination output is implicit:
uux gem!cat gem!/u/doc/F1 sky!/usr/doc/F2 \{gem!/u/doc/F3\}

The UUCP Networking Commands 1419

14.4.2

Using uux from the C Shell


To perform the same operation as in the previous section, but in the C shell, enter one of the following uux commands:
uux "gem\!cat gem\!/u/doc/F1 sky\!/usr/doc/F2 > gem\!/u/doc/F3"

The following example uses an implicit destination output le:


uux gem\!cat gem\!/u/doc/F1 sky\!/usr/doc/F2 \{gem\!/u/doc/F3\}

In the two following examples, uux uses the cat command to send the acct6 le from remote host boston, as output to the acct6 le in the public directory on your local host:
uux "cat boston\!/reports/acct6 > ~uucp/acct6"

The following example uses an implicit destination output le:


uux cat boston\!/reports/acct6 \{~uucp/acct6\}

14.4.3

Other uux Features and Suggestions


The uux command assumes your local host is the default, so you do not need to specify it in the command line. For example, to run the diff command to compare the /u/F1 le on host car with the /u/F2 le on host sea, and place the result in the /u/F3 le on the local host, use the following command:
uux "diff car!/u/F1 sea!/u/F2 > /u/F3"

You can also represent the local host by using just an exclamation point, as in the following example:
uux "!diff car!/u/F1 sea!/u/F2 > !/u/F3"

When you specify the pathname source le in commands such as diff or cat, you can include the following shell pattern-matching characters which the remote host will interpret: the question-mark (?), the asterisk (*), the left-bracket ([), and the right bracket (]). Enclose these characters either within two backslashes (\ ... \) or within quotation marks (" ... ") so that the local shell does not interpret the characters before uux sends the command to the remote host. Do not use pattern-matching characters in destination names. If you use the left-angle bracket (<), the right angle-bracket (>), the semicolon (;), and the pipe (|) shell characters, place them within backslashes (\ ... \), quotation marks (" ... "), or place the entire command line within backslashes or quotation marks.

1420 The UUCP Networking Commands

Note The shell redirection characters, two left-angle brackets (<<) and two right-angle brackets (>>), do not work in UUCP. Table 14-6 summarizes uux command options and required entries. See the uux(1) reference page for more information. Table 14-6: Options to the uux Command
Option n Description Cancels notication through mailx that usually occurs when a command fails to execute on the designated host. The -n and -z options are mutually exclusive. z Sends a message through mailx when command execution fails on the designated host. The -n and -z options are mutually exclusive. j Displays the job identication number of the uux request that runs the remote command; use this number with the uustat command. See Section 13.8.1 for more information. Species any command accepted by the designated host. For more information on the command formats, see Section 13.4. Species the host and le for storing the output of the command run on a remote host. For example, if you want to list all the les in a directory on a remote host, you can use uux to place the listing in a le on your own host by entering the appropriate destination name. For more information on the destination formats, see Section 13.4.

commandstring

destination_name

14.5 Using UUCP To Send and Receive Files


On UNIX based computers (such as Digital UNIX that support the UUCP protocol), you can use the uucp command to copy one or more les from one computer to another. You can use uucp to copy les as follows: Between local and remote hosts Between two remote hosts Between two hosts through an intermediate host

The UUCP Networking Commands 1421

Within your local host

To facilitate le transfers, many sites make the public UUCP directory, usr/spool/uucppublic, available. This directory provides read and write access to all users and bypasses security restrictions. The brief way to specify this directory is ~uucp or ~/ in a uucp command. Note File transfer through uucp is subject to security features on either host. The uucp utility does not display error messages for failed le transfers. For more information, see your system administrator. The system administrator denes security restrictions to prevent unwarranted use by remote users, so only certain directories and les may be accessible for sending or receiving les.

14.5.1

Using UUCP to Copy Files in the Bourne and Korn Shells


From the Bourne or Korn shells, you can specify uucp le names without using a backslash (\) before the exclamation point (!) that precedes the host name of the destination le. For example, to copy the star le from local host earth to the /sun/stats le in the public directory on the remote host sky, enter the following command:
earth% uucp star sky!~/sun/stats

To copy the same le and explicitly identify the /usr/spool/uucppublic directory, enter the following command:
earth% uucp star sky!/usr/spool/uucppublic/sun/stats

If you need to copy a le to a remote host whose address is unknown to your local host, you can do so via another host that knows the remote hosts address. You can copy a local le to a remote host by rst sending it to one or more intermediate hosts, separating each host name by an exclamation point (!). For example, to copy the local le star to the /sun/stats le on the remote host sky by rst sending it through the intermediate host, mlkway, enter the following command:
earth% uucp star mlkway!sky!~uucp/sun/stats

1422 The UUCP Networking Commands

You can use uucp from your local host to copy a le from a remote host to your local host. For example, to copy the /cells/type1 le from remote host biochem to the local le, /dna/sequence, enter the following command from local host earth:
earth% uucp biochem!/cells/type1 /dna/sequence

You can copy multiple les from a remote host to a local host by using a pattern-matching character to specify les. For example, to copy all les with names beginning with report from the /geog/survey directory on remote host moon to the ~uucp public directory on local host earth, enter the following command:
earth% uucp moon/geog/survey/report* ~uucp

14.5.2

Using UUCP to Copy Files in the C Shell


In the C shell, the exclamation point (!) has a special meaning. To prevent the command interpreter from mistranslating it, you must precede it with a backslash (\) in a pathname. For example, to copy the /usr/NASA/ctrl-specs le from local host earth to the ~uucp public directory on remote host luna7, enter the following command from the local host:
earth% uucp /usr/NASA/ctrl-specs luna7\!~uucp

To copy the plan9 le from the /usr/reports/exobio directory on remote host luna7 to the ~uucp public directory on local host earth, enter the following command:
earth% uucp luna7\!/usr/reports/exobio/plan9 ~uucp

To copy all les with names beginning with msg from the /sensory/visual/earthrise directory on the remote host luna7 to the ~uucp public directory on local host earth, you can enter the following command:
earth% uucp luna7\!/sensory/visual/earthrise/msg* ~uucp

Here, the pattern-matching character, the asterisk (*) in the source le names is enclosed within single quotation marks to prevent misinterpretation.

The UUCP Networking Commands 1423

In the next example, the same les are copied to ~uucp, but the entire pathname of the source les is enclosed in double quotation marks to prevent misinterpretation:
earth% uucp "luna7\!/sensory/visual/earthrise/msg*" ~uucp

Table 14-7 summarizes uucp command options and required entries. See the uucp(1) reference page for more information. Table 14-7: Options to the UUCP Command
Option d Description Creates intermediate directories needed when copying a source le to a destination le on a remote host. Entering uucp with the destination pathname creates the required directory. The -d option is the default. Do not create intermediate directories during the le transfer. Displays the job identication number of the transfer operation; use with the uustat command to check transfer status, or with uustat -k to terminate the transfer. See Section 13.8.1 for more information. Species that uucp send mail to the requester to verify copying of destination le on a remote host; no mail is sent for a local transfer. Noties the recipient, username on the remote host, that a le has been sent; no mail is sent for a local transfer. Species the pathname of the le that you want to send or receive. For more information about UUCP pathnames, see Section 13.1. Species the pathname of the le (or directory) that receives the copy. For more information about destination le pathnames, see Section 13.5.1 and Section 13.5.2.

f j

nusername source_le

destination_name

14.6 Using uuto with uupick to Copy Files


The uuto command copies the le you specify to the public directory on the destination host where it is obtained by the recipient through uupick. The rmail program noties the recipient when the le arrives.

1424 The UUCP Networking Commands

Note Any le transfer is subject to the security features on the local and remote hosts. See your system administrator for more information. For example, to send the /usr/bin/data/junk le from local host moe to user curly on remote host stooge, enter the following command:
moe% uuto /usr/bin/data/junk stooge!curly

The uuto command copies the le to the /usr/spool/uucppublic/receive/curly/moe le on host stooge. Next, the rmail utility sends user curly a mail message stating that the le has arrived. User curly can then enter the uupick command to access the le and save, move, or delete it. In the following example, user curly enters the uupick command on host stooge; the response from uupick follows:
stooge% uupick from system moe: file junk ?

At the uupick question mark (?) prompt, user curly enters the d and q options to delete the le and exit from uupick:
? d ? q

Table 14-8 summarizes the uupick le handling options, entered at the ? uupick prompt. Table 14-8: Options to the uupick Command
Option * Return a [dir] Description Displays available uupick le-handling options. Pressing the Return key signals uupick to get the next le. Moves all uuto les from the public directory to the specied directory on the local host; specify the directory by using a full or relative pathname. The default is the directory where you enter uupick. The d option deletes the current le obtained by uupick. Moves a le to a directory specied by either full or relative pathname; the default is the current directory. Displays the le on your workstation.

d m [dir] p

The UUCP Networking Commands 1425

Table 14-8:
Option q or Ctrl/d

(continued)
Description The q (quit) option enables you to exit from uupick without displaying, moving, or deleting any le in the public directory. You can also press Ctrl/d to quit. Enables you to leave uupick and return to the operating system prompt where you can run a command. After the command executes, control returns to uupick.

!command

See the uupick(1) reference page for more information.

14.7 Using uuto to Send a File Locally


You can also use uuto to send a le to another user on your local host. However, the recipient does not receive a mail message indicating the le transfer. For example, user shemp can send the le /usr/bin/data/status to user larry on local host stooge, where each is logged in:
stooge% uuto /usr/bin/data/status larry

Table 14-9 summarizes uuto command options and required entries. See the uuto(1) reference page for more information. Table 14-9: Options to the uuto Command
Option m p Description Noties sender when uuto copies a source le to the specied user name and host. Usually, uuto copies the source le to: /usr/spool/uucppublic/receive /username/host/file. The -p option sends the source le to the spool directory on the local host before transferring a copy of it to the public directory on the specied host. le_name The pathname of the source le.

1426 The UUCP Networking Commands

Table 14-9:
Option destination_name

(continued)
Description The pathname to the location where you want to copy the source le. The destination_name must include the user name of the person receiving the le, and has the form, host!username, where host is the name of the remote computer and username is the user name of the recipient. When copying a le on your local host, the destination_name can be simply the name of the user to whom you are sending the le.

14.8 Displaying Job Status of UUCP Utilities


The UUCP utilities include three commands, uustat, uulog, and uumonitor for displaying status information about UUCP jobs, as described in the following sections.

14.8.1

The uustat Command


The uustat command supports UUCP jobs by providing the following: Status information of le transfers requested by uucp and uuto Status information of command executions requested by uux Limited control of jobs queued to run on a remote computer Cancellation of copy requests from uucp

Status reports from uustat display on your workstation screen in this basic form; variations depend on the uustat option.
jobid date/time status system_name username size file

Note Any status display operation is subject to the security features on the local and remote hosts. See your system administrator for more information. Entering uustat without options displays the status information for all the UUCP commands that you have entered since the last time the holding queue was cleaned up.

The UUCP Networking Commands 1427

To report the status of jobs requested by a specic user, use the -u option, as shown here, for user hugh:
% uustat -u hugh

Two types of output information, each produced by a uustat option, are the current queue and the holding queue: The output of the uustat -q command is the current queue, which lists the UUCP jobs either queued to run or currently executing on one or more remote hosts. The output of the uustat -a command is the holding queue, which lists all jobs that have not executed during a set period of time. Note After the set time period has elapsed, delete the entries in the holding queue manually with the uucleanup command or automatically through the uudemon.cleanu script. The uudemon.cleanu script has an entry in /usr/spool/cron/crontabs/uucp which is activated by the /etc/cron daemon. For more information about cleaning up UUCP queues, see the uucleanup(8) reference page or your system administrator.

14.8.1.1

Displaying the Holding Queue Output with a uustat Option

To examine the status of all UUCP jobs in the holding queue, enter the uustat -a command as shown here with example output:
% uustat -a sunC3113 Thu gemN3130 Thu seaC3120 Wed seaC3119 Wed Jun Jun Jun Jun 04 04 03 03 17:47:25 09:14:30 16:02:33 12:32:01 1996 1996 1996 1996 S R S S sun gem sea sea doc geo doc msg 289 D.car471afd8 338 D.car471bc0a 828 /u/doc/tt rmail doc

This example output consists of the following seven elds: Field 1 job ID of the operation; if you need to cancel a process that is still on the local computer, you would use this eld as input to the uustat command with the -k option, for example,
% uustat -k seaC3119

Field 2 date and time that the UUCP command was entered Field 3 S or an R, depending on whether the job sends or receives a le Field 4 name of the hosts where the command was entered Field 5 username of the person who entered the command

1428 The UUCP Networking Commands

Field 6 le size or, in the case of remote execution (as in the last output line), the name of the remote command (rmail). Field 7 when the size is given in eld 6, (as in the rst three output lines) the le name is displayed in this eld The le name can be either the name given by the user, such as /u/doc/tt or a name that UUCP assigns internally to data les associated with remote executions, such as D.car471afd8.

To report the status of all UUCP jobs in the holding queue requested by a specic host, enter the uustat -s command as shown here with example output, for host sky:
% uustat -s sky skyNlbd7 Wed Jun 03 12:09:30 1996 skyClbd8 Wed Jun 03 12:10:15 1996 skyC3119 Wed Jun 03 12:11:18 1996 S S S sky sky sky doc doc doc 522 59 rmail /user/doc/A D.3b2a12ce4924 msg

This output is the same as the output produced by the command uustat -a -s sky. 14.8.1.2 Displaying the Current Queue Output with uustat Options

To examine the status of all UUCP jobs currently executing or queued to run on each host (the current queue) enter the uustat -q command as shown here with example output:
% uustat -q sea 3C sun 2C gem 1C (2) Mon Jul 13 09:14:35 1996 Mon Jul 13 10:02:22 1996 Mon Jul 13 10:12:48 1996 NO DEVICES AVAILABLE SUCCESSFUL CANT ACCESS DEVICE

This example output consists of the following ve elds: Field 1 host name Field 2 number of les, either command (C), or executable (X), in the holding queue for that host Field 3 number of days (if one or more) that the le has been in the holding queue Field 4 date and time when UUCP last tried to communicate with the host in eld 1. Field 5 status message of the interaction

See the uustat(1) reference page for more information. Table 14-10 summarizes uustat command options and required entries.

The UUCP Networking Commands 1429

Table 14-10: Options to the uustat Command


Option a Description Displays information for all jobs in the holding queue, regardless of the user who entered the original UUCP command. Cancels the UUCP process specied by jobid. You can cancel a job only if you entered the UUCP command specied by jobid. Anyone with superuser privileges can also cancel UUCP requests. Reports on the status of your most recent attempt to communicate with another computer through UUCP. For example, the status is reported as successful if the UUCP request executed. If the job was not completed, UUCP reports an error message, such as Login failed. p Useful only to someone with superuser privileges, it runs a ps -flp (process status: a full, long list of specied process IDs) for all PID numbers in the lock les. Lists the jobs currently queued for each computer. These jobs are either waiting to execute, or in the process of executing. If a status le exists for the computer, UUCP reports its date, time, and the status information. Once the process is completed, UUCP removes the job listing from the current queue. Rejuvenates the UUCP process specied by the job identication number. This option enables you to mark les in the holding queue with the current date and time, to ensure that the cleanup operation does not delete these les until the allotted job modication time ends. Reports the status of all UUCP requests that users have entered to run on the computer specied by the host entry. Reports the status of all UUCP requests entered to run that were specied by the user named in the user entry. You can use both the -shost and the -uusername options with the uustat command to get a status report on all UUCP requests entered by a specied user and host.

k jobid

r jobid

shost uusername

1430 The UUCP Networking Commands

14.8.2

Using the uulog Command to Display UUCP Log Files


Whenever the local host uses the uucp, uuto, or uux commands, UUCP log les are created for each remote host. The uulog command displays these log les. Use uulog to display a summary of uucp, uuto, and uux command requests by user or by host. The uulog command displays the contents of the log le activity of either of the following daemons: The uucico daemon, called by uucp and uuto The activity of this daemon is logged in /usr/spool/uucp/.Log/uucico/host. The uuxqt daemon, called by uux The activity of this daemon is logged in /usr/spool/uucp/.Log/uuxqt/host. To display just the uuxqt log le, use the -x option of uulog, as follows:
% uulog -x

The uulog command also enables you to display the uucico log le or the le transfer log for any host, or only a specied number of lines at the end of either log le. For example, to display the uucico log le for host sky, use the -s option as follows:
% uulog -s sky

To display the last 40 lines of the le transfer log for host sky, use the -f option and the number option as shown:
% uulog -f sky -40

Table 14-11 summarizes uulog command options and required entries. Table 14-11: Options to the uulog Command
Option fhost Description Performs a tail -f on the le transfer log for the specied host, displaying the end of the log le. Use the Interrupt key sequence to leave the le and return to the prompt. Prints information about copy requests involving the specied host. If no host is specied, information is displayed for all hosts. Host names can contain only ASCII characters. Displays the uuxqt log le.

s[host]

The UUCP Networking Commands 1431

Table 14-11:
Option number

(continued)

Description Displays the last lines of the log le. The number of lines is determined by number.

14.8.3

Monitoring UUCP Status


The uumonitor command is helpful for detecting a host whose status has changed due to a backlog of jobs, a temporary shutdown, or a change of either the phone number or log-in password. The uumonitor output consists of the following six elds: Field 1 host name Field 2 number of command les queued for the remote host; if too large (for example, 100-1000, depending on the host), then the cause of the backlog should be determined Field 3 number of requests for remote execution from the remote host Field 4 result of the most recent attempt to connect to the remote host Field 5 number of remote host login failures, not including failed dial attempts; if greater than 20, no further attempts are made Field 6 time of last status entry

For more information, see the uumonitor(8) reference page.

1432 The UUCP Networking Commands

A Beginners Guide to Using vi

Whether you are writing memos or modifying C programs, editing text les is one of the most common uses of any computer system. The vi text editor (hereafter known as vi) is particularly well-suited for the day-to-day text editing tasks of most computer users. You can quickly and easily open a le, edit it, and save the results using vi. The vi text editor is a full-featured text editor with the following major features: Fast processing, especially on startup and global operations Full screen editing and scrolling capability Separate text entry and edit modes Global substitution and complex editing commands using the underlying ex commands Access to operating system level commands Ability to customize system parameters and keyboard mappings

This appendix shows you how to use the basic features of vi. After completing the exercises in this appendix, you will be able to: Create and save a new le Access (open) an existing le Move the cursor within the le Enter new text Change existing text Search for strings Move and copy text Make global substitutions Write all or part of the text to a le Delete, move, or copy blocks of text Customize your editing environment

This appendix only provides an introduction to the features of vi If you want to learn more, see the vi(1) reference page. You may also read one of the many books on the market that describe vis advanced features. This appendix is divided into three sections. The rst section gets you started with vi. The second section shows you some advanced techniques for speeding up your work. The third section shows you how to take advantage of the power of the underlying ex commands.

A.1 Getting Started


This section shows you how to create a le with vi, save the le, move the cursor around in the le, add text, delete text, and modify text.

A.1.1

Creating a File
To create the le, my.file, that will be used in the examples throughout this appendix, enter the vi command as shown below:
$ vi my.file Return

Your screen will look like this:


~ ~ ~ ~ ~ ~ ~ ~ ~ "my.file" [New file]

The lines beginning with tildes (~) represent the blank lines in the le. Because my.file is empty, all lines in the le begin with a tilde (~). The vi editor has two modes: command mode and input mode. Command mode is the mode vi is in when it is started. In command mode, the characters you enter are interpreted as commands for manipulating the text. When vi is in input mode, the characters you enter are interpreted as text. When you create a new le with the vi command, the vi editor is in the command mode. That is, vi is waiting for you to enter a command. However, at this point you want vi to be in the input mode so you can insert text into my.file, which is empty.

A2 A Beginners Guide to Using vi

Put vi into the input mode by typing:


i

The i command will not be displayed on the screen. The vi editor is now in the input mode and vi will interpret all characters that you type to be text. In the sample text below, notice the use of the Escape key on the last line of input and the use of the :wq command to save the le and exit the vi editor. Type the sample text exactly as shown. If you make a mistake, use the Backspace key to correct it; press the Return key where indicated to move to the next line of text:
You can use this text file Return to experiment with vi. Return The examples shown here Return will teach you the basics of vi. Escape ~ ~ ~ ~ ~ :wq "my.file" 4 lines, 108 characters $

Note Depending upon how your terminal or workstation is set up, the Escape key may be programmed to perform a different function. It is possible that one of the function keys on your keyboard (possibly F11) may have been set up to perform the escape function. See your system administrator if your Escape key does not operate properly. Pressing the Escape key while vi is in the input mode puts vi back into the command mode; once in the command mode vi interprets anything you type to be a command. The :wq command writes (saves) the le with the name my.file into your current directory and quits the vi editor. The format of the :wq command is much different than other vi commands because :wq is not a vi command; it is an ex command. When you press a colon ( :) when vi is in the command mode, notice that it appears at the bottom of the screen. The colon ( :) begins all ex commands from within vi. All ex commands are executed when vi is in the command mode. You must press the Return key after the command to signify to ex that you are nished entering the command. See Section A.3 to learn more about ex commands.

A Beginners Guide to Using vi A3

If you lose track of which mode vi is in, press the Escape key a few times to make sure vi is in the command mode. If your system is so congured, you will hear a bell when you press the Escape key that signals that vi is indeed in the command mode. The Escape key and its use in vi and exiting vi using several different methods are described in more detail later in this appendix. The text you just entered in my.file will be used in the remaining examples in this appendix.

A.1.2

Opening an Existing File


Whether you are creating a new le or opening an existing le, the syntax for using vi is the same:
vi lename

To open the my.file le, enter the vi command as follows:


$ vi my.file

Your screen should look like this:


You can use this text file to experiment with vi. The examples shown here will teach you the basics of vi. ~ ~ ~ ~ ~ ~ "my.file" 4 lines, 108 characters

The text you entered in the le will be displayed at the top of the screen. The lines beginning with tildes (~) represent the blank lines in your le. The text at the bottom of the screen shows the name of the le, the number of lines in the le, and the number of characters in the le.

A.1.3

Saving a File and Quitting vi


In the previous example, you learned that the :wq command saved the le and quit the vi editor. However, there are several other options available to save and quit a le: Save a le and continue working in it

A4 A Beginners Guide to Using vi

Save the le and quit (exit) vi Quit vi without saving the changes made to the le

If you are working on a large text le and have been adding, changing, and deleting a lot of information, it is suggested to save the le often (perhaps every 10 minutes) to protect against potential data loss. The write command is used to save an entire le to the current directory. The format of the write command is:
:w lename

The entry of filename is optional and is used only when you want to save a le under a different lename. Omitting filename from the command automatically saves a le to its current le name. When you enter the :w command, the current le name, number of lines, and number of characters is displayed at the bottom of your screen. If you entered a new le name, the new le name will be displayed. Note If you specify a new le name with the :w command, you will have two les saved in your directory: the new le name you just entered and the original le name. If you are nished making changes to a le, you can save the le and quit vi at the same time. The format of the write and quit command is:
:wq

The :wq command saves a le to the same le name, quits vi, and brings you back to your shell prompt. You also have the option to quit a le and vi simultaneously without saving the changes you may have made. This option is useful if, for example, you have deleted many lines of information by mistake and you want to start all over again. Quitting vi will restore your le to its original state. However, quitting vi to restore a le to its original state will only work if you have not saved the le previously during the current editing session. To quit your le and vi without saving your changes enter:
:q!

Quitting a le with the :q! command will not delete the le from your directory. Your le will still reside in the directory, but it will not contain any of the changes you may have made. Table A-1 summarizes the commands used to save les and quit the vi editor.

A Beginners Guide to Using vi A5

Table A-1: Write and Quit Command Summary


Command :w :w lename Result Saves the entire le to the current le name; does not exit the vi editor. Saves the entire le to the new le name; does not exit the vi editor. The new le name and original le name reside in the directory. Saves the entire le to the current le name and exits the vi editor simultaneously. Quits the le; exits the vi editor; does not save any changes made to the le since the last time the le was saved.

:wq :q!

A.1.4

Moving Within a File


If you have closed my.file, reopen it by using the command:
$ vi my.file

The cursor should be on the rst character in the le: the Y in the word You. As mentioned previously in this appendix, vi is in command mode at start up. In command mode, the characters you enter are treated as commands rather than as text input to the le. A.1.4.1 Moving the Cursor Up, Down, Left, and Right Certain keys on the keyboard have been designated to be movement keys when vi is in the command mode. The following letters on the keyboard control cursor movement: h (move the cursor one character to the right) j (move the cursor down one line staying in the same position) k (move the cursor up one line staying in the same position) l (move the cursor one character to the left)

A6 A Beginners Guide to Using vi

Using the movement keys, move the cursor to the rst letter of the word experiment by typing:
lllj

If your keyboard is equipped with arrow keys, you may be able to use the arrow keys to move left, right, up, or down as well. However, using the h, j, k, and l keys allows you to keep your ngers on the main section of the keyboard for faster typing. On some keyboards, the h, j, k, and l keys are repetitive keys. That is, holding the key down will repeat the key action until you release the key. For instance, holding down the j key will rapidly scroll through the lines in a le. In the command mode, the Return key acts as a cursor movement key. Pressing the Return key moves the cursor to the rst character of the next line. This movement differs from the j movement key because the Return key positions the cursor at the rst character of the next line whereas the j moves the cursor to the same character position on the next line. In the command mode, the hyphen () moves the cursor to the rst character of the previous line. This feature is useful to move backward through les. This movement differs from the k movement key because the hyphen () positions the cursor at the rst character of the previous line whereas the k moves the cursor to the same character position on the previous line. If you tested any of the cursor movement keys described above, make sure your cursor is positioned at the rst letter of the word experiment before continuing to the next section. A.1.4.2 Moving the Cursor by Word, Line, Sentence, and Paragraph You can use the w command to move the cursor by whole word boundaries. The w command moves the cursor forward to the beginning of the next word. Move the cursor to the beginning of the word with by typing:
w

You can use the b command to move backward to the beginning of the previous word. For example, move to the beginning of the word experiment by typing:
b

A Beginners Guide to Using vi A7

Now see what happens when you do not use the b command from the beginning of a word by typing:
llllb

The cursor returns to the beginning of the word experiment. The word motion commands will wrap to the next or previous text line when appropriate. Move the cursor to the beginning of the word text by typing:
bbb

Notice how the cursor moved backward and wrapped around to the previous line. There are a few other interesting movement commands you should know about. The zero (0) moves the cursor to the beginning of the current line, and the dollar sign, ($) moves the cursor to the end of the current line. The close parenthesis [)] moves the cursor to the beginning of the next sentence, and the open parenthesis [(] moves the cursor to the beginning of the previous sentence. The right brace (}) moves the cursor to the beginning of the next paragraph, and the left brace ({) moves the cursor to the beginning of the previous paragraph. A.1.4.3 Moving and Scrolling the Cursor Forward and Backward Through a File In larger les, you can move the cursor by whole screenfuls by pressing certain control keys: Ctrl/F moves the cursor one full screen forward Ctrl/B moves the cursor one full screen backward Ctrl/D moves the cursor and scrolls down (forward) a half screen Ctrl/U moves the cursor and scrolls up (backward) a half screen

The following uppercase letters also designate cursor movement over large boundaries of text: The H command moves the cursor Home; that is, to the rst character in the le The G command instructs the cursor to Go to the last line in the le

A8 A Beginners Guide to Using vi

A.1.4.4

Movement Command Summary The vi text editor has many more cursor movement commands. When you have learned the basics documented in this appendix, refer to the vi(1) reference page for more information. Table A-2 summarizes the cursor movement commands. The cursor movement keys are in effect only when vi is in the command mode. Table A-2: Cursor Movement Command Summary
Command h j k l Return key w b 0 $ ) ( } { Ctrl/D Ctrl/F Ctrl/B Ctrl/U H G Result Move the cursor one character to the right. Move the cursor down one line in the same position. Move the cursor up one line in the same position. Move the cursor one character to the left. Move the cursor to the beginning of the next line. Move the cursor to the beginning of the previous line. Move the cursor forward to the beginning of the next word. Move the cursor backward to the beginning of the previous word. Move the cursor to the beginning of the current line. Move the cursor to the end of the current line. Move the cursor to the beginning of the next sentence. Move the cursor to the beginning of the previous sentence. Move the cursor to the beginning of the next paragraph. Move the cursor to the beginning of the previous paragraph. Scroll down (forward) a half screen. Move the cursor forward one screen. Move the cursor backward one screen. Scroll up (backward) a half screen. Move the cursor home (to the rst character in the le). Move the cursor to the last line of the le.

A Beginners Guide to Using vi A9

A.1.5

Entering New Text


To enter new text into a le, vi must be in the input mode. In input mode, the characters you enter are inserted as text directly into the le. Remember that when vi is in the input mode, you can return vi to the command mode by pressing the Escape key once. There are several different commands used to insert text, and all of the commands that are used to insert text automatically place vi in the input mode as soon as the command is typed. To begin this exercise, open my.file and make sure the cursor is positioned at the word text in the rst line of the le. As you did initially to insert text into my.file, you will use the insert command to insert the word new just before the word text. With the cursor positioned on the rst t in the word text, put vi into the input mode command by typing the insert command:
i

Next, enter the word new and press the space bar once:
new Space

Exit the input mode by pressing the Escape key:


Escape

The cursor should now be positioned on the space between the words new and text. The i command starts inserting text at the character just before the cursor. Thats why you have to remember to press the Space bar to insert a space between words if the cursor was positioned at the rst character in a word when you started to insert text. Another command that is used to insert text is the append ( a) command. In contrast to the insert command, the a command adds (or appends) the characters you type just after the cursor position. To see how the a command works, use the cursor movement keys to move to the letter u in the word You, and type:
a , too, Escape

The vi text editor appended the text you typed to the end of the word You. The cursor should now be positioned on the second comma.

A10 A Beginners Guide to Using vi

The o command opens a new line below the line with the cursor and allows you to insert text at the start of that new line. To add a sentence to the end of this le, move the cursor to the last line of the le by pressing the Return key three times:
Return Return Return

The cursor should be positioned at the word will. To open a new line below the current line and automatically put vi into the input mode, type:
o

Enter the sample text shown below (including pressing the Return key where indicated), and press the Escape key to return to command mode when you are nished.
New text can be easily entered Return while in input mode. Escape

Your screen should now look like this:


You, too, can use this new text file to experiment with vi. The examples shown here will teach you the basics of vi. New text can be easily entered while in input mode. ~ ~ ~ ~ ~ ~

The O command opens a new line above the current line and starts inserting text at the start of the new line. This command is most useful for adding new text to the top of an existing le, but can be used anywhere in a le. To practice using this command to open a line and insert text, move the cursor to the rst line in the le (using the cursor movement command H perhaps) and type:
O Opening a new line is easy. Escape

The vi text editor is back in the command mode once the Escape key is pressed. There are two other commands that put vi in the input mode: the I and A commands. The I command inserts text at the beginning of the current line. The A command appends text after the last character at the end of the current line.

A Beginners Guide to Using vi A11

Practice inserting text to the beginning of a line, by typing:


I Inserting text is easy. Space
Escape

Practice appending text to the end of a line by typing:


A Really! Escape

Your screen should now look like this:


Inserting a line is easy. Opening a new line is easy. Really! You, too, can use this new text file to experiment with vi. The examples shown here will teach you the basics of vi. New text can be easily entered while in input mode. ~ ~ ~ ~ ~ ~

Table A-3 summarizes the commands used to insert and append text to a le. These commands are executed from the command mode and automatically put vi into the input mode. Table A-3: Text Insertion Command Summary
Command i a I A o O Result Inserts text immediately before the current cursor position. Appends text immediately after the current cursor position. Inserts text at the beginning of the current line. Appends text to the end of the current line. Opens a new line directly below the current line. Opens a new line directly above the current line.

A12 A Beginners Guide to Using vi

A.1.6

Editing Text
Up to this point you have only learned how to add new text to the le, but what if you need to change some text? The vi text editor provides commands for deleting and changing text. For example, to remove the word easily, from the sixth line in my.file, move the cursor to the rst character of the word and enter:
dw

This command is a combination of the delete command d, and the motion command w. In fact, many vi commands can be combined with motion commands to specify the duration of the action. The general form of a vi command follows:
[ number] [ command] motion

The command entry represents an action command, motion represents a motion command, and number optionally represents the number of times to perform the command. You also can use this general form to move the cursor in larger steps. To illustrate this concept, move the cursor to the beginning of my.file by typing H. Now, to move the cursor forward four words, enter:
4w

The cursor has moved four entire words and is positioned at the rst letter of the fth word, easy. A.1.6.1 Deleting Words Using the general form of commands, you can delete the last ve words of this text le. Move the cursor to the beginning of the last line by pressing the Return key several times and enter:
5dw :w

It takes ve words to delete the whole line rather than four because the period at the end of the line counts as a word. All punctuation counts as one word when youre using the delete word command. As a reminder that you should save a le often, this example also had you write the le (save it) using the :w command. Suppose you only want to delete a portion of a word? The x command deletes one character at a time. To see how this command works, move the cursor to the letter s in the word examples. Press the x key once to delete the letter s.

A Beginners Guide to Using vi A13

A.1.6.2

Deleting Lines The dd command is a shortcut for deleting whole lines at a time. The dd command can be used with a number to delete multiple lines as well. For example, position the cursor at the sixth line in the le (at the line beginning with the word New) and type:
2dd

The sixth and seventh lines (even though the seventh line is empty) of the le are deleted simultaneously. The dd command can be used without specifying a number to delete one line at a time. The D command clears the current line of text from the current cursor position to the end of the line but does not delete the line itself. If the cursor is positioned at the beginning of the line, the entire line is cleared. This command speeds up your work because you dont have to know how many words are in the line to be able to delete them (as you would, for example, if you were using the dw command). This command is useful if you want to rewrite an entire line. With the cursor positioned at the beginning of the line, the D command followed by one of the text insertion commands ( i, I, a, or A) allows you to clear the current line of text and reenter new text with a minimum of keystrokes. A.1.6.3 Changing Text The command for changing text, c, can be used to combine the actions of deleting and returning to input mode. It follows the same general form as the d command. To change the text new text to almost new demo, move the cursor to the rst character in the word "new", and enter the command:
2cw

The text will not immediately disappear. Instead, a dollar sign ($) is placed at the end of the change range (the last t in text), and vi is placed in input mode automatically. The text you enter will overwrite the existing text up to the dollar sign and then extend the text range as needed. Enter the new text by typing:
almost new demo Escape

A.1.6.4

Text Editing Command Summary As shown in the previous sections, the text editing commands can be used together with the motion commands to give you more editing power. The text editing commands can be combined with a number to change or delete large blocks of words or lines simultaneously. Table A-4 summarizes the commands used to edit text.

A14 A Beginners Guide to Using vi

Table A-4: Text Editing Command Summary


Command cw Result Changes the current word to the new text you type. You may change the word with as much next text as necessary. The Escape key signals the end of the change. Changes n number of words to the new text you type. The new text is not limited to just n words. You may change n words with as much new text as necessary. The Escape key signals the end of the change. Clears the text from the current cursor position to the end of the line. Does not delete the space used by the line thereby allowing you to add more text. Deletes the current line. Deletes n number of lines beginning with the current line. Deletes the current word. Deletes n number of words beginning with the current word. Deletes the current character.

ncw

dd

ndd
dw

ndw
x

A.1.7

Undoing a Command
If you make a change and then realize it was in error, you may still be able to correct it if you havent executed another command. The u command undoes the last command entered. Undo the last command, 2cw, by typing:
u

The text string almost new demo will be changed back to new text if you didnt execute any other commands since you executed the 2cw command. The uppercase U command undoes all changes to the current line and restores the line to its original state. The U command works only if you have not moved the cursor to another line.

A Beginners Guide to Using vi A15

A.1.8

Finishing Your Edit Session


After you nish the exercises in this appendix, you should save the le and quit vi. To save your changes and quit vi, enter:
:wq Return

If you want to quit vi without saving your changes, you can do so by entering:
:q! Return

You have now learned enough about vi to edit any le. The following sections show you some advanced techniques that can improve your productivity and allow you to customize your environment.

A.2 Using Advanced Techniques


This section shows you how to search for text strings, move text, and copy and paste text. As you work with larger documents, all these tasks increase your ability to work efciently.

A.2.1

Searching for Strings


In a large document, searching for a particular text string can be very time consuming. The slash ( / ) command prompts for a text string to search for in the le. When you enter the slash (/) and press the Return key, vi searches the le for the rst occurrence of the text string you entered. If you do not have it open, reopen the my.file le. Move to the top of the document using one of the cursor movement keys you learned earlier in this appendix. To search for the text string th, enter the following:
/th Return

As soon as you enter the slash (/) command, the slash (/) is displayed at the bottom of the screen (similar to the way in which the colon (:) works). When you entered the text string th, it was echoed (displayed) at the bottom of the screen. You can use the Backspace key to x mistakes when you enter the search string. After you press the Return key, the cursor moves to the rst occurrence of the string (the th in the word this).

A16 A Beginners Guide to Using vi

The n (next) command continues the search for the next occurrence of the last string you searched for. Try it now by entering:
n

The cursor should move to the next occurrence of the string, which is the th in the word with. Similarly, the N command searches for the next occurrence of the search string, but it searches in the opposite direction of the n command. The N nds the previous occurence of the string. The question mark ( ?) command is also used to initiate a search for text strings, but the question mark (?) initiates a backward search through the le. When you search backward, the n command moves the cursor backward to the previous occurrence of the string, and the N command moves the cursor forward (exactly the opposite of the way in which they work with a slash (/) search).

A.2.2

Deleting and Moving Text


To move a block of text, you must rst select the text to move. You already know how to do this. The delete ( d) command not only deletes a line of text but also copies it to a paste buffer. Once in the paste buffer, the text can be moved (or pasted) by repositioning the cursor and then using the lowercase p command to paste the text on the line after the current cursor position. Move the cursor to the rst line in the le and type:
dd

The line is deleted and copied into the paste buffer, and the cursor is located on the next line in the le. To paste the line in the buffer back into the le, after the line on which the cursor is positioned, enter:
p

The uppercase letter P (Paste) command is used to paste text on the line above the cursor rather than below it. If you delete a letter or block of words, the deleted text will be pasted into the new position within the current line. For example, to move the word can to just before the word with, use the following command sequence (remember to use an uppercase P):
/can Return dw /with Return P

A Beginners Guide to Using vi A17

A.2.3

Yanking and Moving Text


You copy text in the same manner as you move it, except that instead of using the delete text command d, you use the yank text command, y. The y command copies (or yanks) the specied text into the paste buffer without deleting it from the text. It follows the same syntax as the d command. You can also use the yy command to yank an entire text line into the paste buffer, in the same way as dd. For example, to copy the rst two lines of the le to a position immediately underneath them, enter the following command sequence from the rst line of the le:
2yy j p

You must move the cursor down one line using j or the two lines will be pasted after the rst line rather than after the second.

A.2.4

Other vi Features
You may want to try some of the other features of vi. The vi(1) reference page lists all of the available commands. You may want to pay particular attention to the following: J . s x ~ ! Ctrl/L Joins the next line of text to the current line of text. Repeats the last command. Substitutes the current character with the following entered text. Deletes the current character. Changes the alphabetic case of the current character. Executes an operating system command on the current line of text and replaces the text with the output. Refreshes the screen when problems with the screen display occur. Any time your screen is displaying confusing output, press Ctrl/L.

A.3 Using the Underlying ex Commands


The vi text editor is based upon the ex line editor. The underlying ex line editor can bring the power of global changes to your entire text le or any large piece of it. You can access ex commands from within vi by using the colon ( :) command. You were introduced to ex commands earlier in this appendix with the :wq and :q! commands for writing and quitting an editing session.

A18 A Beginners Guide to Using vi

The colon ( :) command causes ex to prompt for a command line at the bottom of the editor screen with a colon ( :). Each ex command is ended by pressing the Return key. You can also enter ex more permanently with the vi command Q. This command turns processing over to ex until you explicitly return to vi. This often happens accidentally. If it should happen to you, you can return to vi by typing vi at the colon ( :) prompt followed by the Return key as follows:
:vi Return

An ex command acts on a block of lines in your text le according to the following general syntax:
:[address[,address]]command

The command, along with any of its arguments, acts on the lines between and including the rst and second address. If one address is specied, the command acts only on the specied line. If no address is specied, the command acts only on the current line. Addresses can be specied in a number of ways. Some of the more common address specications are the following:

line number /pattern/ . $ addresslines %

Address by absolute line number. Next line that contains the pattern. Line that the cursor is on. Last line of the le. Relative offset from the addressed line. All the lines in the le, and is used once in place of both addresses.

The following sections show some of the most generally useful ex commands, and some of the customization features offered by ex. You should read the ex(1) reference page for a more detailed list of commands.

A Beginners Guide to Using vi A19

A.3.1

Making Substitutions
The most common substitution task, possibly the most common ex task, is a global substitution of one word or phrase for another. You can do this with the s command. If you have closed the my.file le, reopen it. To change every occurrence of "is" to "was", use the following command:
:%s/is/was/g Return

This substitution command is applied to all lines in the le by the % address. The slash ( / ) is used as a separator. The g argument at the end of the command causes the substitution to occur globally, that is, on each instance of the pattern within each line. Without the g argument, substitution occurs only once on each line. You should be careful when making substitutions to ensure that you get what you want. In the previous command line, the word this has changed to thwas because every occurrence of is was changed to was. You can add a c argument along with the g argument to prompt for conrmation before each substitution. The format of the conrmation is a bit complex; however, it is well worth using when you want to be scrupulous about making global changes. As an example of conrming a substitution, change the word thwas back to this by issuing the following command:
:%s/thwas/this/gc Return

The following prompt appears at the bottom of the screen:


You, too, use thwas new text file ^^^^^

As shown in the next example, type y and press the Return key. You are then prompted for the second substitution:
You, too, use thwas new text file ^^^^^y Return You, too, use thwas new text file ^^^^^

Type y and press the Return key, and in response to the Hit return to continue prompt, press the Return key once again as follows:
You, too, use thwas new text file ^^^^^y You, too, use thwas new text file ^^^^^y Return [Hit return to continue] Return

You will nd that the two occurrences of the word thwas have been changed back to this. In addition, vi is back in the command mode with the cursor positioned at the rst character of the line with the last substitution.

A20 A Beginners Guide to Using vi

Now try another substitution on your example le. Add three lines of new text to the le by using the $ (go to beginning of last line), o (create new line), yy (yank), and p (paste) commands as follows:
:$ Return o Some new text with a mispelling. Escape yy p p p

You now should have four lines of new text, all containing the incorrectly spelled word mispelling. To x the spelling error, enter one of the following commands:
:1,$s/mispelling/misspelling/ Return

or
:5,8s/mispelling/misspelling/ Return

In the rst example, the address 1,$ indicates that the substitution should begin on line one (1) and end at the last line of the le ($). In the second example, 5,8 indicates that the substitution should being on line ve and end on line eight. You do not need to use the g operator in either case because the change is only necessary once on each line.

A.3.2

Writing a Whole File or Parts of a File


The :wq command is a special ex command that writes the whole le. It combines the features of the write command w and the quit command q. The only argument that the quit command can take is the exclamation point (!). It forces the session to quit even if changes made to the le would be lost by quitting. The w command can also take addresses and a lename argument, which allows you to save part of your text to another le. For example, to save the rst three lines of your text to the new le my.new.file, use the following command:
:1,3w my.new.file Return "my.new.file" [New file] 3 lines, 130 characters

A Beginners Guide to Using vi A21

A.3.3

Deleting a Block of Text


The delete command in ex is d, just as in vi. To delete from the current line to the end of the le, use the following command:
:.,$d Return

A.3.4

Customizing Your Environment


The ex editor provides two mechanisms for customizing your vi environment. You can use the :set command to set environment variables, and the :map command to map a key sequence to a vi command key. Environment variables are set either by assigning them as option or no option for Boolean variables, or by assigning them as option=value. The full set of environment variables is described in the ex(1) reference page. Table A-5 lists some common variables. Table A-5: Selected vi Environment Variables
Variable Description Species that when an error is made, a bell sounds. This is the default setting. Species that when performing searches, the case of characters should be ignored. The default variable setting is noignorecase. Species that line numbers are to be displayed at the left margin. The default variable setting is nonumber. Species that when you enter a matching parenthesis or brace, the cursor moves to the matching character and then returns. The default variable setting is noshowmatch. Species the amount of space between tab stops. The default setting is 8. Species that searches should wrap around the beginning or end of the le. This is the default variable setting.

errorbells ignorecase

number showmatch

tabstop wrapscan

A22 A Beginners Guide to Using vi

Table A-5:
Variable

(continued)
Description Creates an automatic right margin located a specied number of characters from the right side of your screen. Whenever your cursor reaches the specied right margin, an automatic new line is generated, and the word you are typing is brought to the next line. We recommend that you set the wrapmargin variable to a value with which you are comfortable. Otherwise, vi will use the default setting of 0. Using the default setting means that your cursor jumps to the next line when it reaches the end of your screen; however, parts of the word you are keying may be on separate lines.

wrapmargin

To display the line numbers of your example le enter the following command:
:set number Return

To remove the line numbers, enter the following command:


:set nonumber Return

The :map command sets a single vi command key to a vi command sequence. The syntax for the :map command follows:
:map key sequence Return

This command sequence replaces any existing command for that key. The command sequence should be identical to the keystrokes you want to map, except that special keys such as the Return key, the Escape key, and keys modied with the Ctrl key must be quoted rst with Ctrl/V. Because the q and v keys do not have commands associated with them, they are good keys to map. For example, to map a key sequence that inserts a line into your text that says "This space held for new text", you could use the following command:
:map q oThis space held for new text Ctrl/V
Escape Return

Note the use of Ctrl/V to quote the Escape character.

A Beginners Guide to Using vi A23

A.3.5

Saving Your Customizations


You can make your environment customizations permanent by placing the appropriate ex commands in a le named .exrc in your home directory. Commands in this le will take effect every time you enter vi or ex. In this le, you do not need to use the vi command :, because these commands are read directly by the underlying ex editor. For example, to customize your environment to always display line numbers for your les, to use the map sequence shown in the previous section, and to set an automatic right margin of ve spaces, you would rst open the .exrc le with vi in your home directory, and add the following lines of text:
set number map q oThis space held for new text Ctrl/V Escape set wrapmargin=5

After you write this le, verify that it works by opening your example le.

A24 A Beginners Guide to Using vi

Creating and Editing Files with ed

This appendix explains how to create, edit (modify), display, and save text les with ed, a line editing program. If your system has another editing program, you may want to learn how to do these tasks with that program. A good way to learn how ed works is to try the examples in this appendix on your system. Since the examples build upon each other, it is important for you to work through them in sequence. Also, to make what you see on the screen consistent with what you see in this guide, it is important to do the examples just as they are given. In the examples, everything you should enter is printed in boldface. When you are told in the text to enter something, you should enter all of the information for that line and then press Return. Because ed is a line editor, you can work with the contents of a le only one line at a time. Regardless of what text is on the screen, you can edit only the current line. If you have experience with a screen editing program, you should pay careful attention to the differences between that program and ed. For example, with the ed program, you cannot use the Cursor Up and Cursor Down keys to change your current line.

B.1 Understanding Text Files and the Edit Buffer


A le is a collection of data stored together in the computer under an assigned name. You can think of a le as the computer equivalent of an ordinary le folder it may contain the text of a letter, a report, or some other document, or the source code for a computer program. The edit buffer is a temporary storage area that holds a le while you work with it the computer equivalent of the top of your desk. When you work with a text le, you place it in the edit buffer, make your changes to the le (edit it), and then transfer (copy) the contents of the buffer to a permanent storage area. The rest of this appendix explains how to create, display, save, and edit (modify) text les with the ed editor.

B.2 Creating and Saving Text Files


To create and save a text le, perform the following steps. The following sections describe these steps in detail. 1. At the shell prompt, enter the following command:
$ ed filename

The filename argument is the name of the le you want to create or edit. 2. When you receive the ? filename message, enter the following append command:
a

3. Enter your text. 4. To stop adding text, enter a dot (.) at the start of a new line. 5. To copy the contents of the edit buffer into the filename le, enter the following command:
w

6. To end the ed program, enter the following command:


q

B.2.1

Starting the ed Program


To start the ed program, enter a command of the form ed filename after the shell prompt ($). In the following example, the ed afile command starts the ed program and indicates that you want to work with a le named afile:
$ ed afile ?afile _

The ed program responds with the message ?afile, which means that the le does not exist. You can now use the a (append) subcommand (described in the next section) to create afile and put text into it.

B.2.2

Entering Text The a (Append) Subcommand


To put text into your le, enter a. The a subcommand tells ed to add, or append, the text you enter to the edit buffer. If your le had already contained text, the a subcommand would add the new text to the end of the le.

B2 Creating and Editing Files with ed

Type your text, pressing Return at the end of each line. When you have entered all of your text, enter a dot (.) at the start of a new line. Note If you do not press Return at the end of each line, the ed program automatically moves your cursor to the next line after you ll a line with characters. However, ed treats everything you enter before you press Return as one line, regardless of how many lines it takes up on the screen; that is, the line wraps around to the beginning of the next line (based upon your workstation display settings). The following example shows how to enter text into the afile le:
a The only way to stop appending is to enter a line that contains only a dot. . _

If you stop adding text to the buffer and then decide to add some more, enter another a subcommand. Type the text and then enter a dot at the start of a new line to stop adding text to the buffer. If you make errors as you enter your text, you can correct them before you press Return. Use the Backspace key to erase the incorrect character(s). Then enter the correct characters in their place.

B.2.3

Displaying Text The p (print) Subcommand


Use the p (print) subcommand to display the contents of the edit buffer. To display a single line, use the np subcommand, where n is the number of the line. For example:
2p appending is to enter a _

To display a series of lines, use the n,mp subcommand, where n is the starting line number and m is the ending line number. For example:
1,3p The only way to stop appending is to enter a line that contains only _

To display everything from a specic line to the end of the buffer, use the n,$p subcommand, where n is the starting line number and $ stands for the

Creating and Editing Files with ed B3

last line of the buffer. In the following example, 1,$p displays everything in the buffer:
1,$p The only way to stop appending is to enter a line that contains only a dot. _

Note Many examples in the rest of this appendix use 1,$p to display the buffers contents. In these examples, the 1,$p subcommand is optional and convenient it lets you verify that the subcommands in examples work as they should. Another convenient ed convention is ,p, which is equivalent to 1,$p that is, it displays the contents of the buffer.

B.2.4

Saving Text The w (write) Subcommand


The w (write) subcommand writes, or copies, the contents of the buffer into a le. You can save all or part of a le under its original name or under a different name. In either case, ed replaces the original contents of the le you specify with the data copied from the buffer.

B.2.4.1

Saving Text under the Same Filename To save the contents of the buffer under the original name for the le, enter the w subcommand. For example:
w 78 _

The ed program copies the contents of the buffer into the le named afile and displays the number of characters copied into the le (78). This number includes blanks and characters such as Return (sometimes called newline), which are not visible on the screen. The w subcommand does not affect the contents of the edit buffer. You can save a copy of the le and then continue to work with the contents of the buffer. The stored le is not changed until the next time you use the w subcommand to copy the contents of the buffer into it. As a safeguard, it is a good practice to save a le periodically while you work on it. Then, if you make changes (or mistakes) that you do not want to save, you can start over with the most recently saved version of the le.

B4 Creating and Editing Files with ed

Note The u (undo) subcommand restores the buffer to the state it was in before it was last modied by an ed subcommand. The subcommands that u can reverse are a, c, d, g, G, i, j, m, r, s, t, v, and V.

B.2.4.2

Saving Text under a Different Filename Often, you may need more than one copy of the same le. For example, you could have the original text of a letter in two les one to keep as it is, and the other to be revised. If you have followed the previous examples, you have a le named afile that contains the original text of your document. To create another copy of the le (while its contents are still in the buffer), use a subcommand of the form w filename, as the following example shows:
w bfile 78 _

At this point, afile and bfile have the same contents, since each is a copy of the same buffer contents. However, because afile and bfile are separate les, you can change the contents of one without affecting the contents of the other. B.2.4.3 Saving Part of a File To save part of a le, use a subcommand of the form n,mw filename, where:

n m

Species the beginning line number of the part of the le you want to save. Species the ending line number of the part of the le you want to save (or the number of a single line, if that is all you want to save). Species the name of a different le (optional).

filename

In the following example, the w subcommand copies lines 1 and 2 from the buffer into a new le named cfile:
1,2w cfile 44 _

Then ed displays the number of characters written into cfile (44).

Creating and Editing Files with ed B5

B.2.5

Leaving the ed Program The q (quit) Subcommand


Caution The contents of the buffer are lost when you leave the ed program. To save a copy of the data in the buffer, use the w subcommand to copy the buffer into a le before you leave the ed program. To leave the ed program, enter the q (quit) subcommand. For example:
q $ _

The q subcommand returns you to the shell prompt ($). If you have changed the buffer but have not saved a copy of its contents, the q subcommand responds with ?, an error message. At that point, you can either save a copy of the buffer with the w subcommand, or enter q again to leave the ed program without saving a copy of the buffer.

B.3 Loading Files into the Edit Buffer


Before you can edit a le, you must load it into the edit buffer. You can load a le either at the time you start the ed program or while the program is running. To load a le into the edit buffer when you start the ed program, enter the following command:
ed filename

This command starts ed and loads the filename le into the edit buffer. To load a le into the edit buffer while ed is running, you can enter one of the following commands: e filename This loads the filename le into the buffer, erasing any previous contents of the buffer.

nr filename This reads the filename le into the buffer after line n. If you do not specify n, ed adds the le to the end of the buffer.

B6 Creating and Editing Files with ed

B.3.1

Using the ed (edit) Command


To load a le into the edit buffer when you start the ed program, enter the name of the le after the ed command. The ed command in the following example invokes the ed program and loads the le afile into the edit buffer:
$ ed afile 78 _

The ed program displays the number of characters that it read into the edit buffer (78). If ed cannot nd the le, it displays ?filename . To create that le, use the a (append) subcommand (described in Section B.2.2) and the w (write) subcommand (described in Section B.2.4).

B.3.2

Using the e (Edit) Subcommand


Once you start the ed program, you can use the e (edit) subcommand to load a le into the buffer. The e subcommand replaces the contents of the buffer with the new le. (Compare the e subcommand with the r subcommand, described next in Section B.3.3, which adds the new le to the buffer.) Caution When you load a new le into the buffer, the new le replaces the buffers previous contents. Save a copy of the buffer with the w subcommand before you read a new le into the buffer. In the following example, the e cfile subcommand reads the cfile le into the edit buffer, replacing afile. The e afile subcommand then loads afile back into the buffer, deleting cfile. The ed program returns the number of characters read into the buffer after each e subcommand (44 and 78):
e cfile 44 e afile 78 _

If ed cannot nd the le, it returns ? filename. To create that le, use the a (append) subcommand, described in Section B.2.2, and the w (write) subcommand, described in Section B.2.4.

Creating and Editing Files with ed B7

You can edit any number of les, one at a time, without leaving the ed program. Use the e subcommand to load a le into the buffer, make your changes to the le, and use the w subcommand to save a copy of the revised le. (See Section B.2.4 for information about the w subcommand.) Then use the e subcommand again to load another le into the buffer.

B.3.3

Using the r (read) Subcommand


Once you have started the ed program, you can use the r (read) subcommand to read a le into the buffer. The r subcommand adds the contents of the le to the contents of the buffer. The r subcommand does not delete the buffer. (Compare the r subcommand with the e subcommand, described in Section B.3.2, which deletes the buffer before it reads in another le.) With the r subcommand, you can read a le into the buffer at a particular place. For example, the 4r cfile subcommand reads the le cfile into the buffer following line 4. The ed program then renumbers all of the lines in the buffer. If you do not use a line number, the r subcommand adds the new le to the end of the buffers contents. The following example shows how to use the r subcommand with a line number:
1,$p The only way to stop appending is to enter a line that contains only a dot. 3r cfile 44 1,$p The only way to stop appending is to enter a line that contains only The only way to stop appending is to enter a a dot. _

The 1,$p subcommand displays the four lines of afile. Next, the 3r cfile subcommand loads the contents of cfile into the buffer, following line 3, and shows that it read 44 characters into the buffer. The next 1,$p subcommand displays the buffers contents again, letting you verify that the r subcommand read cfile into the buffer after line 3. If you are working the examples on your system, complete the following steps before you go to the next section:

B8 Creating and Editing Files with ed

1. Save the contents of the buffer in the cfile le:


w cfile

2. Load afile into the buffer:


e afile

B.4 Displaying and Changing the Current Line


The ed program is a line editor. This means that ed lets you work with the contents of the buffer one line at a time. The line you can work with at any given time is called the "current line", and it is represented by the dot (.). To work with different parts of a le, you must change the current line. To display the current line, enter the following subcommand:
p

To display the line number of the current line, enter the following subcommand:
.=

Note You cannot use the Cursor Up and Cursor Down keys to change the current line. To change the current line, use the ed subcommands described in the following sections. To change your position in the buffer, do one of the following. These steps are described in detail in the following sections. 1. To set your current line to line number n, enter the following subcommand:
n

2. To move the current line forward through the buffer one line at a time press Return. 3. To move the current line backward through the buffer one line at a time, enter a dash () character. 4. To move the current line n lines forward through the buffer, enter the following subcommand:
.+n

Creating and Editing Files with ed B9

5. To move the current line n lines backward through the buffer, enter the following subcommand:
.-n

B.4.1

Finding Your Position in the Buffer


When you rst load a le into the buffer, the last line of the le is the current line. As you work with the le, you usually change the current line many times. You can display the current line or its line number at any time. To display the current line, enter p:
p a dot. _

The p subcommand displays the current line (a dot.). Because the current line has not been changed since you read afile into the buffer, the current line is the last line of the buffer. Enter .= to display the line number of the current line:
.= 4 _

Since afile has four lines, and the current line is the last line in the buffer, the .= subcommand displays 4. You also can use the dollar sign (the symbol that stands for the last line in the buffer) with the = subcommand to determine the number of the last line in the buffer:
$= 4 _

The $= subcommand is an easy way to nd out how many lines are in the buffer. The ed $ symbol has no relationship to the shell prompt ($).

B.4.2

Changing Your Position in the Buffer


You can change your position in the buffer (change your current line) in one of two ways: Specify a line number (an absolute position) Move forward or backward relative to your current line

B10 Creating and Editing Files with ed

To move the current line to a specic line, enter the line number; ed displays the new current line. In the following example, the rst line of afile becomes the current line:
1 The only way to stop _

Pressing Return advances one line through the buffer and displays the new current line, as the following example shows:
appending is to enter a line that contains only a dot. ? _

When you try to move beyond the last line of the buffer, ed returns ?, an error message. You cannot move beyond the end of the buffer. To set the current line to the last line of the buffer, enter $. To move the current line backward through the buffer one line at a time, enter dashes () one after the other, as the following example shows:
line that contains only appending is to enter a The only way to stop ?_

When you try to move beyond the rst line in the buffer, you receive the ? message. You cannot move beyond the top of the buffer. To move the current line forward through the buffer more than one line at a time, enter .n (where n is the number of lines you want to move):
.2 line that contains only _

Note that .2 is an abbreviation for .+2.

Creating and Editing Files with ed B11

To move the current line backward through the buffer more than one line at a time, enter the following subcommand: .- n (where n is the number of lines you want to move):
.-2 The only way to stop _

B.5 Locating Text


If you do not know the number of the line that contains a particular word or another string of characters, you can locate the line with a context search. To search in context, do one of the following: To search forward, enter the following subcommand:
/string to find/

To search backward, enter the following subcommand:


?string to find?

The following sections describe these methods of searching text in detail.

B.5.1

Searching Forward Through the Buffer


To search forward through the buffer, enter the string enclosed in slashes ( / / ):
/only/ line that contains only _

The context search (/only/) begins on the rst line after the current line, then locates and displays the next line that contains the string "only". That line becomes the current line. If ed does not nd the string between the rst line of the search and the last line of the buffer, then it continues the search at line 1 and searches to the current line. If ed searches the entire buffer without nding the string, it displays the ? error message:
/random/ ? _

Once you have searched for a string, you can search for the same string again by entering //. The following example shows one search for the string

B12 Creating and Editing Files with ed

only, and then a second search for the same string:


/only/ The only way to stop // line that contains only _

B.5.2

Searching Backward Through the Buffer


Searching backward through the buffer is much like searching forward, except that you enclose the string in question marks (?):
?appending? appending is to enter a _

The context search begins on the rst line before the current line, and locates the rst line that contains the string appending. That line becomes the current line. If ed searches the entire buffer without nding the string, it stops the search at the current line and displays the message ?. Once you have searched backward for a string, you can search backward for the same string again by entering ??. This is because ed remembers search strings.

B.5.3

Changing the Direction of a Search


You can change the direction of a search for a particular string by using the slash ( / ) and question mark (?) search characters alternately:
/only/ line that contains only ?? The only way to stop _

If you go too far while searching for a character string, it is convenient to be able to change the direction of your search.

B.6 Making Substitutions The s (substitute) Subcommand


Use the s (substitute) subcommand to replace a character string (a group of one or more characters) with another. The s subcommand works with one or more lines at a time, and is especially useful for correcting typing or spelling errors. To make substitutions, do one of the following:

Creating and Editing Files with ed B13

To substitute newstring for oldstring at the rst occurrence of oldstring in the current line, enter the following subcommand:
s/oldstring/newstring/

To substitute newstring for oldstring at the rst occurrence of oldstring on line number n, enter the following subcommand:
ns/ oldstring/ newstring/

To substitute newstring for oldstring at the rst occurrence of oldstring in each of the lines n through m, enter the following subcommand:
n,ms/ oldstring/ newstring/

The following sections describe these methods of substitution in detail.

B.6.1

Substituting on the Current Line


To make a substitution on the current line, rst make sure that the line you want to change is the current line. In the following example, the /appending/ (search) subcommand locates the line to be changed. Then the s/appending/adding text/p (substitute) subcommand substitutes the string "adding text" for the string "appending" on the current line. The p (print) subcommand displays the changed line.
/appending/ appending is to enter a s/appending/adding text/p adding text is to enter a _

Note For convenience, you can add the p (print) subcommand to the s subcommand (for example, s/appending/adding text/p). This saves you from having to enter a separate p subcommand to see the result of the substitution. The s subcommand changes only the rst occurrence of the string on a given line. To learn how to change all occurrences of a string on the line, see Section B.6.4.

B14 Creating and Editing Files with ed

B.6.2

Substituting on a Specic Line


To make a substitution on a specic line, use a subcommand of the following form:
ns/ oldstring/ newstring/

Here n is the number of the line on which the substitution is to be made. In the following example, the s subcommand moves to line number 1 and replaces the string "stop" with the string "quit" and displays the new line:
1s/stop/quit/p The only way to quit _

The s subcommand changes only the rst occurrence of the string on a given line. To learn how to change all occurrences of a string on the line, see Section B.6.4.

B.6.3

Substituting on Multiple Lines


To make a substitution on multiple lines, use a subcommand of the following form:
n,ms/ oldstring/ newstring/

Here n is the rst line of the group and m is the last. In the following example, the s subcommand replaces the rst occurrence of the string "to" with the string "TO" on every line in the buffer:
1,$s/to/TO/ 1,$p The only way TO quit adding text is TO enter a line that contains only a dot. _

The 1,$p subcommand displays the contents of the buffer, which lets you verify that the substitutions were made.

B.6.4

Changing Every Occurrence of a String


Ordinarily, the s (substitute) subcommand changes only the rst occurrence of a string on a given line. However, the g (global) operator lets you change every occurrence of a string on a line or in a group of lines. To make a global substitution on a single line, use a subcommand of the following form:
ns/ oldstring/ newstring/

Creating and Editing Files with ed B15

In the following example, 3s/on/ON/gp changes each occurrence of the string "on" to "ON" in line 3 and displays the new line:
3s/on/ON/gp line that cONtains ONly _

To make a global substitution on multiple lines, specify the group of lines with a subcommand of the form:
n,ms/ oldstring/ newstring/g

In the following example, 1,$s/TO/to/g changes the string "TO" to the string "to" in every line in the buffer:
1,$s/TO/to/g 1,$p The only way to quit adding text is to enter a line that cONtains ONly a dot. _

B.6.5

Removing Characters
You can use the s (substitute) subcommand to remove a string of characters (that is, to replace the string with nothing). To remove characters, use a subcommand of the form s/oldstring// (with no space between the last two / characters). In the following example, ed removes the string "adding" from line number 2 and then displays the changed line:
2s/adding// text is to enter a _

B.6.6

Substituting at Line Beginnings and Ends


Two special characters let you make substitutions at the beginning or end of a line: ^ (circumex) $ (dollar sign) Makes a substitution at the beginning of the line. Makes a substitution at the end of the line. (In this context, the dollar sign ($) character does not stand for the last line in the buffer.)

B16 Creating and Editing Files with ed

To make a substitution at the beginning of a line, use the s/^/newstring subcommand. In the following example, one s subcommand adds the string "Remember" to the start of line number 1. Another s subcommand adds the string "adding" to the start of line 2:
1s/^/Remember,/p Remember, The only way to quit 2s/^/adding/p adding text is to enter a _

To make a substitution at the end of a line, use a subcommand of the form s/$/newstring. In the following example, the s subcommand adds the string "Then press Enter." to the end of line number 4:
4s/$/ Then press Enter./p a dot. Then press Enter. _

Notice that the substituted string includes two blanks before the word Then to separate the two sentences.

B.6.7

Using a Context Search


If you do not know the number of the line you want to change, you can locate it with a context search. See Section B.5 for more information on context searches. For convenience, you can combine a context search and a substitution into a single subcommand in the following format:
/string to nd/s/oldstring/newstring/

In the following example, ed locates the line that contains the string ", The" and replaces that string with ", the":
/, The/s/, The/, the/p Remember, the only way to quit _

Also, you can use the search string as the string to be replaced with a subcommand of the form /string to find/s//newstring/. In the following example, ed locates the line that contains the string "cONtains ONly", replaces that string with "contains only", and prints the changed line:
/cONtains ONly/s//contains only/p line that contains only _

Creating and Editing Files with ed B17

B.7 Deleting Lines The d (delete) Subcommand


Use the d (delete) subcommand to remove one or more lines from the buffer. The general form of the d subcommand is the following:
starting line,ending line d

After you delete lines, ed sets the current line to the rst line following the lines that were deleted. If you delete the last line from the buffer, the last remaining line in the buffer becomes the current line. After a deletion, ed renumbers the remaining lines in the buffer. To delete lines from the buffer, do the following: To delete the current line, enter the following subcommand:
d

To delete line number n from the buffer, enter the following subcommand:
nd

To delete lines numbered n through m from the buffer, enter the following subcommand:
n,md

The following sections describe these methods of deleting lines in detail.

B.7.1

Deleting the Current Line


If you want to delete the current line, enter d. In the following example, the 1,$p subcommand displays the entire contents of the buffer, and the $ subcommand makes the last line of the buffer the current line:
1,$p Remember, the only adding is to enter line that contains a dot. Then press $ a dot. Then press d _ way to quit a only Enter. Enter

The d subcommand then deletes the current line (in this case, the last line in the buffer).

B18 Creating and Editing Files with ed

B.7.2

Deleting a Specic Line


If you know the number of the line you want to delete, use a subcommand of the form nd to make the deletion. In the following example, the 2d subcommand deletes line 2 from the buffer:
2d 1,$p Remember, the only way to quit line that contains only _

The 1,$p subcommand displays the contents of the buffer, showing that the line was deleted.

B.7.3

Deleting Multiple Lines


To delete a group of lines from the buffer, use a subcommand of the form n,md, where n is the starting line number and m is the ending line number of the group to be deleted. In the following example, the 1,2d subcommand deletes lines 1 and 2:
1,2d 1,$p ? _

The 1,$p subcommand displays the ? message, indicating that the buffer is empty. If you are following the examples on your system, you should restore the contents of the buffer before you move on to the next section. The following example shows you how to restore the contents of the buffer:
e afile ? e afile 78 _

This command sequence reads a copy of the original le afile into the buffer.

B.8 Moving Text The m (move) Subcommand


Use the m (move) subcommand to move a group of lines from one place to another in the buffer. After a move, the last line moved becomes the current line. To move text, enter a subcommand of the following form:
x,y m z

Creating and Editing Files with ed B19

The x variable is the rst line of the group to be moved. The y variable is the last line of the group to be moved. The z variable is the line the moved lines are to follow. In the following example, the 1,2m4 subcommand moves the rst two lines of the buffer to the position following line 4:
1,2m4 1,$p line that contains only a dot. The only way to stop appending is to enter a _

The 1,$p subcommand displays the contents of the buffer, showing that the move is complete. To move a group of lines to the top of the buffer, use zero (0) as the line number for the moved lines to follow. In the next example, the 3,4m0 subcommand moves lines 3 and 4 to the top of the buffer:
3,4m0 1,$p The only way to stop appending is to enter a line that contains only a dot. _

The 1,$p subcommand displays the contents of the buffer, showing that the move was made. To move a group of lines to the end of the buffer, use $ as the line number for the moved lines to follow:
1,2m$ 1,$p line that contains only a dot. The only way to stop appending is to enter a _

B.9 Changing Lines of Text The c (change) Subcommand


Use the c (change) subcommand to replace one or more lines with one or more new lines. The c subcommand rst deletes the line(s) you want to replace and then lets you enter the new lines, just as if you were using the a (append) subcommand. When you have entered all of the new text, enter a dot (.) on a line by itself.

B20 Creating and Editing Files with ed

The general form of the c subcommand is:


starting line,ending line c

To change lines of text, do the following: 1. Enter a subcommand of the following form:
n,m c

The n variable species the number of the rst line of the group to be deleted. The m variable species the number of the last line of the group (or the only line) to be deleted. 2. Type the new line(s), pressing Return at the end of each line. 3. Enter a dot on a line by itself. The following sections describe these methods of searching text in detail.

B.9.1

Changing a Single Line of Text


To change a single line of text, use only one line number with the c (change) subcommand. You can replace the single line with as many new lines as you like. In the following example, the 2c subcommand deletes line 2 from the buffer, and then you can enter new text:
2c appending new material is to use the proper keys to create a . 1,$p The only way to stop appending new material is to use the proper keys to create a line that contains only a dot. _

The dot on a line by itself stops ed from adding text to the buffer. The 1,$p subcommand displays the entire contents of the buffer, showing that the change was made.

B.9.2

Changing Multiple Lines of Text


To change more than one line of text, give the starting and ending line numbers of the group of lines to be with the c subcommand. You can replace the group of lines with one or more new lines.

Creating and Editing Files with ed B21

In the following example, the 2,3c subcommand deletes lines 2 and 3 from the buffer, and then you can enter new text:
2,3c adding text is to enter a . 1,$p The only way to stop adding text is to enter a line that contains only a dot. _

The dot on a line by itself stops ed from adding text to the buffer. The 1,$p subcommand displays the entire contents of the buffer, showing that the change was made.

B.10 Inserting Text The i (insert) Subcommand


Use the i (insert) subcommand to insert one or more new lines of text into the buffer. To locate the place in the buffer for the lines to be inserted, you can use either a line number or a context search. The i subcommand inserts new lines before the specied line. (Compare the i subcommand with the a subcommand, explained in Section B.2.2, which inserts new lines after the specied line.) To insert text, do the following: 1. Enter a subcommand of one of the following types:
ni

The n variable species the number of the line the new lines will be inserted above.
/string/i

The string variable species a group of characters contained in the line the new lines will be inserted above. 2. Enter the new lines. 3. Enter a dot at the start of a new line. The following sections describe these methods of inserting text in detail.

B.10.1

Using Line Numbers


If you know the number of the line where you want to insert new lines, you can use an insert subcommand of the form ni (where n is a line number). The new lines you enter go into the buffer before line number n. To end the i subcommand, enter a dot (.) on a line by itself.

B22 Creating and Editing Files with ed

In the following example, the 1,$p subcommand prints the contents of the buffer. Then the 4i subcommand inserts new lines before line number 4:
1,$p The only way to stop adding text is to enter a line that contains only a dot. 4i --repeat, only-. 1,$p The only way to stop adding text is to enter a line that contains only --repeat, only-a dot. _

After 4i, you enter the new line of text and enter a dot on the next line to end the i subcommand. A second 1,$p subcommand displays the contents of the buffer again, showing that the new text was inserted.

B.10.2

Using a Context Search


Another way to specify where the i subcommand inserts new lines is to use a context search. With a subcommand of the form /string/i, you can locate the line that contains string and insert new lines before that line. When you nish inserting new lines, enter a dot on a line by itself. In the following example, the /dot/i subcommand inserts new text before the line that contains the string "dot":
/dot/i and in the first position-. 1,$p The only way to stop adding text is to enter a line that contains only --repeat, only-and in the first position-a dot. _

The 1,$p subcommand displays the entire contents of the buffer, showing the new text.

B.11 Copying Lines The t (transfer) Subcommand


With the t (transfer) subcommand, you can copy lines from one place in the buffer and insert the copies elsewhere. The t subcommand does not affect the original lines.
Creating and Editing Files with ed B23

The general form of the t subcommand is:


starting line,ending line t line to follow

To copy lines, enter a subcommand of the form:


n,m tx

The n variable species the rst line of the group to be copied. The m variable species the last line of the group to be copied. The x variable species the line the copied lines are to follow. To copy lines to the top of the buffer, use zero (0) as the line number for the copied lines to follow. To copy lines to the bottom of the buffer, use the dollar sign ($) as the line number for the copied lines to follow. In the following example, the 1,3t4 subcommand copies lines 1 through 3, and inserts the copies after line 4:
1,3t4 1,$p The only way to stop adding text is to enter a line that contains only --repeat, only-The only way to stop adding text is to enter a line that contains only and in the first position-a dot. _

The 1,$p subcommand displays the entire contents of the buffer, showing that ed has made and inserted the copies, and that the original lines are not affected.

B.12 Using System Commands from ed


Sometimes you may nd it convenient to use a system command without leaving the ed program. At these times you can use the exclamation point (!) character to leave the ed program temporarily. To use a system command from ed, enter the following:
!command

B24 Creating and Editing Files with ed

In the following example, the !ls command temporarily suspends the ed program and runs the ls (list) system command (a command that lists the les in the current directory):
!ls afile bfile cfile ! _

The ls command displays the names of the les in the current directory (afile, bfile, and cfile), and then displays another ! character. The ls command is nished, and you can continue to use ed. You can use any system command from within the ed program. You can even run another ed program, edit a le, and then return to the original ed program. From the second ed program, you can run a third ed program, use a system command, and so forth.

B.13 Ending the ed Program


This completes the introduction to the ed program. To save your le and end the ed program, perform the following steps: 1. Enter the w command, as follows:
w

2. Enter the q command, as follows:


q

For a full discussion of the w and q subcommands, see Section B.2.4 and Section B.2.5 respectively. For information about other features of ed, see the ed(1) reference page. For information about printing the les you create with ed, see Chapter 3.

Creating and Editing Files with ed B25

Using Internationalization Features

This appendix describes the internationalization features of the operating system. These features provide users with the ability to process data and to interact with the system in a manner appropriate to their native language, customs, and geographic region (their locale). After reading this appendix, you will be able to do the following: Understand the concept of locale Understand what functions are affected by locale Determine whether a locale has been set (if necessary) Set your locale (if necessary) Change your locale or aspects of your locale (if necessary)

If your site is in the United States and you plan to use the American English language and its conventions, there is no need to set a locale because the system default is American English. If your site is outside the United States, the locale will most likely have already been specied by the system administrator. If the locale has already been set, you may want to only skim this appendix for background information on internationalization. If the locale has not been set, the information in this appendix is essential to you.

C.1 Understanding Locale


Because Digital UNIX is an internationalized operating system, it can present information in a variety of ways. Users tell the operating system how to process and present information in a way appropriate for their language, country, and cultural customs by specifying a locale. See Section C.4 for information about how to specify a locale. A locale generally consists of three parts: language, territory, and codeset. All three are important for specifying how information is processed and displayed: Language species the native language (for example, German, French, English).

Territory species the geographic area (for example, Germany, France, Great Britain). Codeset species the coded character set that is used for the locale (for example, ISO 8859/1, the ISO Latin-1 codeset).

At this point, some background information about codesets may be helpful. The ASCII codeset has traditionally been used on UNIX systems to express American English. Each letter of the English alphabet (A to Z, a to z) as well as digits, control characters, and symbols are uniquely identied using only 7 of the 8 bits in a standard byte. However, the introduction of new codesets or expansion of old ones has been necessary to include non-English characters. Because so many programs rely on ASCII characters in one way or another, the most commonly used codesets begin with ASCII and build from there. By using all 8 bits of a standard byte, a single codeset can uniquely identify characters in several alphabetic languages. The most popular codesets are a series called ISO 8859. The rst in the series is called ISO 8859/1, the second is ISO 8859/2, and so on through ISO 8859/10. The ISO 8859/1 codeset, often called Latin-1, supports English and other Western European languages. To identify all ideographic symbols in Asian languages, such as Chinese and Japanese, character encoding requires more than one byte. Numerous codesets using multibyte character encoding, which is not supported by the ISO 8859 series of codesets, have been developed for Asian languages.

C.2 How Locale Affects Processing and Display of Data


As previously mentioned, the locale specied on your system inuences how information is processed and displayed. Specically, locale affects how the software: Collates (sorts) data Formats date and time expressions Formats monetary and other numeric expressions Displays messages Prompts for yes/no responses

The following sections describe the items in this list.

C2 Using Internationalization Features

C.2.1

Collation
Collation is the action of arranging elements of a set into a particular order. Collation always follows a set of rules. Some languages require collation rules that are not used in English. Multilevel Some languages include groups of characters that all sort to the same primary location. Additional sort rules apply to order characters within the same group. For example, the French characters a, a, a, and all sort ` a to the same primary location. Words that begin with these characters collate the same location, at which point words are sorted within the group. These words are in correct French order: a a abord a pre apr` s e a pret e azur One-to-two character mapping In some languages, certain single characters are treated as if they were two characters. For example, the German sharp s () is sorted as if it were ss. Multiple-to-one character mapping Some languages treat a string of characters as if it were a single element. For example, the Spanish ch and ll sequences are treated as unique characters in the Spanish alphabet. The following words are in correct Spanish order: canto construir curioso chapa chocolate dama Ignored characters Some collation rules ignore certain characters. For example, if the hyphen (-) is dened as a character to be ignored, the strings re-locate and relocate sort to the same position.
Using Internationalization Features C3

Note This means that you cannot assume that the range [A to z, a to z] includes every letter of an alphabet. For example, the Danish alphabet includes three characters that sort after z.

C.2.2

Date and Time Formats


Users around the world express dates and times with different formatting conventions. When specifying day and month names, people in the United States generally express dates with an expression like the following one:
Tuesday, May 22, 1996

The French, on the other hand, express dates this way:


mardi, 22 mai 1996

The following examples show alternative formats for the date, March 20, 1996. A given format is not the only way to write the date in the listed country: 3/20/96 (United States) 20/3/96 (Great Britain) 20.3.96 (France and Germany) 20-III-96 (Italy) 96/3/20 (Japan) 2/3/20 (Japan, Emperor format) In Japans Emperor format, the year (2, in the preceding example) is expressed as the number of years that the current emperor has reigned. As with dates, there are many conventions for expressing the time of day. In the United States, people often use the 12-hour clock with its a.m. and p.m. designations. People in most other countries use the 24-hour clock to express the time. In addition to the 12-hour/24-hour clock differences, punctuation for written times can vary, for example: 3:20 p.m. (United States) 15h20 (France) 15.20 (Germany) 15:20 (Japan)

C4 Using Internationalization Features

C.2.3

Numeric and Monetary Formats


The characters used to format numeric and monetary values vary from place to place. In the United States, the convention is to use a period (.) as the radix character (the character that separates whole and fractional quantities), and a comma (,) as the thousands separator. In many European countries, these conventions are reversed. For example: 1,234.56 (United States) 1.234,56 (France) Here are some sample formats for monetary items: $1,234.56 (United States, dollars) kr1.234,56 (Norway, krona) SFrs.1,234.56 (Switzerland, Swiss francs) Note that some formats for monetary amounts include more than two places for fractional digits.

C.2.4

Messages
Programs are sometimes written with English messages embedded in the program itself. In an internationalized program, messages are kept in a separate le and replaced in the program with calls to a messaging system. Messages kept in a separate le can be translated and made available to the program. When translated messages are available, users can interact with the system in their native language.

C.2.5

Yes/No Prompts
Many programs ask questions that need a positive or negative response. Those programs typically look for the English string literals y or yes, n or no. An internationalized program lets users enter the characters or words that are appropriate to their language. For example, a French user should be able to enter o or oui.

C.3 Determining Whether a Locale Has Been Set


If your system is functioning in accordance with the language and conventions of your country, you can assume that the locale has been set correctly.

Using Internationalization Features C5

If you are not sure whether or not your locale has been set, enter the locale command to display current settings of the locale environment variables, for example:
% locale LANG=fr_FR.ISO8859-1 LC_COLLATE="fr_FR.ISO8859-1" LC_CTYPE="fr_FR.ISO8859-1" LC_MONETARY="fr_FR.ISO8859-1" LC_NUMERIC="fr_FR.ISO8859-1" LC_TIME="fr_FR.ISO8859-1" LC_MESSAGES="fr_FR.ISO8859-1" LC_ALL=

The locale environment variables, described in Section C.4.1, dene the locale names used for messages, collation, codeset, numeric formats, monetary formats, date and time formats, and yes/no responses: LANG LC_COLLATE LC_CTYPE LC_NUMERIC LC_MONETARY LC_TIME LC_MESSAGES LC_ALL In most cases, only the LANG variable has been set to a locale name, which then applies to other locale variables with the exception of LC_ALL.

C.4 Setting a Locale


When you specify a locale, you specify a locale name that indicates language, territory, and codeset. On Digital UNIX systems, locale names adhere to the following format:

lang_terr.codeset lang Is a 2-letter, lowercase abbreviation for the language name. The abbreviations are specied in ISO 639 Code for the Representation of Names of Languages, for example: en (English), fr (French), de (German, from Deutsch), ja (Japanese). terr Is a 2-letter, uppercase abbreviation for the territory name. The abbreviations are specied in ISO 3116 Codes for the Representation of

C6 Using Internationalization Features

Names of Countries, for example: US (United States), NL (the Netherlands), FR (France), DE (Germany, from Deutschland), JP (Japan).

codeset Is a string that identies the codeset, for example: ISO8859-1 (ISO 8859/1), SJIS (Shift Japanese Industrial Standard), AJEC (Advanced Japanese EUC).
Full locale names include: en_US.ISO8859-1 (English, incorporating customs for the United States), fr_FR.ISO8859-1 (French, incorporating customs for France), de_DE.ISO8859-1 (German, incorporating customs for Germany). A locale can be set by the system administrator or an individual user. If your system administrator sets the locale at your site, it is likely that a default locale has been specied for all systems, including yours. You can override the default locale if you want to do that. To set a locale, you assign a locale name to one or more environment variables. The easiest way to do this is to assign a locale name to the LANG environment variable because this variable covers all the pieces of a locale (codeset, collating sequence, numeric, monetary, and date and time formats, messages, and so forth). Table C-1 lists the locales available when you install the subset, Single-byte European Locales. Additional locales may be available if language-variant software for the operating system is installed on your system. Table C-1: Locale Names
Language Danish German German Greek English English Spanish Finnish French French Country Denmark Switzerland Germany Greece Great Britain United States Spain Finland Belgium Canada Codeset ASCII ASCII Latin-1 Latin-1 Latin-1 Latin-7 Latin-1 Latin-1 Latin-1 Latin-1 Latin-1 Latin-1 Locale Name C POSIX da_DK.ISO8859-1 de_CH.ISO8859-1 de_DE.ISO8859-1 el_GR.ISO8859-7 en_GB.ISO8859-1 en_US.ISO8859-1 es_ES.ISO8859-1 _FI.ISO8859-1 fr_BE.ISO8859-1 fr_CA.ISO8859-1

Using Internationalization Features C7

Table C-1:
Language French French Italian Dutch Dutch Norwegian Portuguese Swedish Turkish

(continued)
Country Switzerland France Italy Belgium The Netherlands Norway Portugal Sweden Turkey Codeset Latin-1 Latin-1 Latin-1 Latin-1 Latin-1 Latin-1 Latin-1 Latin-1 Latin-9 Locale Name fr_CH.ISO8859-1 fr_FR.ISO8859-1 it_IT.ISO8859-1 nl_BE.ISO8859-1 nl_NL.ISO8859-1 no_NO.ISO8859-1 pt_PT.ISO8859-1 sv_SE.ISO8859-1 tr_TR.ISO8859-9

The C locale is the default if no locales are set on your system. The POSIX locale is equivalent to the C locale; only letters in the English alphabet are included in the ASCII codeset that is specied for the POSIX and C locales.

C.4.1

Locale Categories
Table C-2 describes environment variables that inuence locale functions. Table C-2: Environment Variables That Inuence Locale Functions
Variable LC_COLLATE LC_CTYPE LC_MONETARY LC_NUMERIC LC_MESSAGES Description Species the collating sequence to use when sorting strings and when character ranges occur in patterns. Species the character classication (codeset) information. Species monetary formats. Species numeric formats. Species the language in which messages will appear if translations are available. In addition, this variable species strings for afrmative and negative responses. Species date and time formats.

LC_TIME

C8 Using Internationalization Features

Table C-2:
Variable LC_ALL

(continued)
Description Overrides all preceding variables and the LANG environment variable. In general, this variable is used only in programs and should not be set by system managers and users. See the following section on limitations of locale variables for more information.

As is true for the LANG variable, all of the variables in Table C-2 can be assigned locale names. Consider the case where your company is located in the United States but the prevalent language spoken by employees is Spanish. The LANG environment variable could be set to the name of a Spanish language locale and the LC_NUMERIC and LC_MONETARY variables set to the name of a United States English locale. The explicit setting of the LC_NUMERIC and LC_MONETARY variables overrides what they were implicitly set to by LANG. The LC_CTYPE, LC_MESSAGES, LC_TIME, and LC_COLLATE variables would still be implicitly set to the Spanish locale. The following are the variable assignments for the C shell to implement this example:
setenv LANG es_ES.ISO8859-1 setenv LC_NUMERIC en_US.ISO8859-1 setenv LC_MONETARY en_US.ISO8859-1

The following are the same variable assignments for the Bourne and Korn shells:
LANG=es_ES.ISO8859-1 export LANG LC_NUMERIC=en_US.ISO8859-1 export LC_NUMERIC LC_MONETARY=en_US.ISO8859-1 export LC_MONETARY

Sometimes different versions of the same locale are available locally to meet the needs of certain languages or software applications. The names of such locales end with the at sign (@) plus a modier eld. For example, the collating sequence used for the telephone book in some languages is different from the collating sequence used for dictionaries. If the standard locale for a language dened the dictionary collating sequence, another version of the locale might exist to support the telephone book collating sequence. In this case the alternative locale version might have a name like en_FR.ISO8859-1@phone.

Using Internationalization Features C9

C.4.2

Limitations of Locale Settings


The ability to set locale allows you to tailor your environment, but it does not protect you from making mistakes. The following sections discuss problems that can arise when you dene locale variables.

C.4.2.1

Locale Settings Are Not Validated There is nothing to prevent you from dening implausible combinations of locale names for different aspects of a locale. For example, you could set the LANG environment variable to a French locale and the LC_CTYPE variable to a Norwegian locale. The results would probably be undesirable; for example, French message translations would likely contain characters not specied in the Norwegian locale. If you dene locale variables in addition to LANG, you are responsible for ensuring a valid combination of locale settings.

C.4.2.2

File Data Is Not Bound to a Locale The system has no way of knowing what locale was set when a le was created. Therefore, the system cannot prevent you from processing the les data using a different locale. For example, suppose you copy to your system a le that was created when the LANG variable was set to a German locale. If, on your system, LANG is set to a French locale and you use the grep command to search for a string in the le, the grep command will use French collation and pattern matching rules on the German data. It is therefore your responsibility to know what kind of language data a le contains and to set the locale accordingly.

C.4.2.3

Setting LC_ALL Overrides All Other Locale Variables The LC_ALL variable overrides all other locale-dependent environment variables, even if you set it before setting category-specic variables, such as LC_COLLATE. The only way to cancel the inuence of LC_ALL is to undene the variable. For example, enter the command unsetenv LC_ALL. The LC_ALL variable is available for users familiar with the System V environment. In that environment, users set locale either by setting LC_ALL or by setting all the locale category variables individually.

C10 Using Internationalization Features

Customizing Your mailx Session

You can customize your mailx session permanently by including in your .mailrc le any of the settings described in Table D-1. See the unset command in Appendix F for information about temporary settings. Table D-1: Variables for Customizing Your mailx Session
Variable allnet append Type Binary Binary Description All network names with the same login name are treated the same. Saves messages in your mbox le in the order of arrival; the earliest message is the rst message in the le. When this variable is unset, messages are saved in reverse order; the rst message in the le is the most recent. The mailx program runs faster if append is set. Prompts you for a subject line when you send a message. Enter a blank line to send a message with no subject. Prompts you for carbon-copy recipients for each message you send. Automatically displays the next message when you delete the current message. When autoprint is unset, mailx does not display the next message when you delete a message. In either case, the next message becomes your new current message. Enables the special-case treatment of the exclamation point (!) in escape command lines as in vi. Allows the user to specify the default command to be used when using the vertical bar or pipe ( | ) command. Allows the user to specify how to convert UUCP style addresses for sendmail.

ask

Binary

askcc autoprint

Binary Binary

bang

String

cmd

String

conv

String

Table D-1:
Variable crt

(continued)
Type Numeric Description For use with a video display (CRT) terminal. Reads your mail one screenful at a time using the more program. The value tells mailx how many lines of the message to display before invoking the pager. For example: set crt=20 Allows the user to specify a different location for dead.letter. A dead letter will be written to $HOME/dead.letter by default. Displays debugging information. Interprets a period on a line by itself to be the end of a message. Do not unset dot and also set ignoreeof. Species the pathname for the text editor to be used when you use the edit command or the ~e escape. For example: set EDITOR=/usr/ucb/ex If your terminal is a CRT terminal, you can specify a screen editor for this variable. See the VISUAL variable later in this table.

DEAD

String

debug dot

Binary Binary

EDITOR

String

escape

String

Allows you to specify the escape character (the character that starts an escape command when you are in the middle of writing a message). The default is the tilde (~). You must specify a single character. Allows the user to specify the locale to be used when doing character conversion on outgoing mail messages. Species the directory for storing mail folders. A name beginning with a slash, such as /usr/users/hale, is an absolute pathname. A name without an initial slash is a pathname relative to your home directory. For example, the command set folder=folder indicates the directory /usr/users/hale/folder. Prints the message header of messages when mailx is invoked. Prevents messages from being moved to your mbox le after you read them. Messages you have read are held in your system mailbox.

excode

String

folder

String

header hold

Binary Binary

D2 Customizing Your mailx Session

Table D-1:
Variable ignore

(continued)
Type Binary Description Ignores Ctrl/c interrupts, echoing them as at signs (@). Note that this variable is different from the ignore command described in Appendix F. Ignores Ctrl/d as the end of an outgoing message. Do not set ignoreeof and also unset dot. Allows the user to specify a string to be inserted at the beginning of each line of text of a mail message that was included using the ~m command. Allows mailx to truncate your system mailbox instead of deleting it when it is empty. This is useful if you have set special permissions on your system mailbox for security reasons. If keep is unset, your system mailbox is deleted when it becomes empty; the next time it is created, you must reestablish your desired permissions. Prevents deletion of saved messages when you quit mail. Normally, the mailx program marks messages when you save them in other les or folders, and then deletes them from your system mailbox when you leave mailx. Setting keepsave makes mailx leave these messages in your system mailbox. Allows the user to specify the locale to be used for displaying the mail message. Allows the user to specify the command used by the folders commands. Allows the user to specify the location for the mbox folder. The mbox folder will normally be located in $HOME/mbox. Includes you in the list of recipients when you send mail to an alias of which you are a member. If metoo is unset, you will not receive copies of messages sent to aliases of which you are a member. Inhibits display of the header and version identication when you invoke mailx. Prevents mailx from saving aborted messages as dead.letter in your home directory.

ignoreeof indentprefix

Binary String

keep

Binary

keepsave

Binary

lang LISTER MBOX

String String String

metoo

Binary

noheader nosave

Binary Binary

Customizing Your mailx Session D3

Table D-1:
Variable onehop

(continued)
Type Binary Description When responding to a message which contains other recipients, sometimes the addresses of the recipients are relative to the originators address. The onehop option forces the delivery to not follow the path by which the message arrived and deliver it directly, thereby improving performance. Causes mailx to save outgoing mail messages in the directory specied in folder. Causes a form feed to be inserted between messages that are processed by the pipe ( | ) command. Allows the user to specify the paging program of their choice to be used when displaying their messages. For example: PAGER=/usr/bin/more or PAGER=/usr/bin/pg

outfolder page PAGER

Binary Binary String

prompt

String

Allows the user to change the mailx prompt when mailx is invoked. For example: prompt=>>> Supresses printing the version when rst invoked and the message number when you use the type command. Species the name of a le into which mailx will save copies of all outgoing messages. Reverses the function of the reply and Reply commands. Allows the user to save mail messages into dead.letter. Causes mailx to wait until the message has been processed by the mailer. This option can cause some performance degradation from the users point of view since the user will have to wait until the message has been delivered. Allows the user to specify the shell to use when invoking the ~ or ~! commands. Species the number of messages to be displayed in one screenful when you enter the headers command.

quiet

Binary

record Replayall save sendwait

String Binary Binary Binary

SHELL screen

String Numeric

D4 Customizing Your mailx Session

Table D-1:
Variable sendmail

(continued)
Type String Description Species the pathname of the program to use to send mail messages. If this variable is not specied, mailx uses the default delivery system. See your system administrator for information about alternate delivery systems. Displays the recipients name instead of the authors name in message headers. Allows the user to specify a string to be inserted in the mail message when using the ~a command. Allows the user to specify a string to be inserted in the mail message when using the ~A command. Species the number of lines the top command prints; the default is 5. Invokes top in verbose mode; mailx then announces expansion of aliases as messages are sent. Species the pathname for the screen editor that will be used when you use the visual command or the ~v escape. For example: set VISUAL=/usr/ucb/vi If your only terminal is a CRT, you can specify a screen editor for the EDITOR variable, too; then either edit (~e) or visual (~v) will invoke the same editor.

showto sign Sign toplines verbose VISUAL

Binary String String Numeric Binary String

The following example shows the use of the verbose variable, discussed in the previous table, that causes mailx to display expansion of aliases as messages are sent:
& set verbose & mail eve Subject: Meeting this afternoon Enter message. Use Ctrl/d to terminate the letter. Just a reminder, were meeting at 2.
Ctrl/d

Cc: /usr/users/debra/.forward: line 0: debra... forwarding to debra@orange debra... Connecting to .local... debra... Sent

Customizing Your mailx Session D5

Using Escape Commands in Your mailx Session

There is a special set of commands, called escape commands or escapes, that perform functions while you are in the process of writing a message. You use an escape command by entering it on a line by itself, with a tilde (~) as the very rst character. The tilde is called an escape character because it signals mailx that an escape command follows. If you want to type a real tilde as the very rst character on a line in your message, you must type two tildes. Table E-1 describes the escape commands. Table E-1: Escape Commands in mail
Command ~~ ~!command ~? ~:command ~_command Description Allows the user to enter the tilde (~) character in the body of the mail message. Executes the shell command you enter. Prints a brief summary of escape commands. Executes the specied mail command. This is useful for performing housekeeping tasks such as redisplaying a message. For example, entering ~:10 selects and displays message number 10 just as if you had entered its number at the & mail prompt. Inserts the string set in sign into the mail message. Inserts the string set in Sign into the mail message. Inserts the specied names in address_list into the Bcc: (blind carbon copy) list. Adds the specied names to the Cc: (carbon copy) list. Dump core. Includes the le named dead.letter, in your home directory, into the message. Invokes the editor specied by the EDITOR mail variable to edit the message.

~a ~A ~b address_list ~c address_list ~C ~d ~e

Table E-1:
Command

(continued)
Description Reads the current message or the specied messages into your message. Similar to ~f with the difference that all headers will be included regardless of any discard, ignore, or retain commands. Edits the message header elds. This command displays the elds one at a time so you can alter them by adding text to the end, by using the Delete key, or by pressing Ctrl/u to erase the entire eld and then retyping it. Use this command with caution. Insert the value of the named variable into the mail message. For example: ~a is equivalent to ~i sign. Includes the current message or the specied messages, shifted one tab stop to the right. This is useful to set off messages you are forwarding as part of your new message. Similar to ~m with the difference that all headers will be included regardless of any discard, ignore, or retain commands. Displays the message you are composing on your terminal. This is useful to see that the message looks the way you want it to and that it includes the right subject heading and lists of recipients. Reads the named le into the mail message. If the argument begins with an exclamation point (!), the rest of the string is taken as an arbitrary system command and is executed with the standard output inserted into the mail message. Aborts the current message as if you press two Ctrl/C interrupts. Includes the named le in your message. Makes subject the new subject heading, replacing the previous heading. Adds the names to the To: list of your message. Invokes the editor specied by the VISUAL mail variable to edit the message. Writes the message to the named le.

~f [message_list] ~F [message_list]

~h

~i string ~m [message_list]

~M [message_list]

~p

~r file ~< file ~<!shell_cmd

~q ~Q ~r file ~s subject ~t name... ~v ~w file

E2 Using Escape Commands in Your mailx Session

Table E-1:
Command ~|command ~^

(continued)
Description Pipes the message through the named command. This is useful to make global changes in the message; for example, if you are including a message in your new message you can use the sed editor to prex each line with an angle bracket and a space by using the following command: ~|sed s/^/> / You can then add your own text; the result will look like this: > This is the text of the message > you have included. > This is the text you add yourself.

Using Escape Commands in Your mailx Session E3

Using the mailx Commands

The mailx program has a large set of commands, some of which are described in Appendix D and Appendix E. The commands in Table F-1 can help you to use the mailx environment more effectively. The mailx(1) reference page lists some other commands that are useful only under special circumstances. Table F-1: Commands for the mailx Program
Command = # !command -[n] Description Echoes the number of the current message. Allows the user to write comments in mail script les. Executes the shell command you enter. Selects and displays the previous message or the nth previous message. For example, -4 backs up four messages. With no arguments, lists the current aliases. With one argument, displays only that alias. With two or more arguments, creates an alias with the rst argument as its name and all subsequent arguments as the members of the alias. The group command is an alternate for alias. Informs mailx that the addresses listed in alternate_list refer to the user. If no alternate_list is specied in the command, the command displays the current list of alternates. Changes your current directory to the pathname specied, as if you had executed the cd shell command except that the directory you specify with chdir prevails only while you are in the mail environment.

alias alias alias alias alias name... group

alternates [alternate_list]

chdir path cd path

Table F-1:
Command

(continued)
Description Copies the current message or the specied messages into a le. If file exists, the messages are appended. This command works like save except that it does not mark copied messages for deletion when you quit from mailx. Saves the specied messages in a le whose name is derived from the author of the rst message in the message_list. This command will not mark the messages as being saved. Otherwise equivalent to the Save command. Deletes the current message or the specied messages. You can use the undelete command to recover messages you have accidentally deleted. Identical to the ignore subcommand. Deletes the current message and prints the next active message. Echo the given string. Similar to the shell echo command. Invokes the editor specied by EDITOR and loads message_list into the editor. When you exit, any changes made are saved back into message_list. Exits mail without updating your system mailbox. Selects a mail le or folder. If you do not specify a le, this command prints your current path and le name and the number of messages in your current le. If you specify a le or folder, this command displays any changes you have made to your current le and switches to the specied le for reading. Lists the names of the folders in your folder directory.

copy [message_list] file co [message_list] file

Copy [message_list]

delete [message_list] d [message_list] discard [field_list] dp dt echo string edit [message_list]

exit ex x file [file] fi [file] folder [file] fo [file]

folders

F2 Using the mailx Commands

Table F-1:
Command

(continued)
Description Responds to a message and record the response in a le whose name is derived from the author of the message. This command overrides the record option if set. Responds to the rst message in message_list and sends the message to the author of each message in message_list. The subject line is taken from the rst message and the response is recorded in a le whose name is derived from the author of the rst message. Prints the active message header. If you specify a login name, this command prints all the active messages from the specied name. Lists active message headers, using the value of the screen variable as the number of headers to display. See Appendix D for a description of the screen variable. If you have more than one screenful of messages, you can move forward or backward one screenful with the z command. If you specify a message number, the headers command displays the screenful that includes the specied message. Displays help information. Holds, or preserves, the current message or the specied specied messages in your system mailbox instead of moving them to your mbox le. Construction for conditional execution of mailx subcommands. Subcommands following if are executed if condition is True. Subcommands following else are executed if condition is not True. An else is not required but the endif is required. The condition can be send for sending mail, or receive for receiving mail.

followup message

Followup [message_list]

from [login] f [login] headers [n] h [n]

help hold [message_list] ho [message_list] preserve [message_list] pre [message_list] if condition else endif

Using the mailx Commands F3

Table F-1:
Command

(continued)
Description Sets mailx to display messages without the specied elds of the header when you use the print or type command. Note that this command is different from the ignore variable described in Appendix D. If you enter the ignore command with no arguments, the current list of ignored elds is displayed. Displays a list of valid mailx subcommands. List other names for the local host. Sends a message to the specied user. Marks the current message or the specied messages to be moved to your mbox le. This is helpful if you have set the hold variable in your .mailrc le. Displays the messages in message_list using the dened pager program in PAGER. Identical to the page subcommand. Similar to the more subcommand, but also displays the ignored header elds. See more and ignore subcommands. Marks each message in the message_list as not having been read. Identical to New, unread, and Unread subcommands. Marks each message in the message_list as not having been read. Identical to new, unread, and Unread subcommands. Displays the messages in message_list using the dened pager program in PAGER. Identical to the more subcommand. Similar to page but also displays the ignored header elds. Identical to the More subcommand.

ignore [field...]

list local mail user_name m user_name mbox [message_list]

more [message_list]

More [message_list]

new [message_list]

New [message_list]

page [message_list]

Page [message_list]

F4 Using the mailx Commands

Table F-1: (continued) Command pipe [message_list] [shell_command] | [message_list] [shell_command] Description Pipes the message_list through the shell_command. The message is treated as being read. If no arguments are given, the current message is piped through the command given in cmd. If the page option is set, a formfeed is inserted after each message. Displays the next message.

next n +
Return

Print [message] P [message] Type [message] T [message] print [message] p [message] type [message] t [message] quit q

Displays the current message or the specied message, including any header elds specied by the ignore command. Displays the current message or the specied message without any header elds specied by the ignore command. Leaves the mailx program and updates your system mailbox. If you do not have the hold variable set, all messages that you have not deleted, saved, or preserved are moved to your mbox le. If you do have hold set, all these messages will be left in your system mailbox and marked as having been read. Replies to a message. If the original message was addressed to a group of people, replies sent with the Reply command are sent only to the originator of the message. Replies to a message. If the original message was addressed to a group of people, replies sent with the reply and respond commands are sent to everyone who received the original message. Adds the header elds in field_list to the list of headers to be retained when displaying message with the print or type subcommands. Use type and print to view messages in their entirety, including elds that are not retained. If retain is executed with no arguments, it lists the current set of retained elds.

Reply R Respond reply r respond

retain [field_list]

Using the mailx Commands F5

Table F-1: (continued) Command save [message_list] file s [message_list] file Save [message_list] Description Saves the current message or the specied messages in the le. Note that the messages are added to the specied le so that you will not delete the contents of the le. Saves the specied messages in a le whose name is derived from the author of the rst messages. The name of the le is assumed to be the authors name with all network addressing stripped off. If entered with no variables, the set command displays all the options you have set. If you specify a variable, the option will be set. (Appendix E lists the available variables.) Invokes the shell interactively. Reads mail commands from a le (usually .mailrc). Displays the size in lines and characters of the messages in message_list. Displays the rst ve lines in the current message or each of the specied messages. Marks the messages in message_list to be moved from your system mailbox to your personal mbox when you quit the mailx program even though you have not read the listed messages. The messages appear in your mbox as unread messages. When you use touch, the last message in message_list becomes the current message. Deletes the specied alias names. Undeletes the specied messages.

set [variable] se [variable]

shell sh source file so file size [message_list] top [message_list] to [message_list] touch [message_list]

unalias alias_list undelete message_list u message_list unread [message_list] Unread [message_list]

Marks each message in message_list as not having been read. Identical to the new, New, and Unread subcommands. Marks each message in message_list as not having been read. Identical to the new, New, and unread subcommands.

F6 Using the mailx Commands

Table F-1: (continued) Command unset Description Unsets (turns off) options. For example, if your .mailrc le includes a set hold command, you can use the unset command to disable the hold variable for the current mailx session. Displays the version banner for the mailx command. Invokes the editor specied by the VISUAL mail variable to edit the current message. Saves the current message or the specied messages in the named le. This is similar to the save command, except that write saves only the body of each message; the headers are deleted. Moves forward or backward one screenful of messages. You can specify the number of messages in a screenful with the screen variable. (See Appendix D.) To move forward one full screen, enter z or z+; to move backward, enter z-.

version visual write [message_list] file w [message_list] file z[+] z-

Using the mailx Commands F7

Index

Special Characters
! subcommand (ftp), 128 $, 12 %, 12 & operator, 67 && operator, 78 ( ) (parentheses) See parentheses ; (semicolon) See semicolon ? (question mark) See question mark ? subcommand (ftp), 128 { } (braces) See braces || operator, 78 ~ (tilde) See tilde

active processes, 616 add (a) command (vi text editor), A10 ali command in MH, 1120 alias listing in MH, 1120 aliases, 810, 830 C shell, 810, 813 for mail, 1116 Korn shell, 834 seeing current when in mail, 1116 setting in C and Korn shells, 73 ampersand (&) operator background processes, 67 anno command in MH, 1120 append subcommand (ed editor), B2 append text (A) command (vi text editor), A11 apropos command, 110 arguments to commands, 15 ASCII codeset, C2 ascii subcommand (ftp), 125 Asian languages codesets that support, C2 ask variable in mail, 1116 askcc variable in mail, 1117 assign default values to shell, 713

A
absolute pathname, 210 absolute permissions, 511 removing, 511 accessing help, 19 account subcommand (ftp), 125

at (@) extension in locale name, C9 autologin, 11

burst command in MH, 1120 bye subcommand (ftp), 125

B
background putting process in, 614 background process, 66, 67 displaying status for, 610 backslash, 712 to continue a command on the next line, 1116 bg command, 614 bg command (C shell), 813 binary numbers in permissions, 512 binary subcommand (ftp), 125 Bourne shell, 71, 81, 814 built-in commands, 818 built-in variables, 817 clearing variable values, 723 login script, 814 .logout script, 725 metacharacters, 816 pattern matching, 214 .prole login script, 717, 814 redirecting errors, 64 setting variables, 719 braces using to group commands, 711 breaking remote cu connection (UUCP), 148 breaking remote tip connection (UUCP), 1414 built-in commands, 812, 818, 833 variables, 811, 817, 832

C
C locale, C7 C shell, 71, 81, 83 aliases, 73, 810 built-in commands, 812 built-in variables, 811 changing to another shell, 76 clearing variable values, 723 command history, 73, 88 .cshrc login script, 717, 83 displaying value of variables, 722 lename completion, 73, 89 .login script, 717, 85 .logout script, 725 metacharacters, 86 redirecting errors, 65 setting environment variables, 720 stop and start a process, 613 cant remember command name, 111 cancel print jobs, 313 canceling commands, 16 carbon copies in mail, 113 getting a prompt for, 1117 case sensitivity, 26 cat command, 36 cd command, 31, 44 Bourne shell, 819 Korn shell, 834 cd subcommand (ftp), 127 cdup subcommand (ftp), 127 change (c) command (vi text editor), A14 change (c) subcommand (ed editor), B20

Index2

change word (cw) command (vi text editor), A14 changing directories, 31, 44 directory permissions, 510 le permissions, 510 group, 519 identity, 517 name of le during copy command, 322 owners of les and directories, 519 shell permanently, 76 shell temporarily, 75 your password, 18 your shell, 74 character mapping multiple-to-one, C3 one-to-two, C3 characters list of pattern matching, 214 maximum number in lename, 27 quoting to make literal, 711 upper and lower case, 26 chgrp command, 54 chgrp command, 519, 520 chmod command, 513, 53, 54, 58 chown command, 519, 520 clearing variable values, 722 close subcommand (telnet), 134 codesets ASCII, C2 eight-bit, C2 ISO, C2 part of locale, C2 support for Asian languages in, C2

collation, C3 colon use in vi text editor, 23, A3, A18 command argument containing more than one word, 111 command history, 73 C shell, 88 Korn shell, 825 command mode (vi editor), A6 commands alias, 813, 834 apropos, 110 bg, 614, 813 cant remember command name, 111 cat, 36 cd, 31, 44, 819, 834 chgrp, 519, 54 chmod, 54, 58 chown, 519 connecting with pipes, 78 continuing on the next line, 1116 cp, 319, 320 date, 15 df, 314 diff, 324 documentation for, 19 echo, 722, 813, 819, 834 exit, 518 export, 819, 834 fc, 834 fg, 614, 813 le, 330 nd, 67 ags, 15 history, 813, 834 in mailx, F1

Index3

commands (cont.) jobs, 611, 69, 813, 834 kill, 612 ln, 313, 315 login, 11 logout, 813 lpq, 312, 313 lpr, 310, 311 lprm (remove from print queue), 313 lpstat, 313 ls, 213, 318, 32, 323, 33, 34, 55, 56 man, 110, 15, 19 mkdir, 32, 41 more, 315, 36, 38 mv, 322, 323, 410 options, 15 page (pg), 36 passwd, 16, 18 pg, 36 pr, 37 ps, 68, 69 pwd, 27, 819, 834 redirecting output, 327 rehash, 813 repeat, 813 rm, 317, 319, 327, 328, 413 rmdir, 410, 412 run from remote host (UUCP), 1418 running conditionally, 78 running in sequence, 77 running multiple, 77 set, 813, 819, 834 setenv, 720, 813 sort, 326 source, 813

commands (cont.) stopping execution, 16 su, 517 time, 813 times, 819, 834 touch, 329 trap, 819, 834 umask, 513, 819, 834 unalias, 813, 834 unset, 723, 813, 819, 834 unsetenv, 723, 813 use of special characters in, 711 using, 14 w, 615 wc, 63 who, 614 whoami, 517 communicating with remote host (UUCP), 141 comp command in MH, 1120 comparing les, 324 comparison between shell features, 82 completing lenames, 73 computer virus, 521 concatenate les, 37 conditional running of commands, 78 connecting commands with pipes, 78 connecting to an unknown remote host via modem (UUCP), 1410 connecting to an unknown remote system via modem (UUCP), 144 context searching ed editor, B12 vi text editor, A16 continuing a command on the next line, 1116

Index4

copying changing le name during, 322 les from one directory to another, 320 les to other directories, 321 les (UUCP), 1422 les, local host control (UUCP), 1424 lines, ed editor, B23 copying directories, 48 copying les, 319 correcting mistakes in commands, 15 when logging in, 12 correcting typing errors ed editor, B3 vi text editor, A11 cp command, 319 creating directories, 32, 41 empty les, 329 logout script, 724 mail folders, 1113 multiple names for same le, 319 symbolic links, 315 text les ed editor, B2 vi editor, A4 text les with vi, 22 CRT screen use by talk command, 1124 crt variable in mail, 1117 csh.login system login script, 716 .cshrc login script modifying to use MH program, 1117, 717, 83 modifying for System V Habitat, 92

ct command (UUCP) options, connecting to remote host via modem, 1414 Ctrl/C to abort a mail message, 115 Ctrl/D, 518, 519 to end a mail message, 1117 to log out, 14 cu command (UUCP) connect local to remote, 148 options, connecting to a remote host, 143 using local commands, 143, 146 current directory, 27 current message, 119 customizing login scripts, 718 customizing mailx, D1 customizing your mail environment by customizing the mail program, 1115 by setting mail variables, 1116

D
database security, 51 group, 53 date command, 15 date format, C4 dead.letter le, 115 default permission codes, 513, 57 prompts for shells, 75 setting permissions with umask, 513 values to shells, 713 default user mask (umask), 517 dening custom shell variables, 719 login account, 52

Index5

dening (cont.) user environment, 713 delete character (x) command (vi text editor), A13 delete (d) subcommand (ed editor), B18 delete key, 15 delete line (dd) command (vi text editor), A13 delete subcommand (ftp), 127 delete word (dw) command (vi text editor), A13 deleting a specic line ed editor, B19 vi text editor, A13 clearing a line (D) command, A14 current line ed editor, B18 directories, 410 les, 328, 329 multiple lines ed editor, B19 vi text editor, A13 one character at a time, A13 print jobs from queue, 313 determining le type, 330 device name, specifying with cu command (UUCP), 145 diff command, 324 differences between le and directory permissions, 55 differences between les, 324 digests exploding into messages in MH, 1120 directories, 27 changing, 31, 44

directories (cont.) changing owner of, 519 changing permissions, 510 copying, 48 copying les from another directory, 320 creating, 41 denition of, 21 deleting, 410 displaying, 48 displaying current, 27 listing contents of, 32 managing, 41 parent, 210 path, 210 permissions, 55 removing current, 412 removing empty, 411 removing multiple, 412 renaming, 410 root, 29 search path, 723 tree structure, 28 disk partitions, 314 display log of UUCP utilities, 1431 display subcommand (telnet), 134 displaying active processes, 616 command status, 68 current directory name, 27 differences between les, 324 directories, 48 directory contents, 32 directory permissions, 55 disk partitions, 314 le permissions, 55 le type, 330

Index6

displaying (cont.) les, 35 les with formatting, 37 les without formatting, 36 inactive users, 615 inode number of le, 316 multiple les at once, 36 online reference pages, 19 pathnames for all les, 68 print queue status, 312 printer names, 313 process information, 614 process status, 69 user identity, 518 user name, 517 value of variables in C shell, 722 variable name, 712 variable values, 722 who is logged on, 110 who is on the system, 614 dist command in MH, 1120 dot notation, 211 dot option for mail, 1117 double quotes, 713

ed editor (cont.) creating and saving text les, B2 delete (d) subcommand, B18 deleting a specic line, B19 deleting current line, B18 deleting multiple lines, B19 displaying the current line, B9 edit buffer, B1 edit (e) subcommand, B6, B7 edit (ed) command, B7 global (g) operator, B15, B16 insert (i) subcommand, B22 locating text, B12 move (m) subcommand, B19 moving text, B19 print (p) subcommand, B3 quit (q) subcommand, B6 read (r) subcommand, B6, B8 removing characters, B16 replacing character strings, B13 saving part of a le, B5 saving text, B4, B5 starting the editor, B2 substitute (s) subcommand, B13 substitutions on multiple lines, B15 transfer (t) subcommand, B23 using system commands, B24 write (w) subcommand, B4, B6 edit (e) subcommand (ed editor), B6, B7 edit (ed) command (ed editor), B7 editing command lines in the Korn shell, 827 linked les, 313 mail messages, 114 empty les, 329

E
echo command, 722 Bourne shell, 819 C shell, 813 Korn shell, 834 ed editor append subcommand, B2 change (c) subcommand, B20 context searching, B12 copying lines, B23 correcting typing errors, B3

Index7

end of message/conversation (local communications), 1122, 1123 ending a local message, 1122 a mail message, 113 enhanced security system, 11 environment variables, 714 corresponding to locale categories, C8 HOME, 715 LANG, 715 LC_ALL, 715 LC_COLLATE, 715 LC_CTYPE, 715 LC_MESSAGES, 715 LC_MONETARY, 715 LC_NUMERIC, 715 LC_TIME, 715 LOGNAME, 715 MAIL, 715 PATH, 715 SHELL, 715 TERM, 715 TZ, 715 erasing les prevention, 320 errorbells environment variable, A23 errors redirecting output to a le, 64 escape character including in a mail message, E1 tilde as in mail, E1 escape commands in mail, E1 escape key use in vi, A3 /etc/group le, 53

/etc/passwd le, 52, 53 /etc/password le, 713 ex line editor, A18 execute permission, 55, 59 exit command, 518, 519 exiting from the mail program, 1115 export command Bourne shell, 819 Korn shell, 834

F
fc command (Korn shell), 834 fg command, 614 fg command (C shell), 813 le command, 330 le descriptors, 64 le system, 21, 25 le type determining, 330 lename completion, 73, 829, 89 lenames, 25 characters restricted in, 26 les changing identity to access, 517 changing permissions, 510 comparing differences between two, 324 copying, 319 copying (UUCP), 1422 creating empty, 329 denition of, 25 descriptors, 64 determining type, 330 displaying, 36 displaying multiple, 36 displaying pathnames for all, 68 displaying with formatting, 37

Index8

les (cont.) inode number, 316 introduction to, 21 linking, 313 mailing to other users, 116 maximum length, 27 moving, 322 naming conventions, 26 noclobber variable to prevent erasure, 320 printing, 310 protecting, 53 read permission, 59 reading input from, 62 receiving (UUCP), 1421 redirecting errors to, 64 redirecting output, 63 removing, 327 renaming, 322 restricted characters in lename, 26 restricting access, 515 saving mail messages in, 1113 security, 51 security considerations, 520 sending (UUCP), 1421 setting absolute permissions, 511 sorting contents of, 326 specifying with pattern matching, 213 used for security control, 52 wildcard use, 213 ltering standard input, 79 lters, 78 nd command, 67 nger command, 102 ags, 15 folder command in MH, 1120

folder command in mail, 1113 folder variable for mail, 1117 folders command in MH, 1120 folders in mail, 1112, 1120 creating, 1113 listing in MH, 1118, 1120 names of in MH program, 1118 removing in MH, 1120 seeing what your current folder is, 1113 setting up to use, 1112 used by MH program, 1118 foreground putting process in, 614 foreground processes, 66 forget command name, 111 formatting a le, 37 forw command in MH, 1120 forwarding les (UUCP), 1414 forwarding mail messages, 1114 ftp subcommands, 125, 127, 128 full pathname, 210

G
get subcommand (ftp), 125 getting help, 19 global (g) operator (ed editor), B15, B16 global substitution in vi text editor, A20 group le, 51 grouping commands with braces, 711 with parentheses, 711 groups, 53 guidelines for setting password, 17

Index9

H
hard links, 314 help, 19 getting from the mail program, 1114 help command in mail, 1114 help subcommand (ftp), 128 history command C shell, 813 Korn shell, 834 history of recently used commands, 825, 88 HOME environment variable, 715

internationalization (cont.) LC_COLLATE environment variable, 715, C8 LC_CTYPE environment variable, 715, C8 LC_MESSAGES environment variable, 715, C8 LC_MONETARY environment variable, 715, C8 LC_NUMERIC environment variable, 715, C8 LC_TIME environment variable, 715, C8 pattern matching, 214 ISO codesets, C2

I
i-number, 316 I/O, 61 ignorecase environment variable, A23 illegal characters in lenames, 26 inbox folder in MH program, 1118 inc command in MH, 1118, 1120 including mail in MH, 1118, 1120 inline editing in Korn Shell, 74 inode number, 316 and symbolic links, 317 moving les, 322 input reading, 62 input mode (vi text editor), A10 insert (i) subcommand (ed editor), B22 insert text (I) command (vi text editor), A11 insert text (i) command (vi text editor), A10 intermediate hosts used in le transfers (UUCP), 1414 internationalization LANG environment variable, 715, C7 LC_ALL environment variable, 715, C8

J
jobs command, 611, 69 C shell, 813 Korn shell, 834

K
kill command, 612 killing a job or process, 612 Korn shell, 71, 81, 819 aliases, 73, 830 built-in commands, 833 built-in variables, 832 clearing variable values, 723 command history, 73, 825 editing command lines, 827 lename completion, 73, 829 inline editing, 74 .kshrc login script, 717, 822 login script, 820, 822 .logout script, 725

Index10

Korn shell (cont.) metacharacters, 824 pattern matching, 214 .prole login script, 717, 820 redirecting errors, 64 setting variables, 719 .kshrc login script, 717, 822

listing directory contents, 32, 33, 32 listing mail messages, 117 literal characters, 711 using backslash, 712 using double quotes, 713 using single quotes, 712 ln command, 313, 315 local commands (UUCP), 1412, 143, 146, 149 local communication facility, messages sending, write command, 1121 local host control of le access (UUCP) uuto command, 1424 local variables, 716 locale command, C5 locales categories of, C8 determining which locale is set, C5 effect on date and time format, C4 effect on messages, C5 effect on software, C2 effect on sort order, C3 environment variables used with, C6 introduction to, C1 name format for, C6 names of at (@) modier in, C9, C7 numeric and monetary format in, C5 setting, C7 preferred method for, C7 restrictions when, C10 yes/no response strings, C5 locating command names, 110 locating text ed editor, B12 vi text editor, A16

L
LANG environment variable, 715, C7 language part of locale, C1 LC_ALL environment variable, 715, C8 dangers of setting, C10 LC_COLLATE environment variable, 715, C8 LC_CTYPE environment variable, 715, C8 LC_MESSAGES environment variable, 715, C8 LC_MONETARY environment variable, 715, C8 LC_NUMERIC environment variable, 715, C8 LC_TIME environment variable, 715, C8 lcd subcommand (ftp), 127 length maximum for les, 27 linking les, 313, 315 links hard, 314 removing, 317 soft, 314 symbolic, 314 list of pattern matching characters, 214

Index11

logging in See login logging into another user account, 517 logging out, 14 script, 724 login with enhanced security system, 11 login account, 52 login as root user, 519 login directory, 27 .login login script modifying to use MH program, 1117 modifying for System V Habitat, 92 login program, 713 login script activating umask, 516 Bourne shell, 814 C shell, 83, 85 csh.login system script, 716 .cshrc script, 717, 83 Korn shell, 820, 822 .kshrc script, 717, 822 .login script, 717, 85 .prole script, 717, 814, 820 prole system script, 716 login scripts customizing, 718 LOGNAME environment variable, 715 logout command (C shell), 813 logout script, 724, 725 lowercase characters, 26 lpq (display print queue) command, 312 lpr command, 310 lprm (remove from print queue) command, 313

ls command output from -l option, 34 ls (list directory contents) command, 32 ls (list directory) command, 318, 32, 33, 34, 55, 56 ls subcommand (ftp), 127

M
Mail, 111 to 1117 mail aliases, 1116 aliases, listing in MH, 1120 annotating messages in MH, 1120 announcement of new messages on arrival, 117 checking for messages in MH, 1120 composing messages in MH, 1120 compressing messages into a le in MH, 1120 current message, dened, 119 customizing, 1115 deleting messages in MH, 1120 displaying messages in MH, 1120 distributing messages in MH, 1120 editing a message, 114 ending a message, 113 entering a subject for, 1116, 113 exiting from, 1115 exploding digests in MH, 1120 f option to select a folder, 1113 ling messages in MH, 1120 folders, 1112 creating, 1113 setting up to use, 1112 formatted listings of messages in MH, 1120 forwarding messages in, 1114

Index12

mail (cont.) forwarding messages in MH, 1120 handling with the MH message handling program, 1117 getting help in, 1114 including messages in MH asynchronously, 1120, 1118, 1120 listing folders in MH, 1118, 1120 listing message headers, 117, 118 listing messages in MH, 1120 mailing les in, 116 marking messages in MH, 1120 messages moved to mbox le, 1110 notication when messages are waiting for you, 117 preventing moving of messages to mbox le, 1110 reading messages in, 117 by number, 118 reading messages in MH, 1120 removing folders in MH, 1120 removing messages in MH, 1120 replying to messages in, 1110 replying to messages in MH, 1120 reporting recipients of messages in MH, 1120 saving messages in les, 1113 saving messages in folders, 1113 seeing what your current folder is, with the folder command, 1113 selecting messages by content in MH, 1120 sending messages, 112, 113 by carbon copy, 1117, 113 sending messages in MH, 1120 with a prompting front end, 1120 sending messages to aliases, 1116

mail (cont.) setting or listing a folder in MH, 1120 sorting messages in MH, 1120 specifying length of screen for message display, 1117 specifying location of folders for, 1117 specifying location of record copies of outgoing mail, 1117 specifying options interactively, D1 the MH program, 1117, 1121 variables, 1116 to 1117 verbose mode, example of, D5 MAIL environment variable, 715 mailing les, 116 from the shell, 116 .mailrc le modifying to customize mail, 1115, 1112 mailx, 111 to 1117 escape commands, E1 mailx command to enter the mail environment, 117 mailx commands, F1 man command, 15, 19 man pages, 19 managing directories, 41 map command (vi text editor), A23 mark command in MH, 1120 maximum length for le names, 27 mbox le, 1110 preventing moving of messages to, 1110 message annotating in MH, 1120 checking for in MH, 1120 composing in MH, 1120 compressing into les in MH, 1120 deleting in MH, 1120

Index13

message (cont.) displaying a list of in MH, 1118 displaying in MH, 1120 distributing in MH, 1120 exploding digests into in MH, 1120 ling in MH, 1120 formatted listings of in MH, 1120 forwarding in MH, 1120 including in MH asynchronously, 1120, 1118, 1120 listing in MH, 1120 marking in MH, 1120 reading in MH, 1120 removing in MH, 1118, 1120 replying to in MH, 1120 reporting recipients of in MH, 1120 selecting by content in MH, 1120 sending in MH, 1120 with a prompting front end, 1120 sorting in MH, 1120 messages, C5 messages (local communications) ending, end-of-le symbol (EOF), 1122, 1123 long, in les, 129 sending, write command, 1121 metacharacters Bourne shell, 816 C shell, 86 Korn shell, 824 mget subcommand (ftp), 125 MH message handling program, 1117, 1121 annotating messages in, 1120 checking for messages in, 1120 commands used at the shell prompt, 1117 composing messages in, 1120

MH message handling program (cont.) compressing messages into a le in, 1120 deleting messages in, 1120 distributing messages in, 1120 exploding digests in, 1120 ling messages in, 1120 nding if installed on your host, 1117 forwarding messages in, 1120 including messages in asynchronously, 1120, 1120 listing aliases in, 1120 listing folders in, 1120 listing messages in, 1120 marking messages in, 1120 modifying your path to use, 1117 reading messages in, 1118, 1120 removing folders in, 1120 removing messages in, 1118, 1120 replying to messages in, 1120 reporting recipients of messages in, 1120 selecting a folder in, 1118 selecting messages by content in, 1120 sending messages in, 1120 with a prompting front end, 1120 setting or listing a folder in, 1120 sorting messages in, 1120 tailoring features of, 1121 uses folders, 1118 mhl command in MH, 1120 mhmail command in MH, 1120 mkdir command, 32, 41 mkdir subcommand (ftp), 127 monetary formats, C5 representation of fractions, C5 monitoring UUCP, 1432

Index14

more command, 36 used by mail to display messages, 1117, 118 more than one word in command argument, 111 move command, 410 move (m) subcommand (ed editor), B19 moving les, 322, 323 text ed editor, B19 vi text editor, A17 moving directories, 410 mput subcommand (ftp), 125 msgcheck command in MH, 1120 multibyte characters support for in codesets, C2 multiple names for same le, 319 multitasking, 66 mv command, 322, 410, 323

open line (o) command (vi text editor), A11 open previous line (O) command (vi text editor), A11 open subcommand (ftp), 125 options to commands, 15 options, mail specifying interactively, D1 owner changing for les and directories, 519

P
packf command in MH, 1120 page (pg) command, 36 parameter subsitution, 721 parent directory, 210 parentheses using to group commands, 711 passwd command, 16 password le, 51, 52 for logging in, 12 forgotten, 19 restrictions, 18 security restrictions, 17 selecting new, 17 setting, 16 setting with enhanced security system, 11 PATH environment variable, 715 setting for System V habitat, 92 PATH variable, 723 path, modifying to use MH program, 1117 pathname, 210, 28 absolute, 210 dot notation, 211 full, 210 relative, 210

N
naming les, 26 next command in MH, 1120 noignore environment variable, A23 nonumber environment variable, A23 noshowmatch environment variable, A23 number environment variable, A23 numeric formats, C5

O
octal numbers in setting permissions, 511 online documentation, 19

Index15

pathname (cont.) using tilde in, 212 pathname conventions (UUCP), 141 pattern matching, 213 changing le permissions with, 510 internationalized, 214 list of allowable characters, 214 removing multiple les, 328 permission combinations for umask, 514 permissions binary numbers, 512 changing, 53 combinations, 512 directory, 55 read, 59 setting absolute, 511 setting le and directory, 58 setting with octal numbers, 511 specifying with umask, 515 pg command, 36 pick command in MH, 1120 PID number, 67 pipe character, 78 pipes and lters running multiple commands, 77 POSIX locale, C7 pr command, 37 prev command in MH, 1120 prevent erasure of les, 320 print working directory (pwd), 27 print (p) subcommand (ed editor), B3 printer specifying in print command, 310 printer queues, 310, 312

printing les, 310 on a specic printer, 311 on default printer, 311 options, 311 reference pages, 19 Process Identication Number (PID), 67 process identier, 61 processes, 61 displaying active, 616 displaying status, 68, 69 displaying who is running them, 614 grouping commands, 711 killing, 612 resuming, 613 running through pipes, 710 stopping, 611 .prole login script modifying to use MH program, 1118, 717, 814, 820 modifying for System V Habitat, 92 programs types of, 61 prompt question mark as, in the mail program, 117 prompter command in MH, 1120 prompts for shells, 75 protecting les, 53 ps command, 68, 69 public directory (UUCP), 141 put subcommand (ftp), 125 pwd command, 27 Bourne shell, 819 Korn shell, 834 pwd subcommand (ftp), 127

Index16

Q
question mark as mail prompt, 117 queues printer, 310 quit (q) command (vi editor), A4 quit (q) command (vi text editor), A16 quit (q) subcommand (ed editor), B6 quit subcommand (ftp), 125 quit subcommand (telnet), 134 quotes double, 713 single, 712 quoting, 711 backslash, 712 double quotes, 713 single quotes, 712 to display variable names, 712

redirecting errors, 64 input, 62 output, 327, 62, 63 output, of background processes, 67 redirecting both standard errors and output, 65 redirecting errors, 64 Bourne shell, 64 C shell, 65 Korn shell, 64 redirecting input/output, 61 reexecuting commands, 826, 89 reference pages, 19 referencing variables, 720, 721 rele command in MH, 1120 rehash command (C shell), 813 relative pathname, 210 remote commands, running in UUCP, 1414 remote le transfers (UUCP), 1414 remote host running commands from (UUCP), 1418 remote login, 131, 133 remove directory command, 412 removing absolute permissions, 511 characters ed editor, B16 vi text editor, A13 current directory, 412 directories, 410, 411, 412 le links, 317 les, 327, 328 jobs from print queue, 313 links, 317

R
R command in mail, 1110 r command in mail, 1110 r (read) permission, 59 rcvstore command in MH, 1120 read permission, 55 read (r) subcommand (ed editor), B6, B8 reading input, 62 reading input from pipes, 78 reading mail messages, 117 by number, 118 reading messages in MH, 1118 receiving les (UUCP), 1421 record variable for mail, 1117 recv subcommand (ftp), 125

Index17

removing les with verication, 329 rename subcommand (ftp), 127 renaming les and directories, 322 renaming directories, 410 repeat command (C shell), 813 replacing character strings ed editor, B13 vi text editor, A20 reply command in MH, 1120 replying to mail messages, 1110 caution when, 1110 including other recipients in your reply, 1110 reset environment variables, 721 restart a process, 613 Restricted Bourne shell, 71 restricted Bourne shell, 74 restricted characters in le names, 26 restricting le access, 515 restricting user environment restricted Bourne shell, 74 restrictions password, 18 resuming a process, 613 returning to local host during remote connection (UUCP), 1414, 148 rlogin command, 131 rm command, 317, 327, 328 rmdir command, 410, 411, 412 rmdir subcommand (ftp), 127 rmf command in MH, 1120 rmm command in MH, 1120 root directory, 29

root user becoming, 519 dening shell for, 519 tasks performed by, 518 run shell, 728 runique subcommand (ftp), 125 running background processes, 66, 67 commands conditionally, 78 commands in sequence, 77 foreground processes, 66 shell procedures, 727, 728 running commands on a remote host (UUCP), 1418 ruptime command, 105

S
s (set) permission, 59 saving text les in vi, 23 saving mail messages in les, 1113 in folders, 1113 saving part of a le ed editor, B5 vi text editor, A21 saving text ed editor, B2, B4, B5 vi text editor, A16 scan command in MH, 1120 security enhanced system, 11 group, 52 restricted Bourne shell, 74 user, 52

Index18

security considerations, 520 security les, 51 selecting a folder in mail, 1113 semicolon running commands in sequence, 77 send command in MH, 1120 send subcommand (ftp), 125 sending les to remote hosts (UUCP), 1414 les via UUCP, 1421 local messages, 1121 mail messages, 112 sending a long message, 129 sending mail messages, 113 set command Bourne shell, 819 C shell, 813 Korn shell, 834 set user/group ID permission, 59 setenv command, 720 setenv command (C shell), 813 setting absolute permissions, 511 aliases in C and Korn shells, 73 default le permissions, 513 environment variables, 714, 718 environment variables in all shells, 721 environment variables in C shell, 720 le and directory permissions, 58 le permissions, 53 PATH variable, 724 permissions using octal numbers, 511 user mask, 513 setting password with enhanced security system, 11

shell aliases, 810, 830 assign default values to environment, 713 built-in commands, 812, 818, 833 built-in variables, 811, 817, 832 changing, 74 changing permanently, 76 changing temporarily, 75 command history, 825, 88 command interpreter, 14 comparison of features, 82 default prompts, 75 default run shell, 728 default shell, 71 dening custom variables, 719 dening for root user, 519 editing command lines, 827 features, 81 lename completion, 829, 89 login script, 814, 820, 822 mailing les from, 116 metacharacters, 816, 824, 86 program, 13 prompt, 12 restricting users, 74 sample C shell login script, 83 scripts, 726 setting environment variables, 721 using the source command, 1118e special characters, 26, 711 subshells, 72 SHELL environment variable, 715 shell scripts, 726 compatibility with System V, 94 writing, 726

Index19

shell variables, 716 dening custom, 719 show command in MH, 1120 showmatch environment variable, A23 single quotes, 712 soft links, 314 sort command, 326 sort le contents, 326 sort rules, C3 sorting le contents, 326 sortm command in MH, 1120 source command in C shell, 813 to invoke shell options for MH, 1118e special characters, 711 specifying default run shell, 728 les with pattern matching, 213 printer in print command, 310 standard error, 61, 64 standard input, 61, 62 ltering, 79 standard output, 61, 62 starting the ed editor, B2 starting the vi text editor, A4 status information (UUCP), 1427 status subcommand (ftp), 128 status subcommand (telnet), 134 stderr, 62 stdin, 62 stdout, 62 stop and restart a process, 613 stopping a process with Ctrl/C, 611 stopping commands, 16 stopping (killing) a job or process, 612

structure of directories, 28 su command, 517 subcommands (ftp), 125, 127, 128 subdirectories, 27 subject entering for a mail message, 1116, 113 getting a prompt for in mail, 1116 subshells, 711, 72 subsituting parameters, 721 substitute (s) subcommand (ed editor), B13 substitution global, in vi text editor, A20 sunique subcommand (ftp), 125 superuser, 517 superuser authority, 55 tasks, 518 suspend a process, 613 switch to root user, 519 symbolic links, 314 and inode numbers, 317 system administrator responsibilities, 518 system prompts, 75 System V habitat, 91 to 95 accessing, 92 command summary, 94 to 95 location of, 93 modifying .cshrc le for, 92 modifying .login le for, 92 modifying .prole le for, 92

T
tabstop environment variable, A23 talk command use of CRT screen by, 1124

Index20

tasks performed by root user, 518 telephone number, specifying with cu command (UUCP), 145 telnet command, 133 how to use, 133 TERM environment variable, 715 terminating a connection (local communications), 1122, 1123 terminating a job or process, 612 terminating a UUCP job with the uustat command, 1427 terminating remote cu connection (UUCP), 148 terminating remote tip connection (UUCP), 1414 territory part of locale, C2 text editors overview of, 21 tilde in text les, 22 notation in pathname, 212 tilde as escape character in mail, E1 time command (C shell), 813 time format, C4 punctuation in, C4 times command Bourne shell, 819 Korn shell, 834 tip command (UUCP) options, connecting to a remote computer, 148 options, connecting to a remote host, 149 using local commands, 1412, 149 touch command, 329

transfer (t) subcommand (ed editor), B23 transfer-status information (UUCP), 1427 trap command Bourne shell, 819 Korn shell, 834 tree structure, 28 tree structure (le system), 25 types of programs, 61 typing errors, correcting ed editor, B3 vi text editor, A11 TZ environment variable, 715

U
umask permission combinations, 514 umask command, 513 Bourne shell, 819 C shell, 85 Korn shell, 834 unalias command C shell, 813 Korn shell, 834 undo (u) command (vi text editor), A15 UNIX case sensitive, 26 UNIX-to-UNIX Copy Program (UUCP), 141 unset command, 723 Bourne shell, 819 C shell, 813 Korn shell, 834 unsetenv command, 723 unsetenv command (C shell), 813 uppercase characters, 26 user commands summary, 95

Index21

user environment, 713 assign default values, 713 user identity conrming, 518 user mask, 513 activating in login script, 516 default, 517 username, 12 users displaying inactive, 615 displaying who is logged in, 614 using backslash to quote a single character, 712 braces to group commands, 711 commands, 14 delete key to correct mistakes, 15 lters, 78 parentheses to group commands, 711 pipelines, 78 pipes and lters to run multiple commands, 77 tilde in pathname, 212 wildcards in lenames, 213 /usr/spool/uucppublic le (UUCP), 141 UUCP, 141 uulog command (UUCP), 1431 uumonitor command (UUCP), 1432 uustat command (UUCP), 1427 uuto command (UUCP) copying les, local host control, options, 1424 uux command (UUCP), 1418 options, used to run remote commands, 1414

V
variable name displaying, 712 variables clearing values of, 722 displaying values for, 722 in mailx, D1 mail, 1116 to 1117 referencing, 720 shell built-in, 811 variables, shell built-in, 817, 832 verbose subcommand (ftp), 128 vi command, 22 vi editor $ cursor movement command, A8 ( cursor movement command, A8 ) cursor movement command, A8 { cursor movement command, A8 } cursor movement command, A8 0 cursor movement command, A8 b cursor movement command, A7 command mode, A6 Ctrl/B cursor movement command, A8 Ctrl/F cursor movement command, A8 h cursor movement command, A6 j cursor movement command, A6 k cursor movement command, A6 l cursor movement command, A6 used to edit mail messages, 114 moving within a le, A6 quit (q) command, A4 w cursor movement command, A7 vi environment variables errorname, A23 ignorecase, A23 noignorecase, A23

Index22

vi environment variables (cont.) nonumber, A23 noshowmatch, A23 number, A23 showmatch, A23 tabstop, A23 wrapmargin, A23 wrapscan, A23 vi text editor / search command, A16 add (a) command, A10 append text (A) command, A11 change (c) command, A14 change word (cw) command, A14 clearing a line (D) command, A14 context searching, A16 copying text, A18 correcting typing errors, A11 customizing your environment, A22 delete character (x) command, A13 delete line (dd) command, A13 delete word (dw) command, A13 deleting a block of text, A22 environment variables, A23 ex line editor commands, A18 getting started, A2 insert (i) command, A10 insert text (I) command, A11 locating text, A16 map command, A23 moving text, A17 next (n) search command, A17 open line (o) command, A11 open previous line (O) command, A11 opening text les, A4 paste (p) command, A17

vi text editor (cont.) quit (q) command, A16 saving part of a le, A21 saving text les, A16 saving your customizations, A24 searching for text, A16 starting the editor, A4 substituting text, A20 undo (u) command, A15 using advanced techniques, A16 write (w) command, A16 virus, 521

W
w command, 615 w (write) permission, 59 whatnow command in MH, 1120 who command, 614 who is logged on, 110 whoami command, 517 conrming identity, 518 whom command in MH, 1120 wildards removing multiple directories, 412 wildcards, 213 changing le permissions with, 510 use in removing les, 329 working directory, 27 wrapmargin environment variable, A23 wrapscan environment variable, A23 write command (local communications), 1121 write permission, 55, 59 write (w) command (vi text editor), A16 write (w) subcommand (ed editor), B4, B6

Index23

writing logout script, 724 mail messages, E1 shell scripts, 726 shell scripts (example), 727

X
x (execute) permission, 59

Z
z subcommand (telnet), 134

Index24

How to Order Additional Documentation

Technical Support
If you need help deciding which documentation best meets your needs, call 800-DIGITAL (800-344-4825) before placing your electronic, telephone, or direct mail order.

Electronic Orders
To place an order at the Electronic Store, dial 800-234-1998 using a 1200- or 2400-bps modem from anywhere in the USA, Canada, or Puerto Rico. If you need assistance using the Electronic Store, call 800-DIGITAL (800-344-4825).

Telephone and Direct Mail Orders


Your Location Continental USA, Alaska, or Hawaii Puerto Rico Canada Call 800-DIGITAL Contact Digital Equipment Corporation P.O. Box CS2008 Nashua, New Hampshire 03061 Local Digital subsidiary Digital Equipment of Canada Attn: DECdirect Operations KAO2/2 P.O. Box 13000 100 Herzberg Road Kanata, Ontario, Canada K2K 2A6 Local Digital subsidiary or approved distributor SSB Order Processing NQO/V19 or U. S. Software Supply Business Digital Equipment Corporation 10 Cotton Road Nashua, NH 03063-1260

809-754-7575 800-267-6215

International Internala

For internal orders, you must submit an Internal Software Order Form (EN-01740-07).

Readers Comments

Digital UNIX Command and Shell Users Guide


AA-PS2HD-TE

Digital welcomes your comments and suggestions on this manual. Your input will help us to write documentation that meets your needs. Please send your suggestions using one of the following methods: This postage-paid form Internet electronic mail: [email protected] Fax: (603) 881-0120, Attn: UEG Publications, ZKO3-3/Y32 If you are not using this form, please be sure you include the name of the document, the page number, and the product name and version. Please rate this manual:
Accuracy (software works as manual says) Completeness (enough information) Clarity (easy to understand) Organization (structure of subject matter) Figures (useful) Examples (useful) Index (ability to nd topic) Usability (ability to access information quickly)

Excellent

Good

Fair

Poor

Please list errors you have found in this manual: Page Description

Additional comments or suggestions to improve this manual:

What version of the software described by this manual are you using? Name/Title Company Mailing Address Email Dept. Date Phone

Do Not Cut or Tear Fold Here and Tape


TM

NO POSTAGE NECESSARY IF MAILED IN THE UNITED STATES

BUSINESSPERMIT NO. 33 MAYNARD MA REPLY MAIL FIRSTCLASS MAIL


POSTAGE WILL BE PAID BY ADDRESSEE

DIGITAL EQUIPMENT CORPORATION UEG PUBLICATIONS MANAGER ZKO33/Y32 110 SPIT BROOK RD NASHUA NH 030629987

Do Not Cut or Tear Fold Here

Cut on Dotted Line

You might also like