xref: /original-bsd/bin/ed/USD.doc/advanced/ae5 (revision 95ecee29)
%sccs.include.proprietary.roff%

@(#)ae5 8.1 (Berkeley) 08/14/93

CUT AND PASTE WITH UNIX COMMANDS

One editing area in which non-programmers seem not very confident is in what might be called `cut and paste' operations _ changing the name of a file, making a copy of a file somewhere else, moving a few lines from one place to another in a file, inserting one file in the middle of another, splitting a file into pieces, and splicing two or more files together.

Yet most of these operations are actually quite easy, if you keep your wits about you and go cautiously. The next several sections talk about cut and paste. We will begin with the X commands for moving entire files around, then discuss ed commands for operating on pieces of files.

Changing the Name of a File

You have a file named `memo' and you want it to be called `paper' instead. How is it done?

The X program that renames files is called mv (for `move'); it `moves' the file from one name to another, like this:

1 mv memo paper

2 That's all there is to it: mv from the old name to the new name.

1 mv oldname newname

2 Warning: if there is already a file around with the new name, its present contents will be silently clobbered by the information from the other file. The one exception is that you can't move a file to itself _

1 mv x x

2 is illegal.

Making a Copy of a File

Sometimes what you want is a copy of a file _ an entirely fresh version. This might be because you want to work on a file, and yet save a copy in case something gets fouled up, or just because you're paranoid.

In any case, the way to do it is with the cp command. cp ( stands for `copy'; the UNIX system is big on short command names, which are appreciated by heavy users, but sometimes a strain for novices.) Suppose you have a file called `good' and you want to save a copy before you make some dramatic editing changes. Choose a name _ `savegood' might be acceptable _ then type

1 cp good savegood

2 This copies `good' onto `savegood', and you now have two identical copies of the file `good'. (If `savegood' previously contained something, it gets overwritten.)

Now if you decide at some time that you want to get back to the original state of `good', you can say

1 mv savegood good

2 (if you're not interested in `savegood' any more), or

1 cp savegood good

2 if you still want to retain a safe copy.

In summary, mv just renames a file; cp makes a duplicate copy. Both of them clobber the `target' file if it already exists, so you had better be sure that's what you want to do .ul before you do it.

Removing a File

If you decide you are really done with a file forever, you can remove it with the rm command:

1 rm savegood

2 throws away (irrevocably) the file called `savegood'.

Putting Two or More Files Together

The next step is the familiar one of collecting two or more files into one big one. This will be needed, for example, when the author of a paper decides that several sections need to be combined into one. There are several ways to do it, of which the cleanest, once you get used to it, is a program called cat . (Not .ul all UNIX programs have two-letter names.) cat is short for `concatenate', which is exactly what we want to do.

Suppose the job is to combine the files `file1' and `file2' into a single file called `bigfile'. If you say

1 cat file

2 the contents of `file' will get printed on your terminal. If you say

1 cat file1 file2

2 the contents of `file1' and then the contents of `file2' will .ul both be printed on your terminal, in that order. So cat combines the files, all right, but it's not much help to print them on the terminal _ we want them in `bigfile'.

Fortunately, there is a way. You can tell the system that instead of printing on your terminal, you want the same information put in a file. The way to do it is to add to the command line the character > and the name of the file where you want the output to go. Then you can say

1 cat file1 file2 >bigfile

2 and the job is done. (As with cp and mv , you're putting something into `bigfile', and anything that was already there is destroyed.)

This ability to `capture' the output of a program is one of the most useful aspects of the UNIX system. Fortunately it's not limited to the cat program _ you can use it with .ul any program that prints on your terminal. We'll see some more uses for it in a moment.

Naturally, you can combine several files, not just two:

1 cat file1 file2 file3 ... >bigfile

2 collects a whole bunch.

Question: is there any difference between

1 cp good savegood

2 and

1 cat good >savegood

2 Answer: for most purposes, no. You might reasonably ask why there are two programs in that case, since cat is obviously all you need. The answer is that cp can do some other things as well, which you can investigate for yourself by reading the manual. For now we'll stick to simple usages.

Adding Something to the End of a File

Sometimes you want to add one file to the end of another. We have enough building blocks now that you can do it; in fact before reading further it would be valuable if you figured out how. To be specific, how would you use cp , mv and/or cat to add the file `good1' to the end of the file `good'?

You could try

1 cat good good1 >temp mv temp good

2 which is probably most direct. You should also understand why

1 cat good good1 >good

2 doesn't work. (Don't practice with a good `good'!)

The easy way is to use a variant of > , called >> . In fact, >> is identical to > except that instead of clobbering the old file, it simply tacks stuff on at the end. Thus you could say

1 cat good1 >>good

2 and `good1' is added to the end of `good'. (And if `good' didn't exist, this makes a copy of `good1' called `good'.)