home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / doc / adv.ed / ae5 < prev    next >
Encoding:
Text File  |  1979-01-10  |  5.8 KB  |  324 lines

  1. .NH
  2. CUT AND PASTE WITH UNIX COMMANDS
  3. .PP
  4. One editing area in which non-programmers
  5. seem not very confident
  6. is in what might be called
  7. `cut and paste' operations _
  8. changing the name of a file,
  9. making a copy of a file somewhere else,
  10. moving a few lines from one place to another in a file,
  11. inserting one file in the middle of another,
  12. splitting a file into pieces,
  13. and
  14. splicing two or more files together.
  15. .PP
  16. Yet most of these operations are actually quite easy,
  17. if you keep your wits about you
  18. and go cautiously.
  19. The next several sections talk about cut and paste.
  20. We will begin with the
  21. .UX
  22. commands
  23. for moving entire files around,
  24. then discuss
  25. .UL ed
  26. commands
  27. for operating on pieces of files.
  28. .SH
  29. Changing the Name of a File
  30. .PP
  31. You have a file named 
  32. `memo'
  33. and you want it to be called
  34. `paper'
  35. instead.
  36. How is it done?
  37. .PP
  38. The
  39. .UX
  40. program that renames files
  41. is called
  42. .UL mv
  43. (for `move');
  44. it `moves' the file from one name to another, like this:
  45. .P1
  46. mv  memo  paper
  47. .P2
  48. That's all there is to it:
  49. .UL mv
  50. from the old name to the new name.
  51. .P1
  52. mv  oldname  newname
  53. .P2
  54. Warning: if there is already a file around with the new name,
  55. its present contents will be
  56. silently
  57. clobbered
  58. by the information from the other file.
  59. The one exception is that you can't move a file
  60. to itself _
  61. .P1
  62. mv  x  x
  63. .P2
  64. is illegal.
  65. .SH
  66. Making a Copy of a File
  67. .PP
  68. Sometimes what you want is a copy of a file _
  69. an entirely fresh version.
  70. This might be because you want to work on a file, and
  71. yet save a copy in case something gets fouled up,
  72. or just because you're paranoid.
  73. .PP
  74. In any case, the way to do it is with the
  75. .UL cp
  76. command.
  77. .UL cp \& (
  78. stands for `copy';
  79. the
  80. .UC UNIX
  81. system
  82. is big on short command names,
  83. which are appreciated by heavy users,
  84. but sometimes a strain for novices.)
  85. Suppose you have a file called
  86. `good'
  87. and
  88. you want to save a copy before you make some
  89. dramatic editing changes.
  90. Choose a name _
  91. `savegood'
  92. might be acceptable _ then type
  93. .P1
  94. cp  good  savegood
  95. .P2
  96. This copies
  97. `good'
  98. onto
  99. `savegood',
  100. and you now have two identical copies of the file
  101. `good'.
  102. (If
  103. `savegood'
  104. previously contained something,
  105. it gets overwritten.)
  106. .PP
  107. Now if you decide at some time that you want to get
  108. back to the original state of
  109. `good',
  110. you can say
  111. .P1
  112. mv  savegood  good
  113. .P2
  114. (if you're not interested in
  115. `savegood'
  116. any more), or
  117. .P1
  118. cp  savegood  good
  119. .P2
  120. if you still want to retain a safe copy.
  121. .PP
  122. In summary, 
  123. .UL mv
  124. just renames a file;
  125. .UL cp
  126. makes a duplicate copy.
  127. Both of them clobber the `target' file
  128. if it already exists, so you had better
  129. be sure that's what you want to do
  130. .ul
  131. before
  132. you do it.
  133. .SH
  134. Removing a File
  135. .PP
  136. If you decide you are really done with a file
  137. forever, you can remove it
  138. with the
  139. .UL rm
  140. command:
  141. .P1
  142. rm  savegood
  143. .P2
  144. throws away (irrevocably) the file called
  145. `savegood'.
  146. .SH
  147. Putting Two or More Files Together
  148. .PP
  149. The next step is the familiar one of collecting two or more
  150. files into one big one.
  151. This will be needed, for example,
  152. when the author of a paper
  153. decides that several sections need to be combined
  154. into one.
  155. There are several ways to do it,
  156. of which the cleanest, once you get used to it,
  157. is a program called
  158. .UL cat .
  159. (Not 
  160. .ul
  161. all
  162. .UC UNIX 
  163. programs have two-letter names.)
  164. .UL cat
  165. is short for
  166. `concatenate', which is exactly
  167. what we want to do.
  168. .PP
  169. Suppose the job is to combine the files
  170. `file1'
  171. and
  172. `file2'
  173. into a single file called
  174. `bigfile'.
  175. If you say
  176. .P1
  177. cat  file
  178. .P2
  179. the contents of
  180. `file'
  181. will get printed on your terminal.
  182. If you say
  183. .P1
  184. cat  file1  file2
  185. .P2
  186. the contents of
  187. `file1'
  188. and then the contents of
  189. `file2'
  190. will
  191. .ul
  192. both
  193. be printed on your terminal,
  194. in that order.
  195. So
  196. .UL cat
  197. combines the files, all right,
  198. but it's not much help to print them on the terminal _
  199. we want them in 
  200. `bigfile'.
  201. .PP
  202. Fortunately, there is a way.
  203. You can tell
  204. the system
  205. that instead of printing on your terminal,
  206. you want the same information put in a file. 
  207. The way to do it is to add to the command line
  208. the character
  209. .UL >
  210. and the name of the file
  211. where you want the output to go.
  212. Then you can say
  213. .P1
  214. cat  file1  file2  >bigfile
  215. .P2
  216. and the job is done.
  217. (As with
  218. .UL cp
  219. and
  220. .UL mv ,
  221. you're putting something into
  222. `bigfile',
  223. and anything that was already there is destroyed.)
  224. .PP
  225. This ability to
  226. `capture' the output of a program
  227. is one of the most useful aspects of
  228. the 
  229. .UC UNIX
  230. system.
  231. Fortunately it's not limited to the
  232. .UL cat 
  233. program _
  234. you can use it with 
  235. .ul
  236. any
  237. program that prints on your terminal.
  238. We'll see some more uses for it in a moment.
  239. .PP
  240. Naturally, you can combine several files,
  241. not just two:
  242. .P1
  243. cat  file1  file2  file3  ...  >bigfile
  244. .P2
  245. collects a whole bunch.
  246. .PP
  247. Question:
  248. is there any difference between
  249. .P1
  250. cp  good  savegood
  251. .P2
  252. and
  253. .P1
  254. cat  good  >savegood
  255. .P2
  256. Answer: for most purposes, no.
  257. You might reasonably ask why there are two programs
  258. in that case,
  259. since
  260. .UL cat
  261. is obviously all you need.
  262. The answer is that 
  263. .UL cp
  264. will do some other things as well,
  265. which you can investigate for yourself
  266. by reading the manual.
  267. For now we'll stick to simple usages.
  268. .SH
  269. Adding Something to the End of a File
  270. .PP
  271. Sometimes you want to add one file to the end of another.
  272. We have enough building blocks now that you can do it;
  273. in fact before reading further it would be valuable
  274. if you figured out how.
  275. To be specific,
  276. how would you use
  277. .UL cp ,
  278. .UL mv
  279. and/or
  280. .UL cat
  281. to add the file
  282. `good1'
  283. to the end of the file
  284. `good'?
  285. .PP
  286. You could try
  287. .P1
  288. cat  good  good1  >temp
  289. mv  temp  good
  290. .P2
  291. which is probably most direct.
  292. You should also understand why
  293. .P1
  294. cat  good  good1  >good
  295. .P2
  296. doesn't work.
  297. (Don't practice with a good `good'!)
  298. .PP
  299. The easy way is to use a variant of
  300. .UL > ,
  301. called
  302. .UL >> .
  303. In fact,
  304. .UL >> 
  305. is identical to
  306. .UL >
  307. except that instead of clobbering the old file,
  308. it simply tacks stuff on at the end.
  309. Thus you could say
  310. .P1
  311. cat  good1  >>good
  312. .P2
  313. and
  314. `good1'
  315. is added to the end of
  316. `good'.
  317. (And if
  318. `good'
  319. didn't exist,
  320. this makes a copy of
  321. `good1'
  322. called
  323. `good'.)
  324.