home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / beg / u2 < prev    next >
Encoding:
Text File  |  1975-06-26  |  19.0 KB  |  868 lines

  1. .SH
  2. II.  DAY-TO-DAY USE
  3. .SH
  4. Creating Files _ The Editor
  5. .PP
  6. If we have to type a paper or a letter or a program,
  7. how do we get the information stored in the machine?
  8. Most of these tasks are done with
  9. the
  10. .UC UNIX
  11. ``text editor''
  12. .C ed .
  13. Since
  14. .C ed
  15. is thoroughly documented in 
  16. .SE ed (I) 
  17. and explained in
  18. .ul
  19. A Tutorial Introduction to the UNIX Text Editor,
  20. we won't spend any time here describing how to use it.
  21. All we want it for right now is to make some
  22. .ul
  23. files.
  24. (A file is just a collection of information stored in the machine,
  25. a simplistic but adequate definition.)
  26. .PP
  27. To create a file with some text in it, do the following:
  28. .B1
  29.  ed    (invokes the text editor)
  30.  a    (command to ``ed'', to add text)
  31. .I
  32.  now type in
  33.  whatever text you want ...
  34. .R
  35.  \fB.\fP    (signals the end of adding text)
  36. .B2
  37. At this point we could do various editing operations
  38. on the text we typed in, such as correcting spelling mistakes,
  39. rearranging paragraphs and the like.
  40. Finally, we write the information we have typed
  41. into a file with the editor command ``w'':
  42. .B1
  43. w junk
  44. .B2
  45. .C ed
  46. will respond with the number of characters it wrote
  47. into the file called ``junk''.
  48. .PP
  49. Suppose we now add a few more lines with ``a'',
  50. terminate them with ``.'',
  51. and write the whole thing out as ``temp'', using
  52. .B1
  53. w temp
  54. .B2
  55. We should now have two files, a smaller one called ``junk'' and a bigger one
  56. (bigger by the extra lines) called ``temp''.
  57. Type a ``q'' to quit the editor.
  58. .SH
  59. What files are out there?
  60. .PP
  61. The
  62. .C ls
  63. (for ``list'') command lists the names
  64. (not contents) of any of the files that
  65. .UC UNIX
  66. knows about.
  67. If we type
  68. .B1
  69. ls
  70. .B2
  71. the response will be
  72. .B1
  73. junk
  74. temp
  75. .B2
  76. which are indeed our two files.
  77. They are sorted into alphabetical order automatically,
  78. but other variations are possible.
  79. For example,
  80. if we add the optional argument ``-t'',
  81. .B1
  82. ls -t
  83. .B2
  84. lists them in the order in which they were last changed,
  85. most recent first.
  86. The ``-l'' option gives a ``long'' listing:
  87. .B1
  88. ls -l
  89. .B2
  90. will produce something like
  91. .B1
  92. -rw-rw-rw-  1 bwk   41 Sep 22 12:56 junk
  93. -rw-rw-rw-  1 bwk   78 Sep 22 12:57 temp
  94. .B2
  95. The date and time are of the last change to the file.
  96. The 41 and 78 are the number of characters
  97. (you got the same thing from
  98. .C ed ).
  99. ``bwk'' is the owner of the file _ the person
  100. who created it.
  101. The ``-rw-rw-rw-'' tells who has permission to read and write the file,
  102. in this case everyone.
  103. .PP
  104. Options can be combined:
  105. ``ls -lt'' would give the same thing,
  106. but sorted into time order.
  107. You can also name the files you're interested in,
  108. and 
  109. .C ls
  110. will list the information about them only.
  111. More details can be found in  
  112. .SE ls (I).
  113. .PP
  114. It is generally true of
  115. .UC UNIX
  116. programs that ``flag'' arguments like ``-t''
  117. precede filename arguments.
  118. .SH
  119. Printing Files
  120. .PP
  121. Now that you've got a file of text,
  122. how do you print it so people can look at it?
  123. There are a host of programs that do that,
  124. probably more than are needed.
  125. .PP
  126. One simple thing is to use the editor,
  127. since printing is often done just before making changes anyway.
  128. You can say
  129. .B1
  130. ed junk
  131. 1,$p
  132. .B2
  133. .C ed
  134. will reply with the count of the characters in ``junk''
  135. and then print all the lines in the file.
  136. After you learn how to use the editor,
  137. you can be selective about the parts you print.
  138. .PP
  139. There are times when it's not feasible to use the editor for printing.
  140. For example, there is a limit on how big a file
  141. .C ed
  142. can handle
  143. (about 65,000 characters or 4000 lines).
  144. Secondly, 
  145. it
  146. will only print one file at a time,
  147. and sometimes you want to print several, one after another.
  148. So here are a couple of alternatives.
  149. .PP
  150. First is
  151. .C cat ,
  152. the simplest of all the printing programs.
  153. .C cat
  154. simply copies all the files in a list onto the terminal.
  155. So you can say
  156. .B1
  157. cat junk
  158. .B2
  159. or, to print two files,
  160. .B1
  161. cat junk temp
  162. .B2
  163. The two files are simply concatenated (hence the name ``cat'')
  164. onto the terminal.
  165. .PP
  166. .C pr
  167. produces formatted printouts of files.
  168. As with 
  169. .C cat ,
  170. .C pr
  171. prints all the files in a list.
  172. The difference is that it produces 
  173. headings with date, time, page number and file name
  174. at the top of each page,
  175. and
  176. extra lines to skip over the fold in the paper.
  177. Thus,
  178. .B1
  179. pr junk temp
  180. .B2
  181. will list ``junk'' neatly,
  182. then skip to the top of a new page and list
  183. ``temp'' neatly.
  184. .PP
  185. .C pr
  186. will also produce multi-column output:
  187. .B1
  188. pr -3 junk 
  189. .B2
  190. prints ``junk'' in 3-column format.
  191. You can use any reasonable number in place of ``3''
  192. and 
  193. .C pr
  194. will do its best.
  195. .PP
  196. It should be noted that
  197. .C pr
  198. is
  199. .ul
  200. not
  201. a formatting program in the sense of shuffling lines around
  202. and justifying margins.
  203. The true formatters are
  204. .C roff ,
  205. .C nroff ,
  206. and
  207. .C troff ,
  208. which we will get to in the section on document preparation.
  209. .PP
  210. There are also programs that print files
  211. on a high-speed printer.
  212. Look in your manual under
  213. .C opr
  214. and
  215. .C lpr .
  216. Which to use depends on the hardware configuration
  217. of your machine.
  218. .SH
  219. Shuffling Files About
  220. .PP
  221. Now that you have some files in the file system
  222. and some experience in printing them,
  223. you can try bigger things.
  224. For example,
  225. you can move a file from one place to another
  226. (which amounts to giving a file a new name),
  227. like this:
  228. .B1
  229. mv junk precious
  230. .B2
  231. This means that what used to be ``junk'' is now ``precious''.
  232. If you do an
  233. .C ls
  234. command now,
  235. you will get
  236. .B1
  237. precious
  238. temp
  239. .B2
  240. Beware that if you move a file to another one
  241. that already exists,
  242. the already existing contents are lost forever.
  243. .PP
  244. If you want
  245. to make a
  246. .ul
  247. copy
  248. of a file (that is, to have two versions of something),
  249. you can use the 
  250. .C cp
  251. command:
  252. .B1
  253. cp precious temp1
  254. .B2
  255. makes a duplicate copy of 
  256. ``precious''
  257. in
  258. ``temp1''.
  259. .PP
  260. Finally, when you get tired of creating and moving
  261. files,
  262. there is a command to remove files from the file system,
  263. called
  264. .C rm .
  265. .B1
  266. rm temp temp1
  267. .B2
  268. will remove all of the files named.
  269. You will get a warning message if one of the named files wasn't there.
  270. .SH
  271. Filename, What's in a
  272. .PP
  273. So far we have used filenames without ever saying what's
  274. a legal name,
  275. so it's time for a couple of rules.
  276. First, filenames are limited to 14 characters,
  277. which is enough to be descriptive.
  278. Second, although you can use almost any character
  279. in a filename,
  280. common sense says you should stick to ones that are visible,
  281. and that you should probably avoid characters that might be used
  282. with other meanings.
  283. We already saw, for example,
  284. that in the
  285. .C ls
  286. command,
  287. ``ls -t'' meant to list in time order.
  288. So if you had a file whose name
  289. was ``-t'',
  290. you would have a tough time listing it by name.
  291. There are a number of other characters which
  292. have special meaning either to
  293. .UC UNIX
  294. as a whole or to numerous commands.
  295. To avoid pitfalls,
  296. you would probably do well to 
  297. use only letters, numbers and the period.
  298. (Don't use the period as the first character of a filename,
  299. for reasons too complicated to go into.)
  300. .sp
  301. .PP
  302. On to some more positive suggestions.
  303. Suppose you're typing a large document
  304. like a book.
  305. Logically this divides into many small pieces,
  306. like chapters and perhaps sections.
  307. Physically it must be divided too,
  308. for 
  309. .C ed
  310. will not handle big files.
  311. Thus you should type the document as a number of files.
  312. You might have a separate file for each chapter,
  313. called
  314. .B1
  315. .ne 3
  316. chap1
  317. chap2
  318. etc...
  319. .B2
  320. Or, if each chapter were broken into several files, you might have
  321. .B1
  322. .ne 7
  323. chap1.1
  324. chap1.2
  325. chap1.3
  326.  ...
  327. chap2.1
  328. chap2.2
  329.  ...
  330. .B2
  331. You can now tell at a glance where a particular file fits into the whole.
  332. .PP
  333. There are advantages to a systematic naming convention which are not obvious
  334. to the novice
  335. .UC UNIX 
  336. user.
  337. What if you wanted to print the whole book?
  338. You could say
  339. .B1
  340. pr chap1.1 chap1.2 chap1.3 ......
  341. .B2
  342. but you would get tired pretty fast, and would probably even make mistakes.
  343. Fortunately, there is a shortcut.
  344. You can say
  345. .B1
  346. pr chap*
  347. .B2
  348. The ``*'' means ``anything at all'',
  349. so this translates into ``print all files
  350. whose names begin with `chap' '',
  351. listed in alphabetical order.
  352. This shorthand notation
  353. is not a property of the
  354. .C pr
  355. command, by the way.
  356. It is system-wide, a service of the program
  357. that interprets commands
  358. (the ``shell''
  359. .SE sh (I)).
  360. Using that fact, you can see how to list the files of the book:
  361. .B1
  362. ls chap*
  363. .B2
  364. produces
  365. .B1
  366. .ne 4
  367. chap1.1
  368. chap1.2
  369. chap1.3
  370.  ...
  371. .B2
  372. The ``*'' is not limited to the last position in a filename _
  373. it can be anywhere.
  374. Thus
  375. .B1
  376. rm *junk*
  377. .B2
  378. removes all files that contain ``junk''
  379. as any part of their name.
  380. As a special case, ``*'' by itself matches every filename,
  381. so
  382. .B1
  383. pr *
  384. .B2
  385. prints all the files
  386. (alphabetical order),
  387. and
  388. .B1
  389. rm *
  390. .B2
  391. removes
  392. .ul
  393. all files.
  394. (You had better be sure that's what you wanted to say!)
  395. .PP
  396. The ``*'' is not the only pattern-matching feature available.
  397. Suppose you want to print only chapters 1 through 4 and 9 of the
  398. book.
  399. Then you can say
  400. .B1
  401. pr chap[12349]*
  402. .B2
  403. The ``[...]'' 
  404. means to match any of the characters inside the brackets.
  405. You can also do this 
  406. with
  407. .B1
  408. pr chap[1-49]*
  409. .B2
  410. ``[a-z]'' matches any character in the range
  411. .ul
  412. a
  413. through
  414. .ul 
  415. z.
  416. There is also a ``?'' character, which matches any single character,
  417. so
  418. .B1
  419. pr ?
  420. .B2
  421. will print all files which have single-character names.
  422. .PP
  423. Of these niceties,
  424. ``*'' is probably the most useful,
  425. and you should get used to it.
  426. The others are frills, but worth knowing.
  427. .PP
  428. If you should ever have to turn off the special meaning
  429. of ``*'', ``?'', etc.,
  430. enclose the entire argument in quotes (single or double),
  431. as in
  432. .B1
  433. ls "?"
  434. .B2
  435. .SH
  436. What's in a Filename, Continued
  437. .PP
  438. When you first made that file called ``junk'',
  439. how did 
  440. .UC UNIX
  441. know that there wasn't another ``junk'' somewhere else,
  442. especially since the person in the next office is also
  443. reading this tutorial?
  444. The reason is that generally each user of 
  445. .UC UNIX
  446. has his own ``directory'',
  447. which contains only the files that belong to him.
  448. When you create a new file,
  449. unless you take special action,
  450. the new file is made in your own directory,
  451. and is unrelated to any other file of the same name
  452. that might exist in someone else's directory.
  453. .PP
  454. The set of all files that
  455. .UC UNIX
  456. knows about are organized into a (usually big) tree,
  457. with your files located several branches up into the tree.
  458. It is possible for you to ``walk'' around this tree,
  459. and to find any file in the system, by starting at the root
  460. of the tree and walking along the right set of branches.
  461. .PP
  462. To begin, type 
  463. .B1
  464. ls /
  465. .B2
  466. ``/'' is the name of the root of the tree (a convention used
  467. by
  468. .UC UNIX ).
  469. You will get a response something like this:
  470. .B1
  471. .ne 6
  472. bin
  473. dev
  474. etc
  475. lib
  476. tmp
  477. usr
  478. .B2
  479. This is a collection of the basic directories of files
  480. that
  481. .UC UNIX
  482. knows about.
  483. On most
  484. systems,
  485. ``usr'' is a directory that contains all the
  486. normal users of the system, like you.
  487. Now try
  488. .B1
  489. ls  /usr
  490. .B2
  491. This should list a long series of names,
  492. among which is your own login name.
  493. Finally, try
  494. .B1
  495. ls  /usr/your-name
  496. .B2
  497. You should get what you get from a plain
  498. .B1
  499. ls
  500. .B2
  501. Now try
  502. .B1
  503. cat  /usr/your-name/junk
  504. .B2
  505. (if ``junk'' is still around).
  506. The name
  507. .B1
  508. /usr/your-name/junk
  509. .B2
  510. is called the ``pathname'' of the file that
  511. you normally think of as ``junk''.
  512. ``Pathname'' has an obvious meaning:
  513. it represents the full name of the path you have to follow
  514. through the tree of directories to get to a particular file.
  515. It is a universal rule in
  516. .UC  UNIX
  517. that anywhere you can use an ordinary filename,
  518. you can use a pathname.
  519. .PP
  520. Here is a picture which may make this clearer:
  521. .B1 1
  522. .vs 9p
  523. .if t .tr /\(sl
  524. .ce 100
  525. .ne 12
  526. (root)
  527. / | \\
  528. /  |  \\
  529. /   |   \\
  530.   bin    etc    usr    dev   tmp 
  531. / | \\   / | \\   / | \\   / | \\   / | \\
  532. /  |  \\
  533. /   |   \\
  534. adam  eve   mary
  535. /        /   \\        \\
  536.              /     \\       junk
  537. junk  temp
  538. .ce 0
  539. .br
  540. .tr //
  541. .B2
  542. .PP
  543. Notice that Mary's ``junk'' is unrelated to Eve's.
  544. .PP
  545. This isn't too exciting if all the files of interest are in your own
  546. directory, but if you work with someone else
  547. or on several projects concurrently,
  548. it becomes handy indeed.
  549. For example, your friends can print your book by saying
  550. .B1
  551. pr  /usr/your-name/chap*
  552. .B2
  553. Similarly, you can find out what files your neighbor has
  554. by saying
  555. .B1
  556. ls  /usr/neighbor-name
  557. .B2
  558. or make your own copy of one of his files by
  559. .B1
  560. cp  /usr/your-neighbor/his-file  yourfile
  561. .B2
  562. .PP
  563. (If your neighbor doesn't want you poking around in his files,
  564. or vice versa,
  565. privacy can be arranged.
  566. Each file and directory can have read-write-execute permissions for the owner,
  567. a group, and everyone else,
  568. to control access.
  569. See
  570. .SE ls (I)
  571. and
  572. .SE chmod (I)
  573. for details.
  574. As a matter of observed fact,
  575. most users most of the time find openness of more
  576. benefit than privacy.)
  577. .PP
  578. As a final experiment with pathnames, try
  579. .B1
  580. ls  /bin  /usr/bin
  581. .B2
  582. Do some of the names look familiar?
  583. When you run a program, by typing its name after a ``%'',
  584. the system simply looks for a file of that name.
  585. It looks first in your directory
  586. (where it typically doesn't find it),
  587. then in ``/bin'' and finally in ``/usr/bin''.
  588. There is nothing magic about commands like
  589. .C cat
  590. or
  591. .C ls ,
  592. except that they have been collected into two places to be easy to find and administer.
  593. .sp
  594. .PP
  595. What if you work regularly with someone else on common information
  596. in his directory?
  597. You could just log in as your friend each time you want to,
  598. but you can also say
  599. ``I want to work on his files instead of my own''.
  600. This is done by changing the directory that you are
  601. currently in:
  602. .B1
  603. chdir  /usr/your-friend
  604. .B2
  605. Now when you use a filename in something like
  606. .C cat
  607. or
  608. .C pr ,
  609. it refers to the file in ``your-friend's'' directory.
  610. Changing directories doesn't affect any permissions associated
  611. with a file _
  612. if you couldn't access a file from your own directory,
  613. changing to another directory won't alter that fact.
  614. .PP
  615. If you forget what directory you're in, type
  616. .B1
  617. pwd
  618. .B2
  619. (``print working directory'')
  620. to find out.
  621. .PP
  622. It is often convenient to arrange one's files
  623. so that all the files related to one thing are in a directory separate
  624. from other projects.
  625. For example, when you write your book, you might want to keep all the text
  626. in a directory called book.
  627. So make one with
  628. .B1
  629. mkdir book
  630. .B2
  631. then go to it with
  632. .B1
  633. chdir book
  634. .B2
  635. then start typing chapters.
  636. The book is now found in (presumably)
  637. .B1
  638. /usr/your-name/book
  639. .B2
  640. To delete a directory, see
  641. .SE rmdir (I).
  642. .PP
  643. You can go up one level in the tree of files 
  644. by saying
  645. .B1
  646. chdir ..
  647. .B2
  648. ``..'' is the name of the parent of whatever directory you are currently in.
  649. For completeness, ``.'' is an alternate name
  650. for the directory you are in.
  651. .SH
  652. Using Files instead of the Terminal
  653. .PP
  654. Most of the commands we have seen so far produce output
  655. on the terminal;
  656. some, like the editor, also take their input from the terminal.
  657. It is universal in
  658. .UC UNIX
  659. that the terminal can be replaced by a file
  660. for either or both of input and output.
  661. As one example, you could say
  662. .B1
  663. ls
  664. .B2
  665. to get a list of files.
  666. But you can also say
  667. .B1
  668. ls >filelist
  669. .B2
  670. to get a list of your files in the file ``filelist''.
  671. (``filelist'' will be created if it doesn't already exist,
  672. or overwritten if it does.)
  673. The symbol ``>'' is used throughout
  674. .UC UNIX
  675. to mean ``put the output on the following file,
  676. rather than on the terminal''.
  677. Nothing is produced on the terminal.
  678. As another example, you could concatenate
  679. several files into one by capturing the output of
  680. .C cat
  681. in a file:
  682. .B1
  683. cat  f1  f2  f3 >temp
  684. .B2
  685. .PP
  686. Similarly, the symbol ``<'' means to take the input
  687. for a program from the following file,
  688. instead of from the terminal.
  689. Thus, you could make up a script of commonly used editing commands
  690. and put them into a file called ``script''.
  691. Then you can run the script on a file by saying
  692. .B1
  693. ed file <script
  694. .B2
  695. .SH
  696. Pipes
  697. .PP
  698. One of the novel contributions of
  699. .UC UNIX
  700. is the idea of a
  701. .ul
  702. pipe.
  703. A pipe is simply a way to connect the output of one program
  704. to the input of another program,
  705. so the two run as a sequence of processes _
  706. a pipe-line.
  707. .PP
  708. For example,
  709. .B1
  710. pr  f  g  h
  711. .B2
  712. will print the files
  713. ``f'', ``g'' and ``h'',
  714. beginning each on a new page.
  715. Suppose you want
  716. them run together instead.
  717. You could say
  718. .B1
  719. .ne 3
  720. cat  f  g  h  >temp
  721. pr  temp
  722. rm  temp
  723. .B2
  724. but this is more work than necessary.
  725. Clearly what we want is to take the output of
  726. .C cat
  727. and
  728. connect it to the input of
  729. .C pr .
  730. So let us use a pipe:
  731. .B1
  732. cat  f  g  h  |  pr
  733. .B2
  734. The vertical bar means to
  735. take the output from
  736. .C cat ,
  737. which would normally have gone to the terminal,
  738. and put it into
  739. .C pr ,
  740. which formats it neatly.
  741. .PP
  742. Any program
  743. that reads from the terminal
  744. can read from a pipe instead;
  745. any program that writes on the terminal can drive
  746. a pipe.
  747. You can have as many elements in a pipeline as you wish.
  748. .PP
  749. Many
  750. .UC UNIX
  751. programs are written so that they will take their input from one or more files
  752. if file arguments are given;
  753. if no arguments are given they will read from the terminal,
  754. and thus can be used in pipelines.
  755. .SH
  756. The Shell
  757. .PP
  758. We have already mentioned once or twice the mysterious
  759. ``shell,''
  760. which is in fact
  761. .SE sh (I).
  762. The shell is the program that interprets what you type as
  763. commands and arguments.
  764. It also looks after translating ``*'', etc.,
  765. into lists of filenames.
  766. .PP
  767. The shell has other capabilities too.
  768. For example, you can start two programs with one command line
  769. by separating the commands with a semicolon;
  770. the shell recognizes the semicolon and
  771. breaks the line into two commands.
  772. Thus
  773. .B1
  774. date; who
  775. .B2
  776. does both commands before returning with a ``%''.
  777. .PP
  778. You can also have more than one program running
  779. .ul
  780. simultaneously
  781. if you wish.
  782. For example, if you are doing something time-consuming,
  783. like the editor script
  784. of an earlier section,
  785. and you don't want to wait around for the results before starting something else,
  786. you can say
  787. .B1
  788. ed  file  <script &
  789. .B2
  790. The ampersand at the end of a command line
  791. says ``start this command running,
  792. then take further commands from the terminal immediately.''
  793. Thus the script will begin,
  794. but you can do something else at the same time.
  795. Of course, to keep the output from interfering
  796. with what you're doing on the terminal,
  797. it would be better to have said
  798. .B1
  799. ed  file  <script >lines &
  800. .B2
  801. which would save the output lines in a file
  802. called
  803. ``lines''.
  804. .PP
  805. When you initiate a command with ``&'',
  806. .UC UNIX
  807. replies with a number
  808. called the process number,
  809. which identifies the command in case you later want
  810. to stop it.
  811. If you do, you can say
  812. .B1
  813. kill process-number
  814. .B2
  815. You might also read
  816. .SE ps (I).
  817. .PP
  818. You can say
  819. .B1 1
  820. (command-1; command-2; command-3) &
  821. .B2
  822. to start these commands in the background,
  823. or you can start a background pipeline with
  824. .B1
  825. command-1 | command-2 &
  826. .B2
  827. .PP
  828. Just as you can tell the editor
  829. or some similar program to take its input
  830. from a file instead of from the terminal,
  831. you can tell the shell to read a file
  832. to get commands.
  833. (Why not? The shell after all is just a program,
  834. albeit a clever one.)
  835. For instance, suppose you want to set tabs on
  836. your terminal, and find out the date
  837. and who's on the system every time you log in.
  838. Then you can put the three necessary commands
  839. (
  840. .C tabs ;
  841. .C date ;
  842. .C who )
  843. into a file, let's call it  ``xxx'',
  844. and then run it with
  845. either
  846. .B1
  847. sh xxx
  848. .B2
  849. or
  850. .B1
  851. sh <xxx
  852. .B2
  853. This says to run the shell with the file ``xxx'' as input.
  854. The effect is as if you had typed 
  855. the contents of ``xxx''
  856. on the terminal.
  857. (If this is to be a regular thing,
  858. you can eliminate the 
  859. need to type
  860. ``sh'';
  861. see
  862. .SE chmod (I)
  863. and
  864. .SE sh (I).)
  865. .PP
  866. The shell has quite a few other capabilities as well,
  867. some of which we'll get to in the section on programming.
  868.