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