Annotation of 43BSDReno/share/doc/usd/01.begin/u2, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.