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