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