Commit | Line | Data |
---|---|---|
fc2a7a49 KD |
1 | .\" @(#)u2 4.1 (Berkeley) %G% |
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. | |
368 | Second, although you can use almost any character | |
369 | in a filename, | |
370 | common sense says you should stick to ones that are visible, | |
371 | and that you should probably avoid characters that might be used | |
372 | with other meanings. | |
373 | We have already seen, for example, | |
374 | that in the | |
375 | .UL ls | |
376 | command, | |
377 | .UL ls\ \-t | |
378 | means to list in time order. | |
379 | So if you had a file whose name | |
380 | was | |
381 | .UL \-t , | |
382 | you would have a tough time listing it by name. | |
383 | Besides the minus sign, there are other characters which | |
384 | have special meaning. | |
385 | To avoid pitfalls, | |
386 | you would do well to | |
387 | use only letters, numbers and the period | |
388 | until you're familiar with the situation. | |
389 | .PP | |
390 | On to some more positive suggestions. | |
391 | Suppose you're typing a large document | |
392 | like a book. | |
393 | Logically this divides into many small pieces, | |
394 | like chapters and perhaps sections. | |
395 | Physically it must be divided too, | |
396 | for | |
397 | .UL ed | |
398 | will not handle really big files. | |
399 | Thus you should type the document as a number of files. | |
400 | You might have a separate file for each chapter, | |
401 | called | |
402 | .P1 | |
403 | chap1 | |
404 | chap2 | |
405 | .ft R | |
406 | etc... | |
407 | .P2 | |
408 | Or, if each chapter were broken into several files, you might have | |
409 | .P1 | |
410 | chap1.1 | |
411 | chap1.2 | |
412 | chap1.3 | |
413 | \&... | |
414 | chap2.1 | |
415 | chap2.2 | |
416 | \&... | |
417 | .P2 | |
418 | You can now tell at a glance where a particular file fits into the whole. | |
419 | .PP | |
420 | There are advantages to a systematic naming convention which are not obvious | |
421 | to the novice | |
422 | .UC UNIX | |
423 | user. | |
424 | What if you wanted to print the whole book? | |
425 | You could say | |
426 | .P1 | |
427 | pr chap1.1 chap1.2 chap1.3 ...... | |
428 | .P2 | |
429 | but you would get tired pretty fast, and would probably even make mistakes. | |
430 | Fortunately, there is a shortcut. | |
431 | You can say | |
432 | .P1 | |
433 | pr chap* | |
434 | .P2 | |
435 | The | |
436 | .UL * | |
437 | means ``anything at all,'' | |
438 | so this translates into ``print all files | |
439 | whose names begin with | |
440 | .UL chap '', | |
441 | listed in alphabetical order. | |
442 | .PP | |
443 | This shorthand notation | |
444 | is not a property of the | |
445 | .UL pr | |
446 | command, by the way. | |
447 | It is system-wide, a service of the program | |
448 | that interprets commands | |
449 | (the ``shell,'' | |
450 | .UL sh (1)). | |
451 | Using that fact, you can see how to list the names of the files in the book: | |
452 | .P1 | |
453 | ls chap* | |
454 | .P2 | |
455 | produces | |
456 | .P1 | |
457 | chap1.1 | |
458 | chap1.2 | |
459 | chap1.3 | |
460 | \&... | |
461 | .P2 | |
462 | The | |
463 | .UL * | |
464 | is not limited to the last position in a filename \(em | |
465 | it can be anywhere | |
466 | and can occur several times. | |
467 | Thus | |
468 | .P1 | |
469 | rm *junk* *temp* | |
470 | .P2 | |
471 | removes all files that contain | |
472 | .UL junk | |
473 | or | |
474 | .UL temp | |
475 | as any part of their name. | |
476 | As a special case, | |
477 | .UL * | |
478 | by itself matches every filename, | |
479 | so | |
480 | .P1 | |
481 | pr * | |
482 | .P2 | |
483 | prints all your files | |
484 | (alphabetical order), | |
485 | and | |
486 | .P1 | |
487 | rm * | |
488 | .P2 | |
489 | removes | |
490 | .ul | |
491 | all files. | |
492 | (You had better be | |
493 | .IT very | |
494 | sure that's what you wanted to say!) | |
495 | .PP | |
496 | The | |
497 | .UL * | |
498 | is not | |
499 | the only pattern-matching feature available. | |
500 | Suppose you want to print only chapters 1 through 4 and 9. | |
501 | Then you can say | |
502 | .P1 | |
503 | pr chap[12349]* | |
504 | .P2 | |
505 | The | |
506 | .UL [...] | |
507 | means to match any of the characters inside the brackets. | |
508 | A range of consecutive letters or digits can be abbreviated, | |
509 | so you can also do this | |
510 | with | |
511 | .P1 | |
512 | pr chap[1-49]* | |
513 | .P2 | |
514 | Letters can also be used within brackets: | |
515 | .UL [a\-z] | |
516 | matches any character in the range | |
517 | .UL a | |
518 | through | |
519 | .UL z . | |
520 | .PP | |
521 | The | |
522 | .UL ? | |
523 | pattern matches any single character, | |
524 | so | |
525 | .P1 | |
526 | ls ? | |
527 | .P2 | |
528 | lists all files which have single-character names, | |
529 | and | |
530 | .P1 | |
531 | ls -l chap?.1 | |
532 | .P2 | |
533 | lists information about the first file of each chapter | |
534 | .UL chap1.1 \&, ( | |
535 | .UL chap2.1 , | |
536 | etc.). | |
537 | .PP | |
538 | Of these niceties, | |
539 | .UL * | |
540 | is certainly the most useful, | |
541 | and you should get used to it. | |
542 | The others are frills, but worth knowing. | |
543 | .PP | |
544 | If you should ever have to turn off the special meaning | |
545 | of | |
546 | .UL * , | |
547 | .UL ? , | |
548 | etc., | |
549 | enclose the entire argument in single quotes, | |
550 | as in | |
551 | .P1 | |
552 | ls \(fm?\(fm | |
553 | .P2 | |
554 | We'll see some more examples of this shortly. | |
555 | .SH | |
556 | What's in a Filename, Continued | |
557 | .PP | |
558 | When you first made that file called | |
559 | .UL junk , | |
560 | how did | |
561 | the system | |
562 | know that there wasn't another | |
563 | .UL junk | |
564 | somewhere else, | |
565 | especially since the person in the next office is also | |
566 | reading this tutorial? | |
567 | The answer is that generally each user | |
568 | has a private | |
569 | .IT directory , | |
570 | which contains only the files that belong to him. | |
571 | When you log in, you are ``in'' your directory. | |
572 | Unless you take special action, | |
573 | when you create a new file, | |
574 | it is made in the directory that you are currently in; | |
575 | this is most often your own directory, | |
576 | and thus the file is unrelated to any other file of the same name | |
577 | that might exist in someone else's directory. | |
578 | .PP | |
579 | The set of all files | |
580 | is organized into a (usually big) tree, | |
581 | with your files located several branches into the tree. | |
582 | It is possible for you to ``walk'' around this tree, | |
583 | and to find any file in the system, by starting at the root | |
584 | of the tree and walking along the proper set of branches. | |
585 | Conversely, you can start where you are and walk toward the root. | |
586 | .PP | |
587 | Let's try the latter first. | |
588 | The basic tools is the command | |
589 | .UL pwd | |
590 | (``print working directory''), | |
591 | which prints the name of the directory you are currently in. | |
592 | .PP | |
593 | Although the details will vary according to the system you are on, | |
594 | if you give the | |
595 | command | |
596 | .UL pwd , | |
597 | it will print something like | |
598 | .P1 | |
599 | /usr/your\(hyname | |
600 | .P2 | |
601 | This says that you are currently in the directory | |
602 | .UL your-name , | |
603 | which is in turn in the directory | |
604 | .UL /usr , | |
605 | which is in turn in the root directory | |
606 | called by convention just | |
607 | .UL / . | |
608 | (Even if it's not called | |
609 | .UL /usr | |
610 | on your system, | |
611 | you will get something analogous. | |
612 | Make the corresponding changes and read on.) | |
613 | .PP | |
614 | If you now type | |
615 | .P1 | |
616 | ls /usr/your\(hyname | |
617 | .P2 | |
618 | you should get exactly the same list of file names | |
619 | as you get from a plain | |
620 | .UL ls : | |
621 | with no arguments, | |
622 | .UL ls | |
623 | lists the contents of the current directory; | |
624 | given the name of a directory, | |
625 | it lists the contents of that directory. | |
626 | .PP | |
627 | Next, try | |
628 | .P1 | |
629 | ls /usr | |
630 | .P2 | |
631 | This should print a long series of names, | |
632 | among which is your own login name | |
633 | .UL your-name . | |
634 | On many systems, | |
635 | .UL usr | |
636 | is a directory that contains the directories | |
637 | of all the normal users of the system, | |
638 | like you. | |
639 | .PP | |
640 | The next step is to try | |
641 | .P1 | |
642 | ls / | |
643 | .P2 | |
644 | You should get a response something like this | |
645 | (although again the details may be different): | |
646 | .P1 | |
647 | bin | |
648 | dev | |
649 | etc | |
650 | lib | |
651 | tmp | |
652 | usr | |
653 | .P2 | |
654 | This is a collection of the basic directories of files | |
655 | that | |
656 | the system | |
657 | knows about; | |
658 | we are at the root of the tree. | |
659 | .PP | |
660 | Now try | |
661 | .P1 | |
662 | cat /usr/your\(hyname/junk | |
663 | .P2 | |
664 | (if | |
665 | .UL junk | |
666 | is still around in your directory). | |
667 | The name | |
668 | .P1 | |
669 | /usr/your\(hyname/junk | |
670 | .P2 | |
671 | is called the | |
672 | .UL pathname | |
673 | of the file that | |
674 | you normally think of as ``junk''. | |
675 | ``Pathname'' has an obvious meaning: | |
676 | it represents the full name of the path you have to follow from the root | |
677 | through the tree of directories to get to a particular file. | |
678 | It is a universal rule in | |
679 | the | |
680 | .UC UNIX | |
681 | system | |
682 | that anywhere you can use an ordinary filename, | |
683 | you can use a pathname. | |
684 | .PP | |
685 | Here is a picture which may make this clearer: | |
686 | .P1 1 | |
687 | .ft R | |
688 | .if t .vs 9p | |
689 | .if t .tr /\(sl | |
690 | .if t .tr || | |
691 | .ce 100 | |
692 | (root) | |
693 | / | \e | |
694 | / | \e | |
695 | / | \e | |
696 | bin etc usr dev tmp | |
697 | / | \e / | \e / | \e / | \e / | \e | |
698 | / | \e | |
699 | / | \e | |
700 | adam eve mary | |
701 | / / \e \e | |
702 | / \e junk | |
703 | junk temp | |
704 | .ce 0 | |
705 | .br | |
706 | .tr // | |
707 | .P2 | |
708 | .LP | |
709 | Notice that Mary's | |
710 | .UL junk | |
711 | is unrelated to Eve's. | |
712 | .PP | |
713 | This isn't too exciting if all the files of interest are in your own | |
714 | directory, but if you work with someone else | |
715 | or on several projects concurrently, | |
716 | it becomes handy indeed. | |
717 | For example, your friends can print your book by saying | |
718 | .P1 | |
719 | pr /usr/your\(hyname/chap* | |
720 | .P2 | |
721 | Similarly, you can find out what files your neighbor has | |
722 | by saying | |
723 | .P1 | |
724 | ls /usr/neighbor\(hyname | |
725 | .P2 | |
726 | or make your own copy of one of his files by | |
727 | .P1 | |
728 | cp /usr/your\(hyneighbor/his\(hyfile yourfile | |
729 | .P2 | |
730 | .PP | |
731 | If your neighbor doesn't want you poking around in his files, | |
732 | or vice versa, | |
733 | privacy can be arranged. | |
734 | Each file and directory has read-write-execute permissions for the owner, | |
735 | a group, and everyone else, | |
736 | which can be set | |
737 | to control access. | |
738 | See | |
739 | .UL ls (1) | |
740 | and | |
741 | .UL chmod (1) | |
742 | for details. | |
743 | As a matter of observed fact, | |
744 | most users most of the time find openness of more | |
745 | benefit than privacy. | |
746 | .PP | |
747 | As a final experiment with pathnames, try | |
748 | .P1 | |
749 | ls /bin /usr/bin | |
750 | .P2 | |
751 | Do some of the names look familiar? | |
752 | When you run a program, by typing its name after the prompt character, | |
753 | the system simply looks for a file of that name. | |
754 | It normally looks first in your directory | |
755 | (where it typically doesn't find it), | |
756 | then in | |
757 | .UL /bin | |
758 | and finally in | |
759 | .UL /usr/bin . | |
760 | There is nothing magic about commands like | |
761 | .UL cat | |
762 | or | |
763 | .UL ls , | |
764 | except that they have been collected into a couple of places to be easy to find and administer. | |
765 | .PP | |
766 | What if you work regularly with someone else on common information | |
767 | in his directory? | |
768 | You could just log in as your friend each time you want to, | |
769 | but you can also say | |
770 | ``I want to work on his files instead of my own''. | |
771 | This is done by changing the directory that you are | |
772 | currently in: | |
773 | .P1 | |
774 | cd /usr/your\(hyfriend | |
775 | .P2 | |
776 | (On some systems, | |
777 | .UL cd | |
778 | is spelled | |
779 | .UL chdir .) | |
780 | Now when you use a filename in something like | |
781 | .UL cat | |
782 | or | |
783 | .UL pr , | |
784 | it refers to the file in your friend's directory. | |
785 | Changing directories doesn't affect any permissions associated | |
786 | with a file \(em | |
787 | if you couldn't access a file from your own directory, | |
788 | changing to another directory won't alter that fact. | |
789 | Of course, | |
790 | if you forget what directory you're in, type | |
791 | .P1 | |
792 | pwd | |
793 | .P2 | |
794 | to find out. | |
795 | .PP | |
796 | It is usually convenient to arrange your own files | |
797 | so that all the files related to one thing are in a directory separate | |
798 | from other projects. | |
799 | For example, when you write your book, you might want to keep all the text | |
800 | in a directory called | |
801 | .UL book . | |
802 | So make one with | |
803 | .P1 | |
804 | mkdir book | |
805 | .P2 | |
806 | then go to it with | |
807 | .P1 | |
808 | cd book | |
809 | .P2 | |
810 | then start typing chapters. | |
811 | The book is now found in (presumably) | |
812 | .P1 | |
813 | /usr/your\(hyname/book | |
814 | .P2 | |
815 | To remove the directory | |
816 | .UL book , | |
817 | type | |
818 | .P1 | |
819 | rm book/* | |
820 | rmdir book | |
821 | .P2 | |
822 | The first command removes all files from the directory; | |
823 | the second | |
824 | removes the empty directory. | |
825 | .PP | |
826 | You can go up one level in the tree of files | |
827 | by saying | |
828 | .P1 | |
829 | cd .. | |
830 | .P2 | |
831 | .UL .. '' `` | |
832 | is the name of the parent of whatever directory you are currently in. | |
833 | For completeness, | |
834 | .UL . '' `` | |
835 | is an alternate name | |
836 | for the directory you are in. | |
837 | .SH | |
838 | Using Files instead of the Terminal | |
839 | .PP | |
840 | Most of the commands we have seen so far produce output | |
841 | on the terminal; | |
842 | some, like the editor, also take their input from the terminal. | |
843 | It is universal in | |
844 | .UC UNIX | |
845 | systems | |
846 | that the terminal can be replaced by a file | |
847 | for either or both of input and output. | |
848 | As one example, | |
849 | .P1 | |
850 | ls | |
851 | .P2 | |
852 | makes a list of files on your terminal. | |
853 | But if you say | |
854 | .P1 | |
855 | ls >filelist | |
856 | .P2 | |
857 | a list of your files will be placed in the file | |
858 | .UL filelist | |
859 | (which | |
860 | will be created if it doesn't already exist, | |
861 | or overwritten if it does). | |
862 | The symbol | |
863 | .UL > | |
864 | means ``put the output on the following file, | |
865 | rather than on the terminal.'' | |
866 | Nothing is produced on the terminal. | |
867 | As another example, you could combine | |
868 | several files into one by capturing the output of | |
869 | .UL cat | |
870 | in a file: | |
871 | .P1 | |
872 | cat f1 f2 f3 >temp | |
873 | .P2 | |
874 | .PP | |
875 | The symbol | |
876 | .UL >> | |
877 | operates very much like | |
878 | .UL > | |
879 | does, | |
880 | except that it means | |
881 | ``add to the end of.'' | |
882 | That is, | |
883 | .P1 | |
884 | cat f1 f2 f3 >>temp | |
885 | .P2 | |
886 | means to concatenate | |
887 | .UL f1 , | |
888 | .UL f2 | |
889 | and | |
890 | .UL f3 | |
891 | to the end of whatever is already in | |
892 | .UL temp , | |
893 | instead of overwriting the existing contents. | |
894 | As with | |
895 | .UL > , | |
896 | if | |
897 | .UL temp | |
898 | doesn't exist, it will be created for you. | |
899 | .PP | |
900 | In a similar way, the symbol | |
901 | .UL < | |
902 | means to take the input | |
903 | for a program from the following file, | |
904 | instead of from the terminal. | |
905 | Thus, you could make up a script of commonly used editing commands | |
906 | and put them into a file called | |
907 | .UL script . | |
908 | Then you can run the script on a file by saying | |
909 | .P1 | |
910 | ed file <script | |
911 | .P2 | |
912 | As another example, you can use | |
913 | .UL ed | |
914 | to prepare a letter in file | |
915 | .UL let , | |
916 | then send it to several people with | |
917 | .P1 | |
918 | mail adam eve mary joe <let | |
919 | .P2 | |
920 | .SH | |
921 | Pipes | |
922 | .PP | |
923 | One of the novel contributions of | |
924 | the | |
925 | .UC UNIX | |
926 | system | |
927 | is the idea of a | |
928 | .ul | |
929 | pipe. | |
930 | A pipe is simply a way to connect the output of one program | |
931 | to the input of another program, | |
932 | so the two run as a sequence of processes \(em | |
933 | a pipeline. | |
934 | .PP | |
935 | For example, | |
936 | .P1 | |
937 | pr f g h | |
938 | .P2 | |
939 | will print the files | |
940 | .UL f , | |
941 | .UL g , | |
942 | and | |
943 | .UL h , | |
944 | beginning each on a new page. | |
945 | Suppose you want | |
946 | them run together instead. | |
947 | You could say | |
948 | .P1 | |
949 | cat f g h >temp | |
950 | pr <temp | |
951 | rm temp | |
952 | .P2 | |
953 | but this is more work than necessary. | |
954 | Clearly what we want is to take the output of | |
955 | .UL cat | |
956 | and | |
957 | connect it to the input of | |
958 | .UL pr . | |
959 | So let us use a pipe: | |
960 | .P1 | |
961 | cat f g h | pr | |
962 | .P2 | |
963 | The vertical bar | |
964 | .UL | | |
965 | means to | |
966 | take the output from | |
967 | .UL cat , | |
968 | which would normally have gone to the terminal, | |
969 | and put it into | |
970 | .UL pr | |
971 | to be neatly formatted. | |
972 | .PP | |
973 | There are many other examples of pipes. | |
974 | For example, | |
975 | .P1 | |
976 | ls | pr -3 | |
977 | .P2 | |
978 | prints a list of your files in three columns. | |
979 | The program | |
980 | .UL wc | |
981 | counts the number of lines, words and characters in | |
982 | its input, and as we saw earlier, | |
983 | .UL who | |
984 | prints a list of currently-logged on people, | |
985 | one per line. | |
986 | Thus | |
987 | .P1 | |
988 | who | wc | |
989 | .P2 | |
990 | tells how many people are logged on. | |
991 | And of course | |
992 | .P1 | |
993 | ls | wc | |
994 | .P2 | |
995 | counts your files. | |
996 | .PP | |
997 | Any program | |
998 | that reads from the terminal | |
999 | can read from a pipe instead; | |
1000 | any program that writes on the terminal can drive | |
1001 | a pipe. | |
1002 | You can have as many elements in a pipeline as you wish. | |
1003 | .PP | |
1004 | Many | |
1005 | .UC UNIX | |
1006 | programs are written so that they will take their input from one or more files | |
1007 | if file arguments are given; | |
1008 | if no arguments are given they will read from the terminal, | |
1009 | and thus can be used in pipelines. | |
1010 | .UL pr | |
1011 | is one example: | |
1012 | .P1 | |
1013 | pr -3 a b c | |
1014 | .P2 | |
1015 | prints files | |
1016 | .UL a , | |
1017 | .UL b | |
1018 | and | |
1019 | .UL c | |
1020 | in order in three columns. | |
1021 | But in | |
1022 | .P1 | |
1023 | cat a b c | pr -3 | |
1024 | .P2 | |
1025 | .UL pr | |
1026 | prints the information coming down the pipeline, | |
1027 | still in | |
1028 | three columns. | |
1029 | .SH | |
1030 | The Shell | |
1031 | .PP | |
1032 | We have already mentioned once or twice the mysterious | |
1033 | ``shell,'' | |
1034 | which is in fact | |
1035 | .UL sh (1). | |
1036 | The shell is the program that interprets what you type as | |
1037 | commands and arguments. | |
1038 | It also looks after translating | |
1039 | .UL * , | |
1040 | etc., | |
1041 | into lists of filenames, | |
1042 | and | |
1043 | .UL < , | |
1044 | .UL > , | |
1045 | and | |
1046 | .UL | | |
1047 | into changes of input and output streams. | |
1048 | .PP | |
1049 | The shell has other capabilities too. | |
1050 | For example, you can run two programs with one command line | |
1051 | by separating the commands with a semicolon; | |
1052 | the shell recognizes the semicolon and | |
1053 | breaks the line into two commands. | |
1054 | Thus | |
1055 | .P1 | |
1056 | date; who | |
1057 | .P2 | |
1058 | does both commands before returning with a prompt character. | |
1059 | .PP | |
1060 | You can also have more than one program running | |
1061 | .ul | |
1062 | simultaneously | |
1063 | if you wish. | |
1064 | For example, if you are doing something time-consuming, | |
1065 | like the editor script | |
1066 | of an earlier section, | |
1067 | and you don't want to wait around for the results before starting something else, | |
1068 | you can say | |
1069 | .P1 | |
1070 | ed file <script & | |
1071 | .P2 | |
1072 | The ampersand at the end of a command line | |
1073 | says ``start this command running, | |
1074 | then take further commands from the terminal immediately,'' | |
1075 | that is, | |
1076 | don't wait for it to complete. | |
1077 | Thus the script will begin, | |
1078 | but you can do something else at the same time. | |
1079 | Of course, to keep the output from interfering | |
1080 | with what you're doing on the terminal, | |
1081 | it would be better to say | |
1082 | .P1 | |
1083 | ed file <script >script.out & | |
1084 | .P2 | |
1085 | which saves the output lines in a file | |
1086 | called | |
1087 | .UL script.out . | |
1088 | .PP | |
1089 | When you initiate a command with | |
1090 | .UL & , | |
1091 | the system | |
1092 | replies with a number | |
1093 | called the process number, | |
1094 | which identifies the command in case you later want | |
1095 | to stop it. | |
1096 | If you do, you can say | |
1097 | .P1 | |
1098 | kill process\(hynumber | |
1099 | .P2 | |
1100 | If you forget the process number, | |
1101 | the command | |
1102 | .UL ps | |
1103 | will tell you about everything you have running. | |
1104 | (If you are desperate, | |
1105 | .UL kill\ 0 | |
1106 | will kill all your processes.) | |
1107 | And if you're curious about other people, | |
1108 | .UL ps\ a | |
1109 | will tell you about | |
1110 | .ul | |
1111 | all | |
1112 | programs that are currently running. | |
1113 | .PP | |
1114 | You can say | |
1115 | .P1 1 | |
1116 | (command\(hy1; command\(hy2; command\(hy3) & | |
1117 | .P2 | |
1118 | to start three commands in the background, | |
1119 | or you can start a background pipeline with | |
1120 | .P1 | |
1121 | command\(hy1 | command\(hy2 & | |
1122 | .P2 | |
1123 | .PP | |
1124 | Just as you can tell the editor | |
1125 | or some similar program to take its input | |
1126 | from a file instead of from the terminal, | |
1127 | you can tell the shell to read a file | |
1128 | to get commands. | |
1129 | (Why not? The shell, after all, is just a program, | |
1130 | albeit a clever one.) | |
1131 | For instance, suppose you want to set tabs on | |
1132 | your terminal, and find out the date | |
1133 | and who's on the system every time you log in. | |
1134 | Then you can put the three necessary commands | |
1135 | .UL tabs , ( | |
1136 | .UL date , | |
1137 | .UL who ) | |
1138 | into a file, let's call it | |
1139 | .UL startup , | |
1140 | and then run it with | |
1141 | .P1 | |
1142 | sh startup | |
1143 | .P2 | |
1144 | This says to run the shell with the file | |
1145 | .UL startup | |
1146 | as input. | |
1147 | The effect is as if you had typed | |
1148 | the contents of | |
1149 | .UL startup | |
1150 | on the terminal. | |
1151 | .PP | |
1152 | If this is to be a regular thing, | |
1153 | you can eliminate the | |
1154 | need to type | |
1155 | .UL sh : | |
1156 | simply type, once only, the command | |
1157 | .P1 | |
1158 | chmod +x startup | |
1159 | .P2 | |
1160 | and thereafter you need only say | |
1161 | .P1 | |
1162 | startup | |
1163 | .P2 | |
1164 | to run the sequence of commands. | |
1165 | The | |
1166 | .UL chmod (1) | |
1167 | command marks the file executable; | |
1168 | the shell recognizes this and runs it as a sequence of commands. | |
1169 | .PP | |
1170 | If you want | |
1171 | .UL startup | |
1172 | to run automatically every time you log in, | |
1173 | create a file in your login directory called | |
1174 | .UL .profile , | |
1175 | and place in it the line | |
1176 | .UL startup . | |
1177 | When the shell first gains control when you log in, | |
1178 | it looks for the | |
1179 | .UL .profile | |
1180 | file and does whatever commands it finds in it. | |
1181 | We'll get back to the shell in the section | |
1182 | on programming. |