1CP(P) POSIX Programmer's Manual CP(P)
2
3
4
6 cp - copy files
7
9 cp [-fip] source_file target_file
10
11 cp [-fip] source_file ... target
12
13 cp -R [-H | -L | -P][-fip] source_file ... target
14
15 cp -r [-H | -L | -P][-fip] source_file ... target
16
17
19 The first synopsis form is denoted by two operands, neither of which
20 are existing files of type directory. The cp utility shall copy the
21 contents of source_file (or, if source_file is a file of type symbolic
22 link, the contents of the file referenced by source_file) to the desti‐
23 nation path named by target_file.
24
25 The second synopsis form is denoted by two or more operands where the
26 -R or -r options are not specified and the first synopsis form is not
27 applicable. It shall be an error if any source_file is a file of type
28 directory, if target does not exist, or if target is a file of a type
29 defined by the System Interfaces volume of IEEE Std 1003.1-2001, but is
30 not a file of type directory. The cp utility shall copy the contents of
31 each source_file (or, if source_file is a file of type symbolic link,
32 the contents of the file referenced by source_file) to the destination
33 path named by the concatenation of target, a slash character, and the
34 last component of source_file.
35
36 The third and fourth synopsis forms are denoted by two or more operands
37 where the -R or -r options are specified. The cp utility shall copy
38 each file in the file hierarchy rooted in each source_file to a desti‐
39 nation path named as follows:
40
41 * If target exists and is a file of type directory, the name of the
42 corresponding destination path for each file in the file hierarchy
43 shall be the concatenation of target, a slash character, and the
44 pathname of the file relative to the directory containing
45 source_file.
46
47 * If target does not exist and two operands are specified, the name of
48 the corresponding destination path for source_file shall be target;
49 the name of the corresponding destination path for all other files
50 in the file hierarchy shall be the concatenation of target, a slash
51 character, and the pathname of the file relative to source_file.
52
53 It shall be an error if target does not exist and more than two oper‐
54 ands are specified, or if target exists and is a file of a type defined
55 by the System Interfaces volume of IEEE Std 1003.1-2001, but is not a
56 file of type directory.
57
58 In the following description, the term dest_file refers to the file
59 named by the destination path. The term source_file refers to the file
60 that is being copied, whether specified as an operand or a file in a
61 file hierarchy rooted in a source_file operand. If source_file is a
62 file of type symbolic link:
63
64 * If neither the -R nor -r options were specified, cp shall take
65 actions based on the type and contents of the file referenced by the
66 symbolic link, and not by the symbolic link itself.
67
68 * If the -R option was specified:
69
70 * If none of the options -H, -L, nor -P were specified, it is
71 unspecified which of -H, -L, or -P will be used as a default.
72
73 * If the -H option was specified, cp shall take actions based on
74 the type and contents of the file referenced by any symbolic link
75 specified as a source_file operand.
76
77 * If the -L option was specified, cp shall take actions based on
78 the type and contents of the file referenced by any symbolic link
79 specified as a source_file operand or any symbolic links encoun‐
80 tered during traversal of a file hierarchy.
81
82 * If the -P option was specified, cp shall copy any symbolic link
83 specified as a source_file operand and any symbolic links encoun‐
84 tered during traversal of a file hierarchy, and shall not follow
85 any symbolic links.
86
87 * If the -r option was specified, the behavior is implementation-
88 defined.
89
90 For each source_file, the following steps shall be taken:
91
92 1. If source_file references the same file as dest_file, cp may write
93 a diagnostic message to standard error; it shall do nothing more
94 with source_file and shall go on to any remaining files.
95
96 2. If source_file is of type directory, the following steps shall be
97 taken:
98
99 a. If neither the -R or -r options were specified, cp shall write
100 a diagnostic message to standard error, do nothing more with
101 source_file, and go on to any remaining files.
102
103 b. If source_file was not specified as an operand and source_file
104 is dot or dot-dot, cp shall do nothing more with source_file
105 and go on to any remaining files.
106
107 c. If dest_file exists and it is a file type not specified by the
108 System Interfaces volume of IEEE Std 1003.1-2001, the behavior
109 is implementation-defined.
110
111 d. If dest_file exists and it is not of type directory, cp shall
112 write a diagnostic message to standard error, do nothing more
113 with source_file or any files below source_file in the file
114 hierarchy, and go on to any remaining files.
115
116 e. If the directory dest_file does not exist, it shall be created
117 with file permission bits set to the same value as those of
118 source_file, modified by the file creation mask of the user if
119 the -p option was not specified, and then bitwise-inclusively
120 OR'ed with S_IRWXU. If dest_file cannot be created, cp shall
121 write a diagnostic message to standard error, do nothing more
122 with source_file, and go on to any remaining files. It is
123 unspecified if cp attempts to copy files in the file hierarchy
124 rooted in source_file.
125
126 f. The files in the directory source_file shall be copied to the
127 directory dest_file, taking the four steps (1 to 4) listed here
128 with the files as source_files.
129
130 g. If dest_file was created, its file permission bits shall be
131 changed (if necessary) to be the same as those of source_file,
132 modified by the file creation mask of the user if the -p option
133 was not specified.
134
135 h. The cp utility shall do nothing more with source_file and go on
136 to any remaining files.
137
138 3. If source_file is of type regular file, the following steps shall
139 be taken:
140
141 a. If dest_file exists, the following steps shall be taken:
142
143 i. If the -i option is in effect, the cp utility shall write
144 a prompt to the standard error and read a line from the
145 standard input. If the response is not affirmative, cp
146 shall do nothing more with source_file and go on to any
147 remaining files.
148
149 ii. A file descriptor for dest_file shall be obtained by per‐
150 forming actions equivalent to the open() function defined
151 in the System Interfaces volume of IEEE Std 1003.1-2001
152 called using dest_file as the path argument, and the bit‐
153 wise-inclusive OR of O_WRONLY and O_TRUNC as the oflag
154 argument.
155
156 iii. If the attempt to obtain a file descriptor fails and the
157 -f option is in effect, cp shall attempt to remove the
158 file by performing actions equivalent to the unlink()
159 function defined in the System Interfaces volume of
160 IEEE Std 1003.1-2001 called using dest_file as the path
161 argument. If this attempt succeeds, cp shall continue with
162 step 3b.
163
164 b. If dest_file does not exist, a file descriptor shall be
165 obtained by performing actions equivalent to the open() func‐
166 tion defined in the System Interfaces volume of
167 IEEE Std 1003.1-2001 called using dest_file as the path argu‐
168 ment, and the bitwise-inclusive OR of O_WRONLY and O_CREAT as
169 the oflag argument. The file permission bits of source_file
170 shall be the mode argument.
171
172 c. If the attempt to obtain a file descriptor fails, cp shall
173 write a diagnostic message to standard error, do nothing more
174 with source_file, and go on to any remaining files.
175
176 d. The contents of source_file shall be written to the file
177 descriptor. Any write errors shall cause cp to write a diag‐
178 nostic message to standard error and continue to step 3e.
179
180 e. The file descriptor shall be closed.
181
182 f. The cp utility shall do nothing more with source_file. If a
183 write error occurred in step 3d, it is unspecified if cp con‐
184 tinues with any remaining files. If no write error occurred in
185 step 3d, cp shall go on to any remaining files.
186
187 4. Otherwise, the following steps shall be taken:
188
189 a. If the -r option was specified, the behavior is implementation-
190 defined.
191
192 b. If the -R option was specified, the following steps shall be
193 taken:
194
195 i. The dest_file shall be created with the same file type as
196 source_file.
197
198 ii. If source_file is a file of type FIFO, the file permission
199 bits shall be the same as those of source_file, modified
200 by the file creation mask of the user if the -p option was
201 not specified. Otherwise, the permissions, owner ID, and
202 group ID of dest_file are implementation-defined.
203
204 If this creation fails for any reason, cp shall write a diag‐
205 nostic message to standard error, do nothing more with
206 source_file, and go on to any remaining files.
207
208 iii. If source_file is a file of type symbolic link, the path‐
209 name contained in dest_file shall be the same as the path‐
210 name contained in source_file.
211
212 If this fails for any reason, cp shall write a diagnostic mes‐
213 sage to standard error, do nothing more with source_file, and
214 go on to any remaining files.
215
216 If the implementation provides additional or alternate access control
217 mechanisms (see the Base Definitions volume of IEEE Std 1003.1-2001,
218 Section 4.4, File Access Permissions), their effect on copies of files
219 is implementation-defined.
220
222 The cp utility shall conform to the Base Definitions volume of
223 IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
224
225 The following options shall be supported:
226
227 -f If a file descriptor for a destination file cannot be obtained,
228 as described in step 3.a.ii., attempt to unlink the destination
229 file and proceed.
230
231 -H Take actions based on the type and contents of the file refer‐
232 enced by any symbolic link specified as a source_file operand.
233
234 -i Write a prompt to standard error before copying to any existing
235 destination file. If the response from the standard input is
236 affirmative, the copy shall be attempted; otherwise, it shall
237 not.
238
239 -L Take actions based on the type and contents of the file refer‐
240 enced by any symbolic link specified as a source_file operand or
241 any symbolic links encountered during traversal of a file hier‐
242 archy.
243
244 -P Take actions on any symbolic link specified as a source_file op‐
245 erand or any symbolic link encountered during traversal of a
246 file hierarchy.
247
248 -p Duplicate the following characteristics of each source file in
249 the corresponding destination file:
250
251 1. The time of last data modification and time of last access.
252 If this duplication fails for any reason, cp shall write a
253 diagnostic message to standard error.
254
255 2. The user ID and group ID. If this duplication fails for any
256 reason, it is unspecified whether cp writes a diagnostic
257 message to standard error.
258
259 3. The file permission bits and the S_ISUID and S_ISGID bits.
260 Other, implementation-defined, bits may be duplicated as
261 well. If this duplication fails for any reason, cp shall
262 write a diagnostic message to standard error.
263
264 If the user ID or the group ID cannot be duplicated, the file permis‐
265 sion bits S_ISUID and S_ISGID shall be cleared. If these bits are
266 present in the source file but are not duplicated in the destination
267 file, it is unspecified whether cp writes a diagnostic message to stan‐
268 dard error.
269
270 The order in which the preceding characteristics are duplicated is
271 unspecified. The dest_file shall not be deleted if these characteris‐
272 tics cannot be preserved.
273
274 -R Copy file hierarchies.
275
276 -r Copy file hierarchies. The treatment of special files is imple‐
277 mentation-defined.
278
279
280 Specifying more than one of the mutually-exclusive options -H, -L, and
281 -P shall not be considered an error. The last option specified shall
282 determine the behavior of the utility.
283
285 The following operands shall be supported:
286
287 source_file
288 A pathname of a file to be copied.
289
290 target_file
291 A pathname of an existing or nonexistent file, used for the out‐
292 put when a single file is copied.
293
294 target A pathname of a directory to contain the copied files.
295
296
298 The standard input shall be used to read an input line in response to
299 each prompt specified in the STDERR section. Otherwise, the standard
300 input shall not be used.
301
303 The input files specified as operands may be of any file type.
304
306 The following environment variables shall affect the execution of cp:
307
308 LANG Provide a default value for the internationalization variables
309 that are unset or null. (See the Base Definitions volume of
310 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
311 ables for the precedence of internationalization variables used
312 to determine the values of locale categories.)
313
314 LC_ALL If set to a non-empty string value, override the values of all
315 the other internationalization variables.
316
317 LC_COLLATE
318
319 Determine the locale for the behavior of ranges, equivalence
320 classes, and multi-character collating elements used in the
321 extended regular expression defined for the yesexpr locale key‐
322 word in the LC_MESSAGES category.
323
324 LC_CTYPE
325 Determine the locale for the interpretation of sequences of
326 bytes of text data as characters (for example, single-byte as
327 opposed to multi-byte characters in arguments and input files)
328 and the behavior of character classes used in the extended regu‐
329 lar expression defined for the yesexpr locale keyword in the
330 LC_MESSAGES category.
331
332 LC_MESSAGES
333 Determine the locale for the processing of affirmative responses
334 that should be used to affect the format and contents of diag‐
335 nostic messages written to standard error.
336
337 NLSPATH
338 Determine the location of message catalogs for the processing of
339 LC_MESSAGES .
340
341
343 Default.
344
346 Not used.
347
349 A prompt shall be written to standard error under the conditions speci‐
350 fied in the DESCRIPTION section. The prompt shall contain the destina‐
351 tion pathname, but its format is otherwise unspecified. Otherwise, the
352 standard error shall be used only for diagnostic messages.
353
355 The output files may be of any type.
356
358 None.
359
361 The following exit values shall be returned:
362
363 0 All files were copied successfully.
364
365 >0 An error occurred.
366
367
369 If cp is prematurely terminated by a signal or error, files or file
370 hierarchies may be only partially copied and files and directories may
371 have incorrect permissions or access and modification times.
372
373 The following sections are informative.
374
376 The difference between -R and -r is in the treatment by cp of file
377 types other than regular and directory. The original -r flag, for his‐
378 toric reasons, does not handle special files any differently from regu‐
379 lar files, but always reads the file and copies its contents. This has
380 obvious problems in the presence of special file types; for example,
381 character devices, FIFOs, and sockets. The -R option is intended to
382 recreate the file hierarchy and the -r option supports historical prac‐
383 tice. It was anticipated that a future version of this volume of
384 IEEE Std 1003.1-2001 would deprecate the -r option, and for that rea‐
385 son, there has been no attempt to fix its behavior with respect to
386 FIFOs or other file types where copying the file is clearly wrong. How‐
387 ever, some implementations support -r with the same abilities as the -R
388 defined in this volume of IEEE Std 1003.1-2001. To accommodate them as
389 well as systems that do not, the differences between -r and -R are
390 implementation-defined. Implementations may make them identical. The -r
391 option is marked obsolescent.
392
393 The set-user-ID and set-group-ID bits are explicitly cleared when files
394 are created. This is to prevent users from creating programs that are
395 set-user-ID or set-group-ID to them when copying files or to make set-
396 user-ID or set-group-ID files accessible to new groups of users. For
397 example, if a file is set-user-ID and the copy has a different group ID
398 than the source, a new group of users has execute permission to a set-
399 user-ID program than did previously. In particular, this is a problem
400 for superusers copying users' trees.
401
403 None.
404
406 The -i option exists on BSD systems, giving applications and users a
407 way to avoid accidentally removing files when copying. Although the 4.3
408 BSD version does not prompt if the standard input is not a terminal,
409 the standard developers decided that use of -i is a request for inter‐
410 action, so when the destination path exists, the utility takes instruc‐
411 tions from whatever responds on standard input.
412
413 The exact format of the interactive prompts is unspecified. Only the
414 general nature of the contents of prompts are specified because imple‐
415 mentations may desire more descriptive prompts than those used on his‐
416 torical implementations. Therefore, an application using the -i option
417 relies on the system to provide the most suitable dialog directly with
418 the user, based on the behavior specified.
419
420 The -p option is historical practice on BSD systems, duplicating the
421 time of last data modification and time of last access. This volume of
422 IEEE Std 1003.1-2001 extends it to preserve the user and group IDs, as
423 well as the file permissions. This requirement has obvious problems in
424 that the directories are almost certainly modified after being copied.
425 This volume of IEEE Std 1003.1-2001 requires that the modification
426 times be preserved. The statement that the order in which the charac‐
427 teristics are duplicated is unspecified is to permit implementations to
428 provide the maximum amount of security for the user. Implementations
429 should take into account the obvious security issues involved in set‐
430 ting the owner, group, and mode in the wrong order or creating files
431 with an owner, group, or mode different from the final value.
432
433 It is unspecified whether cp writes diagnostic messages when the user
434 and group IDs cannot be set due to the widespread practice of users
435 using -p to duplicate some portion of the file characteristics, indif‐
436 ferent to the duplication of others. Historic implementations only
437 write diagnostic messages on errors other than [EPERM].
438
439 The -r option is historical practice on BSD and BSD-derived systems,
440 copying file hierarchies as opposed to single files. This functional‐
441 ity is used heavily in historical applications, and its loss would sig‐
442 nificantly decrease consensus. The -R option was added as a close syn‐
443 onym to the -r option, selected for consistency with all other options
444 in this volume of IEEE Std 1003.1-2001 that do recursive directory
445 descent.
446
447 When a failure occurs during the copying of a file hierarchy, cp is
448 required to attempt to copy files that are on the same level in the
449 hierarchy or above the file where the failure occurred. It is unspeci‐
450 fied if cp shall attempt to copy files below the file where the failure
451 occurred (which cannot succeed in any case).
452
453 Permissions, owners, and groups of created special file types have been
454 deliberately left as implementation-defined. This is to allow systems
455 to satisfy special requirements (for example, allowing users to create
456 character special devices, but requiring them to be owned by a certain
457 group). In general, it is strongly suggested that the permissions,
458 owner, and group be the same as if the user had run the historical
459 mknod, ln, or other utility to create the file. It is also probable
460 that additional privileges are required to create block, character, or
461 other implementation-defined special file types.
462
463 Additionally, the -p option explicitly requires that all set-user-ID
464 and set-group-ID permissions be discarded if any of the owner or group
465 IDs cannot be set. This is to keep users from unintentionally giving
466 away special privilege when copying programs.
467
468 When creating regular files, historical versions of cp use the mode of
469 the source file as modified by the file mode creation mask. Other
470 choices would have been to use the mode of the source file unmodified
471 by the creation mask or to use the same mode as would be given to a new
472 file created by the user (plus the execution bits of the source file)
473 and then modify it by the file mode creation mask. In the absence of
474 any strong reason to change historic practice, it was in large part
475 retained.
476
477 When creating directories, historical versions of cp use the mode of
478 the source directory, plus read, write, and search bits for the owner,
479 as modified by the file mode creation mask. This is done so that cp can
480 copy trees where the user has read permission, but the owner does not.
481 A side effect is that if the file creation mask denies the owner per‐
482 missions, cp fails. Also, once the copy is done, historical versions of
483 cp set the permissions on the created directory to be the same as the
484 source directory, unmodified by the file creation mask.
485
486 This behavior has been modified so that cp is always able to create the
487 contents of the directory, regardless of the file creation mask. After
488 the copy is done, the permissions are set to be the same as the source
489 directory, as modified by the file creation mask. This latter change
490 from historical behavior is to prevent users from accidentally creating
491 directories with permissions beyond those they would normally set and
492 for consistency with the behavior of cp in creating files.
493
494 It is not a requirement that cp detect attempts to copy a file to
495 itself; however, implementations are strongly encouraged to do so. His‐
496 torical implementations have detected the attempt in most cases.
497
498 There are two methods of copying subtrees in this volume of
499 IEEE Std 1003.1-2001. The other method is described as part of the pax
500 utility (see pax ). Both methods are historical practice. The cp util‐
501 ity provides a simpler, more intuitive interface, while pax offers a
502 finer granularity of control. Each provides additional functionality to
503 the other; in particular, pax maintains the hard-link structure of the
504 hierarchy, while cp does not. It is the intention of the standard
505 developers that the results be similar (using appropriate option combi‐
506 nations in both utilities). The results are not required to be identi‐
507 cal; there seemed insufficient gain to applications to balance the dif‐
508 ficulty of implementations having to guarantee that the results would
509 be exactly identical.
510
511 The wording allowing cp to copy a directory to implementation-defined
512 file types not specified by the System Interfaces volume of
513 IEEE Std 1003.1-2001 is provided so that implementations supporting
514 symbolic links are not required to prohibit copying directories to sym‐
515 bolic links. Other extensions to the System Interfaces volume of
516 IEEE Std 1003.1-2001 file types may need to use this loophole as well.
517
519 The -r option may be removed; use -R instead.
520
522 mv , find , ln , pax , the System Interfaces volume of
523 IEEE Std 1003.1-2001, open(), unlink()
524
526 Portions of this text are reprinted and reproduced in electronic form
527 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
528 -- Portable Operating System Interface (POSIX), The Open Group Base
529 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
530 Electrical and Electronics Engineers, Inc and The Open Group. In the
531 event of any discrepancy between this version and the original IEEE and
532 The Open Group Standard, the original IEEE and The Open Group Standard
533 is the referee document. The original Standard can be obtained online
534 at http://www.opengroup.org/unix/online.html .
535
536
537
538IEEE/The Open Group 2003 CP(P)