Annotation of src/share/doc/usd/01.begin/u2, Revision 1.1
1.1 ! mjf 1: .\" $NetBSD$
! 2: .\"
! 3: .\" Copyright (C) Caldera International Inc. 2001-2002.
! 4: .\" All rights reserved.
! 5: .\"
! 6: .\" Redistribution and use in source and binary forms, with or without
! 7: .\" modification, are permitted provided that the following conditions
! 8: .\" are met:
! 9: .\" 1. Redistributions of source code and documentation must retain the above
! 10: .\" copyright notice, this list of conditions and the following disclaimer.
! 11: .\" 2. Redistributions in binary form must reproduce the above copyright
! 12: .\" notice, this list of conditions and the following disclaimer in the
! 13: .\" documentation and/or other materials provided with the distribution.
! 14: .\" 3. All advertising materials mentioning features or use of this software
! 15: .\" must display the following acknowledgement:
! 16: .\" This product includes software developed or owned by Caldera
! 17: .\" International, Inc.
! 18: .\" 4. Neither the name of Caldera International, Inc. nor the names of other
! 19: .\" contributors may be used to endorse or promote products derived from
! 20: .\" this software without specific prior written permission.
! 21: .\"
! 22: .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
! 23: .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
! 24: .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
! 25: .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
! 26: .\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
! 27: .\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
! 28: .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
! 29: .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 30: .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
! 31: .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
! 32: .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
! 33: .\" POSSIBILITY OF SUCH DAMAGE.
! 34: .\"
! 35: .\" @(#)u2 8.1 (Berkeley) 6/8/93
! 36: .\"
! 37: .SH
! 38: II. DAY-TO-DAY USE
! 39: .SH
! 40: Creating Files \(em The Editor
! 41: .PP
! 42: If you have to type a paper or a letter or a program,
! 43: how do you get the information stored in the machine?
! 44: Most of these tasks are done with
! 45: the
! 46: .UC UNIX
! 47: ``text editor''
! 48: .UL ed .
! 49: Since
! 50: .UL ed
! 51: is thoroughly documented in
! 52: .UL ed (1)
! 53: and explained in
! 54: .ul
! 55: A Tutorial Introduction to the UNIX Text Editor,
! 56: we won't spend any time here describing how to use it.
! 57: All we want it for right now is to make some
! 58: .ul
! 59: files.
! 60: (A file is just a collection of information stored in the machine,
! 61: a simplistic but adequate definition.)
! 62: .PP
! 63: To create a file
! 64: called
! 65: .UL junk
! 66: with some text in it, do the following:
! 67: .P1
! 68: .ta .65i
! 69: ed junk \fR(invokes the text editor)\f3
! 70: a \fR(command to ``ed'', to add text)\f3
! 71: .ft I
! 72: now type in
! 73: whatever text you want ...
! 74: .ft 3
! 75: \&. \fR(signals the end of adding text)\f3
! 76: .P2
! 77: The ``\f3.\fR'' that signals the end of adding text must be
! 78: at the beginning of a line by itself.
! 79: Don't forget it,
! 80: for until it is typed,
! 81: no other
! 82: .UL ed
! 83: commands will be recognized \(em
! 84: everything you type will be treated as text to be added.
! 85: .PP
! 86: At this point you can do various editing operations
! 87: on the text you typed in, such as correcting spelling mistakes,
! 88: rearranging paragraphs and the like.
! 89: Finally, you must write the information you have typed
! 90: into a file with the editor command
! 91: .UL w :
! 92: .P1
! 93: w
! 94: .P2
! 95: .UL ed
! 96: will respond with the number of characters it wrote
! 97: into the file
! 98: .UL junk .
! 99: .PP
! 100: Until the
! 101: .UL w
! 102: command,
! 103: nothing is stored permanently,
! 104: so if you hang up and go home
! 105: the information is lost.\(dg
! 106: .FS
! 107: \(dg This is not strictly true \(em
! 108: if you hang up while editing, the data you were
! 109: working on is saved in a file called
! 110: .UL ed.hup ,
! 111: which you can continue with at your next session.
! 112: .FE
! 113: But after
! 114: .UL w
! 115: the information is there permanently;
! 116: you can re-access it any time by typing
! 117: .P1
! 118: ed junk
! 119: .P2
! 120: Type a
! 121: .UL q
! 122: command
! 123: to quit the editor.
! 124: (If you try to quit without writing,
! 125: .UL ed
! 126: will print a
! 127: .UL ?
! 128: to remind you.
! 129: A second
! 130: .UL q
! 131: gets you out regardless.)
! 132: .PP
! 133: Now create a second file called
! 134: .UL temp
! 135: in the same manner.
! 136: You should now have two files,
! 137: .UL junk
! 138: and
! 139: .UL temp .
! 140: .SH
! 141: What files are out there?
! 142: .PP
! 143: The
! 144: .UL ls
! 145: (for ``list'') command lists the names
! 146: (not contents)
! 147: of any of the files that
! 148: .UC UNIX
! 149: knows about.
! 150: If you type
! 151: .P1
! 152: ls
! 153: .P2
! 154: the response will be
! 155: .P1
! 156: junk
! 157: temp
! 158: .P2
! 159: which are indeed the two files just created.
! 160: The names are sorted into alphabetical order automatically,
! 161: but other variations are possible.
! 162: For example,
! 163: the command
! 164: .P1
! 165: ls -t
! 166: .P2
! 167: causes the files to be listed in the order in which they were last changed,
! 168: most recent first.
! 169: The
! 170: .UL \-l
! 171: option gives a ``long'' listing:
! 172: .P1
! 173: ls -l
! 174: .P2
! 175: will produce something like
! 176: .P1
! 177: -rw-rw-rw- 1 bwk users 41 Jul 22 2:56 junk
! 178: -rw-rw-rw- 1 bwk users 78 Jul 22 2:57 temp
! 179: .P2
! 180: The date and time are of the last change to the file.
! 181: The 41 and 78 are the number of characters
! 182: (which should agree with the numbers you got from
! 183: .UL ed ).
! 184: .UL bwk
! 185: is the owner of the file, that is, the person
! 186: who created it.
! 187: .UL users
! 188: is the name of the file's group.
! 189: The
! 190: .UL \-rw\-rw\-rw\-
! 191: tells who has permission to read and write the file,
! 192: in this case everyone.
! 193: .PP
! 194: Options can be combined:
! 195: .UL ls\ \-lt
! 196: gives the same thing as
! 197: .UL ls\ \-l ,
! 198: but sorted into time order.
! 199: You can also name the files you're interested in,
! 200: and
! 201: .UL ls
! 202: will list the information about them only.
! 203: More details can be found in
! 204: .UL ls (1).
! 205: .PP
! 206: The use of optional arguments that begin with a minus sign,
! 207: like
! 208: .UL \-t
! 209: and
! 210: .UL \-lt ,
! 211: is a common convention for
! 212: .UC UNIX
! 213: programs.
! 214: In general, if a program accepts such optional arguments,
! 215: they precede any filename arguments.
! 216: It is also vital that you separate the various arguments with spaces:
! 217: .UL ls\-l
! 218: is not the same as
! 219: .UL ls\ \ \-l .
! 220: .SH
! 221: Printing Files
! 222: .PP
! 223: Now that you've got a file of text,
! 224: how do you print it so people can look at it?
! 225: There are a host of programs that do that,
! 226: probably more than are needed.
! 227: .PP
! 228: One simple thing is to use the editor,
! 229: since printing is often done just before making changes anyway.
! 230: You can say
! 231: .P1
! 232: ed junk
! 233: 1,$p
! 234: .P2
! 235: .UL ed
! 236: will reply with the count of the characters in
! 237: .UL junk
! 238: and then print all the lines in the file.
! 239: After you learn how to use the editor,
! 240: you can be selective about the parts you print.
! 241: .PP
! 242: There are times when it's not feasible to use the editor for printing.
! 243: For example, there is a limit on how big a file
! 244: .UL ed
! 245: can handle
! 246: (several thousand lines).
! 247: Secondly,
! 248: it
! 249: will only print one file at a time,
! 250: and sometimes you want to print several, one after another.
! 251: So here are a couple of alternatives.
! 252: .PP
! 253: First is
! 254: .UL cat ,
! 255: the simplest of all the printing programs.
! 256: .UL cat
! 257: simply prints on the terminal the contents of all the files
! 258: named in a list.
! 259: Thus
! 260: .P1
! 261: cat junk
! 262: .P2
! 263: prints one file, and
! 264: .P1
! 265: cat junk temp
! 266: .P2
! 267: prints two.
! 268: The files are simply concatenated (hence the name
! 269: .UL cat '') ``
! 270: onto the terminal.
! 271: .PP
! 272: .UL pr
! 273: produces formatted printouts of files.
! 274: As with
! 275: .UL cat ,
! 276: .UL pr
! 277: prints all the files named in a list.
! 278: The difference is that it produces
! 279: headings with date, time, page number and file name
! 280: at the top of each page,
! 281: and
! 282: extra lines to skip over the fold in the paper.
! 283: Thus,
! 284: .P1
! 285: pr junk temp
! 286: .P2
! 287: will print
! 288: .UL junk
! 289: neatly,
! 290: then skip to the top of a new page and print
! 291: .UL temp
! 292: neatly.
! 293: .PP
! 294: .UL pr
! 295: can also produce multi-column output:
! 296: .P1
! 297: pr -3 junk
! 298: .P2
! 299: prints
! 300: .UL junk
! 301: in 3-column format.
! 302: You can use any reasonable number in place of ``3''
! 303: and
! 304: .UL pr
! 305: will do its best.
! 306: .UL pr
! 307: has other capabilities as well;
! 308: see
! 309: .UL pr (1).
! 310: .PP
! 311: It should be noted that
! 312: .UL pr
! 313: is
! 314: .ul
! 315: not
! 316: a formatting program in the sense of shuffling lines around
! 317: and justifying margins.
! 318: The true formatters are
! 319: .UL nroff
! 320: and
! 321: .UL troff ,
! 322: which we will get to in the section on document preparation.
! 323: .PP
! 324: There are also programs that print files
! 325: on a high-speed printer.
! 326: Look in your manual under
! 327: .UL lpr .
! 328: .SH
! 329: Shuffling Files About
! 330: .PP
! 331: Now that you have some files in the file system
! 332: and some experience in printing them,
! 333: you can try bigger things.
! 334: For example,
! 335: you can move a file from one place to another
! 336: (which amounts to giving it a new name),
! 337: like this:
! 338: .P1
! 339: mv junk precious
! 340: .P2
! 341: This means that what used to be ``junk'' is now ``precious''.
! 342: If you do an
! 343: .UL ls
! 344: command now,
! 345: you will get
! 346: .P1
! 347: precious
! 348: temp
! 349: .P2
! 350: Beware that if you move a file to another one
! 351: that already exists,
! 352: the already existing contents are lost forever.
! 353: .PP
! 354: If you want
! 355: to make a
! 356: .ul
! 357: copy
! 358: of a file (that is, to have two versions of something),
! 359: you can use the
! 360: .UL cp
! 361: command:
! 362: .P1
! 363: cp precious temp1
! 364: .P2
! 365: makes a duplicate copy of
! 366: .UL precious
! 367: in
! 368: .UL temp1 .
! 369: .PP
! 370: Finally, when you get tired of creating and moving
! 371: files,
! 372: there is a command to remove files from the file system,
! 373: called
! 374: .UL rm .
! 375: .P1
! 376: rm temp temp1
! 377: .P2
! 378: will remove both of the files named.
! 379: .PP
! 380: You will get a warning message if one of the named files wasn't there,
! 381: but otherwise
! 382: .UL rm ,
! 383: like most
! 384: .UC UNIX
! 385: commands,
! 386: does its work silently.
! 387: There is no prompting or chatter,
! 388: and error messages are occasionally curt.
! 389: This terseness is sometimes disconcerting
! 390: to new\%comers,
! 391: but experienced users find it desirable.
! 392: .SH
! 393: What's in a Filename
! 394: .PP
! 395: So far we have used filenames without ever saying what's
! 396: a legal name,
! 397: so it's time for a couple of rules.
! 398: First, filenames are limited to 14 characters,
! 399: which is enough to be descriptive.\(dg
! 400: .FS
! 401: \(dg In 4.2 BSD the limit was extended to 255 characters.
! 402: .FE
! 403: Second, although you can use almost any character
! 404: in a filename,
! 405: common sense says you should stick to ones that are visible,
! 406: and that you should probably avoid characters that might be used
! 407: with other meanings.
! 408: We have already seen, for example,
! 409: that in the
! 410: .UL ls
! 411: command,
! 412: .UL ls\ \-t
! 413: means to list in time order.
! 414: So if you had a file whose name
! 415: was
! 416: .UL \-t ,
! 417: you would have a tough time listing it by name.
! 418: Besides the minus sign, there are other characters which
! 419: have special meaning.
! 420: To avoid pitfalls,
! 421: you would do well to
! 422: use only letters, numbers and the period
! 423: until you're familiar with the situation.
! 424: .PP
! 425: On to some more positive suggestions.
! 426: Suppose you're typing a large document
! 427: like a book.
! 428: Logically this divides into many small pieces,
! 429: like chapters and perhaps sections.
! 430: Physically it must be divided too,
! 431: for
! 432: .UL ed
! 433: will not handle really big files.
! 434: Thus you should type the document as a number of files.
! 435: You might have a separate file for each chapter,
! 436: called
! 437: .P1
! 438: chap1
! 439: chap2
! 440: .ft R
! 441: etc...
! 442: .P2
! 443: Or, if each chapter were broken into several files, you might have
! 444: .P1
! 445: chap1.1
! 446: chap1.2
! 447: chap1.3
! 448: \&...
! 449: chap2.1
! 450: chap2.2
! 451: \&...
! 452: .P2
! 453: You can now tell at a glance where a particular file fits into the whole.
! 454: .PP
! 455: There are advantages to a systematic naming convention which are not obvious
! 456: to the novice
! 457: .UC UNIX
! 458: user.
! 459: What if you wanted to print the whole book?
! 460: You could say
! 461: .P1
! 462: pr chap1.1 chap1.2 chap1.3 ......
! 463: .P2
! 464: but you would get tired pretty fast, and would probably even make mistakes.
! 465: Fortunately, there is a shortcut.
! 466: You can say
! 467: .P1
! 468: pr chap*
! 469: .P2
! 470: The
! 471: .UL *
! 472: means ``anything at all,''
! 473: so this translates into ``print all files
! 474: whose names begin with
! 475: .UL chap '',
! 476: listed in alphabetical order.
! 477: .PP
! 478: This shorthand notation
! 479: is not a property of the
! 480: .UL pr
! 481: command, by the way.
! 482: It is system-wide, a service of the program
! 483: that interprets commands
! 484: (the ``shell,''
! 485: .UL sh (1)).
! 486: Using that fact, you can see how to list the names of the files in the book:
! 487: .P1
! 488: ls chap*
! 489: .P2
! 490: produces
! 491: .P1
! 492: chap1.1
! 493: chap1.2
! 494: chap1.3
! 495: \&...
! 496: .P2
! 497: The
! 498: .UL *
! 499: is not limited to the last position in a filename \(em
! 500: it can be anywhere
! 501: and can occur several times.
! 502: Thus
! 503: .P1
! 504: rm *junk* *temp*
! 505: .P2
! 506: removes all files that contain
! 507: .UL junk
! 508: or
! 509: .UL temp
! 510: as any part of their name.
! 511: As a special case,
! 512: .UL *
! 513: by itself matches every filename,
! 514: so
! 515: .P1
! 516: pr *
! 517: .P2
! 518: prints all your files
! 519: (alphabetical order),
! 520: and
! 521: .P1
! 522: rm *
! 523: .P2
! 524: removes
! 525: .ul
! 526: all files.
! 527: (You had better be
! 528: .IT very
! 529: sure that's what you wanted to say!)
! 530: .PP
! 531: The
! 532: .UL *
! 533: is not
! 534: the only pattern-matching feature available.
! 535: Suppose you want to print only chapters 1 through 4 and 9.
! 536: Then you can say
! 537: .P1
! 538: pr chap[12349]*
! 539: .P2
! 540: The
! 541: .UL [...]
! 542: means to match any of the characters inside the brackets.
! 543: A range of consecutive letters or digits can be abbreviated,
! 544: so you can also do this
! 545: with
! 546: .P1
! 547: pr chap[1-49]*
! 548: .P2
! 549: Letters can also be used within brackets:
! 550: .UL [a\-z]
! 551: matches any character in the range
! 552: .UL a
! 553: through
! 554: .UL z .
! 555: .PP
! 556: The
! 557: .UL ?
! 558: pattern matches any single character,
! 559: so
! 560: .P1
! 561: ls ?
! 562: .P2
! 563: lists all files which have single-character names,
! 564: and
! 565: .P1
! 566: ls -l chap?.1
! 567: .P2
! 568: lists information about the first file of each chapter
! 569: .UL chap1.1 \&, (
! 570: .UL chap2.1 ,
! 571: etc.).
! 572: .PP
! 573: Of these niceties,
! 574: .UL *
! 575: is certainly the most useful,
! 576: and you should get used to it.
! 577: The others are frills, but worth knowing.
! 578: .PP
! 579: If you should ever have to turn off the special meaning
! 580: of
! 581: .UL * ,
! 582: .UL ? ,
! 583: etc.,
! 584: enclose the entire argument in single quotes,
! 585: as in
! 586: .P1
! 587: ls \(fm?\(fm
! 588: .P2
! 589: We'll see some more examples of this shortly.
! 590: .SH
! 591: What's in a Filename, Continued
! 592: .PP
! 593: When you first made that file called
! 594: .UL junk ,
! 595: how did
! 596: the system
! 597: know that there wasn't another
! 598: .UL junk
! 599: somewhere else,
! 600: especially since the person in the next office is also
! 601: reading this tutorial?
! 602: The answer is that generally each user
! 603: has a private
! 604: .IT directory ,
! 605: which contains only the files that belong to him.
! 606: When you log in, you are ``in'' your directory.
! 607: Unless you take special action,
! 608: when you create a new file,
! 609: it is made in the directory that you are currently in;
! 610: this is most often your own directory,
! 611: and thus the file is unrelated to any other file of the same name
! 612: that might exist in someone else's directory.
! 613: .PP
! 614: The set of all files
! 615: is organized into a (usually big) tree,
! 616: with your files located several branches into the tree.
! 617: It is possible for you to ``walk'' around this tree,
! 618: and to find any file in the system, by starting at the root
! 619: of the tree and walking along the proper set of branches.
! 620: Conversely, you can start where you are and walk toward the root.
! 621: .PP
! 622: Let's try the latter first.
! 623: The basic tools is the command
! 624: .UL pwd
! 625: (``print working directory''),
! 626: which prints the name of the directory you are currently in.
! 627: .PP
! 628: Although the details will vary according to the system you are on,
! 629: if you give the
! 630: command
! 631: .UL pwd ,
! 632: it will print something like
! 633: .P1
! 634: /usr/your\(hyname
! 635: .P2
! 636: This says that you are currently in the directory
! 637: .UL your-name ,
! 638: which is in turn in the directory
! 639: .UL /usr ,
! 640: which is in turn in the root directory
! 641: called by convention just
! 642: .UL / .
! 643: (Even if it's not called
! 644: .UL /usr
! 645: on your system,
! 646: you will get something analogous.
! 647: Make the corresponding mental adjustment and read on.)
! 648: .PP
! 649: If you now type
! 650: .P1
! 651: ls /usr/your\(hyname
! 652: .P2
! 653: you should get exactly the same list of file names
! 654: as you get from a plain
! 655: .UL ls :
! 656: with no arguments,
! 657: .UL ls
! 658: lists the contents of the current directory;
! 659: given the name of a directory,
! 660: it lists the contents of that directory.
! 661: .PP
! 662: Next, try
! 663: .P1
! 664: ls /usr
! 665: .P2
! 666: This should print a long series of names,
! 667: among which is your own login name
! 668: .UL your-name .
! 669: On many systems,
! 670: .UL usr
! 671: is a directory that contains the directories
! 672: of all the normal users of the system,
! 673: like you.
! 674: .PP
! 675: The next step is to try
! 676: .P1
! 677: ls /
! 678: .P2
! 679: You should get a response something like this
! 680: (although again the details may be different):
! 681: .P1
! 682: bin
! 683: dev
! 684: etc
! 685: lib
! 686: tmp
! 687: usr
! 688: .P2
! 689: This is a collection of the basic directories of files
! 690: that
! 691: the system
! 692: knows about;
! 693: we are at the root of the tree.
! 694: .PP
! 695: Now try
! 696: .P1
! 697: cat /usr/your\(hyname/junk
! 698: .P2
! 699: (if
! 700: .UL junk
! 701: is still around in your directory).
! 702: The name
! 703: .P1
! 704: /usr/your\(hyname/junk
! 705: .P2
! 706: is called the
! 707: .UL pathname
! 708: of the file that
! 709: you normally think of as ``junk''.
! 710: ``Pathname'' has an obvious meaning:
! 711: it represents the full name of the path you have to follow from the root
! 712: through the tree of directories to get to a particular file.
! 713: It is a universal rule in
! 714: the
! 715: .UC UNIX
! 716: system
! 717: that anywhere you can use an ordinary filename,
! 718: you can use a pathname.
! 719: .PP
! 720: Here is a picture which may make this clearer:
! 721: .P1 1
! 722: .ft R
! 723: .if t .vs 9p
! 724: .if t .tr /\(sl
! 725: .if t .tr ||
! 726: .ce 100
! 727: (root)
! 728: / | \e
! 729: / | \e
! 730: / | \e
! 731: bin etc usr dev tmp
! 732: / | \e / | \e / | \e / | \e / | \e
! 733: / | \e
! 734: / | \e
! 735: adam eve mary
! 736: / / \e \e
! 737: / \e junk
! 738: junk temp
! 739: .ce 0
! 740: .br
! 741: .tr //
! 742: .P2
! 743: .LP
! 744: Notice that Mary's
! 745: .UL junk
! 746: is unrelated to Eve's.
! 747: .PP
! 748: This isn't too exciting if all the files of interest are in your own
! 749: directory, but if you work with someone else
! 750: or on several projects concurrently,
! 751: it becomes handy indeed.
! 752: For example, your friends can print your book by saying
! 753: .P1
! 754: pr /usr/your\(hyname/chap*
! 755: .P2
! 756: Similarly, you can find out what files your neighbor has
! 757: by saying
! 758: .P1
! 759: ls /usr/neighbor\(hyname
! 760: .P2
! 761: or make your own copy of one of his files by
! 762: .P1
! 763: cp /usr/your\(hyneighbor/his\(hyfile yourfile
! 764: .P2
! 765: .PP
! 766: If your neighbor doesn't want you poking around in his files,
! 767: or vice versa,
! 768: privacy can be arranged.
! 769: Each file and directory has read-write-execute permissions for the owner,
! 770: a group, and everyone else,
! 771: which can be set
! 772: to control access.
! 773: See
! 774: .UL ls (1)
! 775: and
! 776: .UL chmod (1)
! 777: for details.
! 778: As a matter of observed fact,
! 779: most users most of the time find openness of more
! 780: benefit than privacy.
! 781: .PP
! 782: As a final experiment with pathnames, try
! 783: .P1
! 784: ls /bin /usr/bin
! 785: .P2
! 786: Do some of the names look familiar?
! 787: When you run a program, by typing its name after the prompt character,
! 788: the system simply looks for a file of that name.
! 789: It normally looks first in your directory
! 790: (where it typically doesn't find it),
! 791: then in
! 792: .UL /bin
! 793: and finally in
! 794: .UL /usr/bin .
! 795: There is nothing magic about commands like
! 796: .UL cat
! 797: or
! 798: .UL ls ,
! 799: except that they have been collected into a couple of places to be easy to find and administer.
! 800: .PP
! 801: What if you work regularly with someone else on common information
! 802: in his directory?
! 803: You could just log in as your friend each time you want to,
! 804: but you can also say
! 805: ``I want to work on his files instead of my own''.
! 806: This is done by changing the directory that you are
! 807: currently in:
! 808: .P1
! 809: cd /usr/your\(hyfriend
! 810: .P2
! 811: (On some systems,
! 812: .UL cd
! 813: is spelled
! 814: .UL chdir .)
! 815: Now when you use a filename in something like
! 816: .UL cat
! 817: or
! 818: .UL pr ,
! 819: it refers to the file in your friend's directory.
! 820: Changing directories doesn't affect any permissions associated
! 821: with a file \(em
! 822: if you couldn't access a file from your own directory,
! 823: changing to another directory won't alter that fact.
! 824: Of course,
! 825: if you forget what directory you're in, type
! 826: .P1
! 827: pwd
! 828: .P2
! 829: to find out.
! 830: .PP
! 831: It is usually convenient to arrange your own files
! 832: so that all the files related to one thing are in a directory separate
! 833: from other projects.
! 834: For example, when you write your book, you might want to keep all the text
! 835: in a directory called
! 836: .UL book .
! 837: So make one with
! 838: .P1
! 839: mkdir book
! 840: .P2
! 841: then go to it with
! 842: .P1
! 843: cd book
! 844: .P2
! 845: then start typing chapters.
! 846: The book is now found in (presumably)
! 847: .P1
! 848: /usr/your\(hyname/book
! 849: .P2
! 850: To remove the directory
! 851: .UL book ,
! 852: type
! 853: .P1
! 854: rm book/*
! 855: rmdir book
! 856: .P2
! 857: The first command removes all files from the directory;
! 858: the second
! 859: removes the empty directory.
! 860: .PP
! 861: You can go up one level in the tree of files
! 862: by saying
! 863: .P1
! 864: cd ..
! 865: .P2
! 866: .UL .. '' ``
! 867: is the name of the parent of whatever directory you are currently in.
! 868: For completeness,
! 869: .UL . '' ``
! 870: is an alternate name
! 871: for the directory you are in.
! 872: .SH
! 873: Using Files instead of the Terminal
! 874: .PP
! 875: Most of the commands we have seen so far produce output
! 876: on the terminal;
! 877: some, like the editor, also take their input from the terminal.
! 878: It is universal in
! 879: .UC UNIX
! 880: systems
! 881: that the terminal can be replaced by a file
! 882: for either or both of input and output.
! 883: As one example,
! 884: .P1
! 885: ls
! 886: .P2
! 887: makes a list of files on your terminal.
! 888: But if you say
! 889: .P1
! 890: ls >filelist
! 891: .P2
! 892: a list of your files will be placed in the file
! 893: .UL filelist
! 894: (which
! 895: will be created if it doesn't already exist,
! 896: or overwritten if it does).
! 897: The symbol
! 898: .UL >
! 899: means ``put the output on the following file,
! 900: rather than on the terminal.''
! 901: Nothing is produced on the terminal.
! 902: As another example, you could combine
! 903: several files into one by capturing the output of
! 904: .UL cat
! 905: in a file:
! 906: .P1
! 907: cat f1 f2 f3 >temp
! 908: .P2
! 909: .PP
! 910: The symbol
! 911: .UL >>
! 912: operates very much like
! 913: .UL >
! 914: does,
! 915: except that it means
! 916: ``add to the end of.''
! 917: That is,
! 918: .P1
! 919: cat f1 f2 f3 >>temp
! 920: .P2
! 921: means to concatenate
! 922: .UL f1 ,
! 923: .UL f2
! 924: and
! 925: .UL f3
! 926: to the end of whatever is already in
! 927: .UL temp ,
! 928: instead of overwriting the existing contents.
! 929: As with
! 930: .UL > ,
! 931: if
! 932: .UL temp
! 933: doesn't exist, it will be created for you.
! 934: .PP
! 935: In a similar way, the symbol
! 936: .UL <
! 937: means to take the input
! 938: for a program from the following file,
! 939: instead of from the terminal.
! 940: Thus, you could make up a script of commonly used editing commands
! 941: and put them into a file called
! 942: .UL script .
! 943: Then you can run the script on a file by saying
! 944: .P1
! 945: ed file <script
! 946: .P2
! 947: As another example, you can use
! 948: .UL ed
! 949: to prepare a letter in file
! 950: .UL let ,
! 951: then send it to several people with
! 952: .P1
! 953: mail adam eve mary joe <let
! 954: .P2
! 955: .SH
! 956: Pipes
! 957: .PP
! 958: One of the novel contributions of
! 959: the
! 960: .UC UNIX
! 961: system
! 962: is the idea of a
! 963: .ul
! 964: pipe.
! 965: A pipe is simply a way to connect the output of one program
! 966: to the input of another program,
! 967: so the two run as a sequence of processes \(em
! 968: a pipeline.
! 969: .PP
! 970: For example,
! 971: .P1
! 972: pr f g h
! 973: .P2
! 974: will print the files
! 975: .UL f ,
! 976: .UL g ,
! 977: and
! 978: .UL h ,
! 979: beginning each on a new page.
! 980: Suppose you want
! 981: them run together instead.
! 982: You could say
! 983: .P1
! 984: cat f g h >temp
! 985: pr <temp
! 986: rm temp
! 987: .P2
! 988: but this is more work than necessary.
! 989: Clearly what we want is to take the output of
! 990: .UL cat
! 991: and
! 992: connect it to the input of
! 993: .UL pr .
! 994: So let us use a pipe:
! 995: .P1
! 996: cat f g h | pr
! 997: .P2
! 998: The vertical bar
! 999: .UL |
! 1000: means to
! 1001: take the output from
! 1002: .UL cat ,
! 1003: which would normally have gone to the terminal,
! 1004: and put it into
! 1005: .UL pr
! 1006: to be neatly formatted.
! 1007: .PP
! 1008: There are many other examples of pipes.
! 1009: For example,
! 1010: .P1
! 1011: ls | pr -3
! 1012: .P2
! 1013: prints a list of your files in three columns.
! 1014: The program
! 1015: .UL wc
! 1016: counts the number of lines, words and characters in
! 1017: its input, and as we saw earlier,
! 1018: .UL who
! 1019: prints a list of currently-logged on people,
! 1020: one per line.
! 1021: Thus
! 1022: .P1
! 1023: who | wc
! 1024: .P2
! 1025: tells how many people are logged on.
! 1026: And of course
! 1027: .P1
! 1028: ls | wc
! 1029: .P2
! 1030: counts your files.
! 1031: .PP
! 1032: Any program
! 1033: that reads from the terminal
! 1034: can read from a pipe instead;
! 1035: any program that writes on the terminal can drive
! 1036: a pipe.
! 1037: You can have as many elements in a pipeline as you wish.
! 1038: .PP
! 1039: Many
! 1040: .UC UNIX
! 1041: programs are written so that they will take their input from one or more files
! 1042: if file arguments are given;
! 1043: if no arguments are given they will read from the terminal,
! 1044: and thus can be used in pipelines.
! 1045: .UL pr
! 1046: is one example:
! 1047: .P1
! 1048: pr -3 a b c
! 1049: .P2
! 1050: prints files
! 1051: .UL a ,
! 1052: .UL b
! 1053: and
! 1054: .UL c
! 1055: in order in three columns.
! 1056: But in
! 1057: .P1
! 1058: cat a b c | pr -3
! 1059: .P2
! 1060: .UL pr
! 1061: prints the information coming down the pipeline,
! 1062: still in
! 1063: three columns.
! 1064: .SH
! 1065: The Shell
! 1066: .PP
! 1067: We have already mentioned once or twice the mysterious
! 1068: ``shell,''
! 1069: which is in fact
! 1070: .UL sh (1).
! 1071: The shell is the program that interprets what you type as
! 1072: commands and arguments.
! 1073: It also looks after translating
! 1074: .UL * ,
! 1075: etc.,
! 1076: into lists of filenames,
! 1077: and
! 1078: .UL < ,
! 1079: .UL > ,
! 1080: and
! 1081: .UL |
! 1082: into changes of input and output streams.
! 1083: .PP
! 1084: The shell has other capabilities too.
! 1085: For example, you can run two programs with one command line
! 1086: by separating the commands with a semicolon;
! 1087: the shell recognizes the semicolon and
! 1088: breaks the line into two commands.
! 1089: Thus
! 1090: .P1
! 1091: date; who
! 1092: .P2
! 1093: does both commands before returning with a prompt character.
! 1094: .PP
! 1095: You can also have more than one program running
! 1096: .ul
! 1097: simultaneously
! 1098: if you wish.
! 1099: For example, if you are doing something time-consuming,
! 1100: like the editor script
! 1101: of an earlier section,
! 1102: and you don't want to wait around for the results before starting something else,
! 1103: you can say
! 1104: .P1
! 1105: ed file <script &
! 1106: .P2
! 1107: The ampersand at the end of a command line
! 1108: says ``start this command running,
! 1109: then take further commands from the terminal immediately,''
! 1110: that is,
! 1111: don't wait for it to complete.
! 1112: Thus the script will begin,
! 1113: but you can do something else at the same time.
! 1114: Of course, to keep the output from interfering
! 1115: with what you're doing on the terminal,
! 1116: it would be better to say
! 1117: .P1
! 1118: ed file <script >script.out &
! 1119: .P2
! 1120: which saves the output lines in a file
! 1121: called
! 1122: .UL script.out .
! 1123: .PP
! 1124: When you initiate a command with
! 1125: .UL & ,
! 1126: the system
! 1127: replies with a number
! 1128: called the process number,
! 1129: which identifies the command in case you later want
! 1130: to stop it.
! 1131: If you do, you can say
! 1132: .P1
! 1133: kill process\(hynumber
! 1134: .P2
! 1135: If you forget the process number,
! 1136: the command
! 1137: .UL ps
! 1138: will tell you about everything you have running.
! 1139: (If you are desperate,
! 1140: .UL kill\ 0
! 1141: will kill all your processes.)
! 1142: And if you're curious about other people,
! 1143: .UL ps\ a
! 1144: will tell you about
! 1145: .ul
! 1146: all
! 1147: programs that are currently running.
! 1148: .PP
! 1149: You can say
! 1150: .P1 1
! 1151: (command\(hy1; command\(hy2; command\(hy3) &
! 1152: .P2
! 1153: to start three commands in the background,
! 1154: or you can start a background pipeline with
! 1155: .P1
! 1156: command\(hy1 | command\(hy2 &
! 1157: .P2
! 1158: .PP
! 1159: Just as you can tell the editor
! 1160: or some similar program to take its input
! 1161: from a file instead of from the terminal,
! 1162: you can tell the shell to read a file
! 1163: to get commands.
! 1164: (Why not? The shell, after all, is just a program,
! 1165: albeit a clever one.)
! 1166: For instance, suppose you want to set tabs on
! 1167: your terminal, and find out the date
! 1168: and who's on the system every time you log in.
! 1169: Then you can put the three necessary commands
! 1170: .UL tabs , (
! 1171: .UL date ,
! 1172: .UL who )
! 1173: into a file, let's call it
! 1174: .UL startup ,
! 1175: and then run it with
! 1176: .P1
! 1177: sh startup
! 1178: .P2
! 1179: This says to run the shell with the file
! 1180: .UL startup
! 1181: as input.
! 1182: The effect is as if you had typed
! 1183: the contents of
! 1184: .UL startup
! 1185: on the terminal.
! 1186: .PP
! 1187: If this is to be a regular thing,
! 1188: you can eliminate the
! 1189: need to type
! 1190: .UL sh :
! 1191: simply type, once only, the command
! 1192: .P1
! 1193: chmod +x startup
! 1194: .P2
! 1195: and thereafter you need only say
! 1196: .P1
! 1197: startup
! 1198: .P2
! 1199: to run the sequence of commands.
! 1200: The
! 1201: .UL chmod (1)
! 1202: command marks the file executable;
! 1203: the shell recognizes this and runs it as a sequence of commands.
! 1204: .PP
! 1205: If you want
! 1206: .UL startup
! 1207: to run automatically every time you log in,
! 1208: create a file in your login directory called
! 1209: .UL .profile ,
! 1210: and place in it the line
! 1211: .UL startup .
! 1212: When the shell first gains control when you log in,
! 1213: it looks for the
! 1214: .UL .profile
! 1215: file and does whatever commands it finds in it.\(dg
! 1216: .FS
! 1217: \(dg The c shell instead reads a file called
! 1218: .UL .login
! 1219: .
! 1220: .FE
! 1221: We'll get back to the shell in the section
! 1222: on programming.
CVSweb <webmaster@jp.NetBSD.org>