1 .\"
   2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
   3 .\" permission to reproduce portions of its copyrighted documentation.
   4 .\" Original documentation from The Open Group can be obtained online at
   5 .\" http://www.opengroup.org/bookstore/.
   6 .\"
   7 .\" The Institute of Electrical and Electronics Engineers and The Open
   8 .\" Group, have given us permission to reprint portions of their
   9 .\" documentation.
  10 .\"
  11 .\" In the following statement, the phrase ``this text'' refers to portions
  12 .\" of the system documentation.
  13 .\"
  14 .\" Portions of this text are reprinted and reproduced in electronic form
  15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
  16 .\" Standard for Information Technology -- Portable Operating System
  17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
  18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
  19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
  20 .\" between these versions and the original IEEE and The Open Group
  21 .\" Standard, the original IEEE and The Open Group Standard is the referee
  22 .\" document.  The original Standard can be obtained online at
  23 .\" http://www.opengroup.org/unix/online.html.
  24 .\"
  25 .\" This notice shall appear on any product containing this material.
  26 .\"
  27 .\" The contents of this file are subject to the terms of the
  28 .\" Common Development and Distribution License (the "License").
  29 .\" You may not use this file except in compliance with the License.
  30 .\"
  31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  32 .\" or http://www.opensolaris.org/os/licensing.
  33 .\" See the License for the specific language governing permissions
  34 .\" and limitations under the License.
  35 .\"
  36 .\" When distributing Covered Code, include this CDDL HEADER in each
  37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  38 .\" If applicable, add the following below this CDDL HEADER, with the
  39 .\" fields enclosed by brackets "[]" replaced with your own identifying
  40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  41 .\"
  42 .\"
  43 .\" Copyright 1989 AT&T
  44 .\" Copyright (c) 1992, X/Open Company Limited All Rights Reserved
  45 .\" Portions Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved
  46 .\" Copyright 2013 Nexenta Systems, Inc. All rights reserved.
  47 .\"
  48 .TH CP 1 "May 17, 2020"
  49 .SH NAME
  50 cp \- copy files
  51 .SH SYNOPSIS
  52 .nf
  53 \fB/usr/bin/cp\fR [\fB-afip@/\fR] \fIsource_file\fR \fItarget_file\fR
  54 .fi
  55 
  56 .LP
  57 .nf
  58 \fB/usr/bin/cp\fR [\fB-afip@/\fR] \fIsource_file\fR... \fItarget\fR
  59 .fi
  60 
  61 .LP
  62 .nf
  63 \fB/usr/bin/cp\fR [\fB-r\fR | \fB-R\fR [\fB-H\fR | \fB-L\fR | \fB-P\fR]] [\fB-afip@/\fR] \fIsource_dir\fR... \fItarget\fR
  64 .fi
  65 
  66 .LP
  67 .nf
  68 \fB/usr/bin/cp\fR [\fB-R\fR | \fB-R\fR [\fB-H\fR | \fB-L\fR | \fB-P\fR]] [\fB-afip@/\fR] \fIsource_dir\fR... \fItarget\fR
  69 .fi
  70 
  71 .LP
  72 .nf
  73 \fB/usr/xpg4/bin/cp\fR [\fB-afip@/\fR] \fIsource_file\fR \fItarget_file\fR
  74 .fi
  75 
  76 .LP
  77 .nf
  78 \fB/usr/xpg4/bin/cp\fR [\fB-afip@/\fR] \fIsource_file\fR... \fItarget\fR
  79 .fi
  80 
  81 .LP
  82 .nf
  83 \fB/usr/xpg4/bin/cp\fR [\fB-r\fR | \fB-R\fR [\fB-H\fR | \fB-L\fR | \fB-P\fR]] [\fB-afip@/\fR] \fIsource_dir\fR... \fItarget\fR
  84 .fi
  85 
  86 .LP
  87 .nf
  88 \fB/usr/xpg4/bin/cp\fR [\fB-R\fR | \fB-R\fR [\fB-H\fR | \fB-L\fR | \fB-P\fR]] [\fB-afip@/\fR] \fIsource_dir\fR... \fItarget\fR
  89 .fi
  90 
  91 .SH DESCRIPTION
  92 In the first synopsis form, neither \fIsource_file\fR nor \fItarget_file\fR are
  93 directory files, nor can they have the same name. The \fBcp\fR utility copies
  94 the contents of \fIsource_file\fR to the destination path named by
  95 \fItarget_file\fR. If \fItarget_file\fR exists, \fBcp\fR overwrites its
  96 contents, but the mode (and \fBACL\fR if applicable), owner, and group
  97 associated with it are not changed. The last modification time of
  98 \fItarget_file\fR and the last access time of \fIsource_file\fR are set to the
  99 time the copy was made. If \fItarget_file\fR does not exist, \fBcp\fR creates a
 100 new file named \fItarget_file\fR that has the same mode as \fIsource_file\fR
 101 except that the sticky bit is not set unless the user is super-user. In this
 102 case, the owner and group of \fItarget_file\fR are those of the user, unless
 103 the setgid bit is set on the directory containing the newly created file. If
 104 the directory's setgid bit is set, the newly created file has the group of the
 105 containing directory rather than of the creating user. If \fItarget_file\fR is
 106 a link to another file, \fBcp\fR overwrites the link destination with the
 107 contents of \fIsource_file\fR; the link(s) from \fItarget_file\fR remains.
 108 .sp
 109 .LP
 110 In the second synopsis form, one or more \fIsource_file\fRs are copied to the
 111 directory specified by \fItarget\fR. It is an error if any \fIsource_file\fR is
 112 a file of type directory, if \fItarget\fR either does not exist or is not a
 113 directory.
 114 .sp
 115 .LP
 116 In the third or fourth synopsis forms, one or more directories specified by
 117 \fIsource_dir\fR are copied to the directory specified by \fItarget\fR. Either
 118 the \fB-r\fR or \fB-R\fR must be specified. For each \fIsource_dir\fR, \fBcp\fR
 119 copies all files and subdirectories.
 120 .SH OPTIONS
 121 The following options are supported for both \fB/usr/bin/cp\fR and
 122 \fB/usr/xpg4/bin/cp\fR:
 123 .sp
 124 .ne 2
 125 .na
 126 \fB\fB-a\fR\fR
 127 .ad
 128 .RS 6n
 129 Archive mode. Same as -RpP.
 130 .RE
 131 
 132 .sp
 133 .ne 2
 134 .na
 135 \fB\fB-f\fR\fR
 136 .ad
 137 .RS 6n
 138 Unlink. If a file descriptor for a destination file cannot be obtained, this
 139 option attempts to unlink the destination file and proceed.
 140 .RE
 141 
 142 .sp
 143 .ne 2
 144 .na
 145 \fB\fB-H\fR\fR
 146 .ad
 147 .RS 6n
 148 Takes actions based on the type and contents of the file referenced by any
 149 symbolic link specified as a \fIsource_file\fR operand.
 150 .sp
 151 If the \fIsource_file\fR operand is a symbolic link, then \fBcp\fR copies the
 152 file referenced by the symbolic link for the \fIsource_file\fR operand. All
 153 other symbolic links encountered during traversal of a file hierarchy are
 154 preserved.
 155 .RE
 156 
 157 .sp
 158 .ne 2
 159 .na
 160 \fB\fB-i\fR\fR
 161 .ad
 162 .RS 6n
 163 Interactive. \fBcp\fR prompts for confirmation whenever the copy would
 164 overwrite an existing \fItarget\fR. An affirmative response means that the copy
 165 should proceed. Any other answer prevents \fBcp\fR from overwriting
 166 \fItarget\fR.
 167 .RE
 168 
 169 .sp
 170 .ne 2
 171 .na
 172 \fB\fB-L\fR\fR
 173 .ad
 174 .RS 6n
 175 Takes actions based on the type and contents of the file referenced by any
 176 symbolic link specified as a \fIsource_file\fR operand or any symbolic links
 177 encountered during traversal of a file hierarchy.
 178 .sp
 179 Copies files referenced by symbolic links. Symbolic links encountered during
 180 traversal of a file hierarchy are not preserved.
 181 .RE
 182 
 183 .sp
 184 .ne 2
 185 .na
 186 \fB\fB-p\fR\fR
 187 .ad
 188 .RS 6n
 189 Preserve. The \fBcp\fR utility duplicates not only the contents of
 190 \fIsource_file\fR, but also attempts to preserve its ACL, access and
 191 modification times, extended attributes, extended system attributes, file mode,
 192 and owner and group ids.
 193 .sp
 194 If \fBcp\fR is unable to preserve the access and modification times, extended
 195 attributes, or the file mode, \fBcp\fR does not consider it a failure. If
 196 \fBcp\fR is unable to preserve the owner and group id, the copy does not fail,
 197 but \fBcp\fR silently clears the \fBS_ISUID\fR and \fBS_ISGID\fR bits from the
 198 file mode of the target. The copy fails if \fBcp\fR is unable to clear these
 199 bits. If \fBcp\fR is unable to preserve the ACL or extended system attributes,
 200 the copy fails. If the copy fails, then a diagnostic message is written to
 201 \fBstderr\fR and (after processing any remaining operands) \fBcp\fR exits with
 202 a \fBnon-zero\fR exit status.
 203 .RE
 204 
 205 .sp
 206 .ne 2
 207 .na
 208 \fB\fB-P\fR\fR
 209 .ad
 210 .RS 6n
 211 Takes actions on any symbolic link specified as a \fIsource_file\fR operand or
 212 any symbolic link encountered during traversal of a file hierarchy.
 213 .sp
 214 Copies symbolic links. Symbolic links encountered during traversal of a file
 215 hierarchy are preserved.
 216 .RE
 217 
 218 .sp
 219 .ne 2
 220 .na
 221 \fB\fB-r\fR\fR
 222 .ad
 223 .RS 6n
 224 Recursive. \fBcp\fR copies the directory and all its files, including any
 225 subdirectories and their files to \fItarget\fR. Unless the \fB-H\fR, \fB-L\fR,
 226 or \fB-P\fR option is specified, the \fB-L\fR option is used as the default
 227 mode.
 228 .RE
 229 
 230 .sp
 231 .ne 2
 232 .na
 233 \fB\fB-R\fR\fR
 234 .ad
 235 .RS 6n
 236 Same as \fB-r\fR, except pipes are replicated, not read from.
 237 .RE
 238 
 239 .sp
 240 .ne 2
 241 .na
 242 \fB\fB-@\fR\fR
 243 .ad
 244 .RS 6n
 245 Preserves extended attributes. \fBcp\fR attempts to copy all of the source
 246 file's extended attributes along with the file data to the destination file.
 247 .RE
 248 
 249 .sp
 250 .ne 2
 251 .na
 252 \fB\fB-/\fR\fR
 253 .ad
 254 .RS 6n
 255 Preserves extended attributes and extended system attributes. Along with the
 256 file's data, the \fBcp\fR utility attempts to copy extended attributes and
 257 extended system attributes from each source file, and extended system
 258 attributes associated with extended attributes to the destination file. If
 259 \fBcp\fR is unable to copy extended attributes or extended system attributes,
 260 then a diagnostic message is written to \fBstderr\fR and (after processing any
 261 remaining operands) exits with a \fBnon-zero\fR exit status.
 262 .RE
 263 
 264 .sp
 265 .LP
 266 Specifying more than one of the mutually-exclusive options \fB-H\fR, \fB-L\fR,
 267 and \fB-P\fR is not considered an error. The last option specified determines
 268 the behavior of the utility.
 269 .SS "/usr/bin/cp"
 270 If the \fB-p\fR option is specified with either the \fB-@\fR option or the
 271 \fB-/\fR option, \fB/usr/bin/cp\fR behaves as follows
 272 .RS +4
 273 .TP
 274 .ie t \(bu
 275 .el o
 276 When both \fB-p\fR and \fB-@\fR are specified in any order, the copy fails if
 277 extended attributes cannot be copied.
 278 .RE
 279 .RS +4
 280 .TP
 281 .ie t \(bu
 282 .el o
 283 When both \fB-p\fR and \fB-/\fR are specified in any order, the copy fails if
 284 extended system attributes cannot be copied.
 285 .RE
 286 .SS "/usr/xpg4/bin/cp"
 287 If the \fB-p\fR option is specified with either the \fB-@\fR option or the
 288 \fB-/\fR option, \fB/usr/xpg4/bin/cp\fR behaves as follows:
 289 .RS +4
 290 .TP
 291 .ie t \(bu
 292 .el o
 293 When both \fB-p\fR and \fB-@\fR are specified, the last option specified
 294 determines whether the copy fails if extended attributes cannot be preserved.
 295 .RE
 296 .RS +4
 297 .TP
 298 .ie t \(bu
 299 .el o
 300 When both \fB-p\fR and \fB-/\fR are specified, the last option specified
 301 determines whether the copy fails if extended system attributes cannot be
 302 preserved.
 303 .RE
 304 .SH OPERANDS
 305 The following operands are supported:
 306 .sp
 307 .ne 2
 308 .na
 309 \fB\fIsource_file\fR\fR
 310 .ad
 311 .RS 15n
 312 A pathname of a regular file to be copied.
 313 .RE
 314 
 315 .sp
 316 .ne 2
 317 .na
 318 \fB\fIsource_dir\fR\fR
 319 .ad
 320 .RS 15n
 321 A pathname of a directory to be copied.
 322 .RE
 323 
 324 .sp
 325 .ne 2
 326 .na
 327 \fB\fItarget_file\fR\fR
 328 .ad
 329 .RS 15n
 330 A pathname of an existing or non-existing file, used for the output when a
 331 single file is copied.
 332 .RE
 333 
 334 .sp
 335 .ne 2
 336 .na
 337 \fB\fItarget\fR\fR
 338 .ad
 339 .RS 15n
 340 A pathname of a directory to contain the copied files.
 341 .RE
 342 
 343 .SH USAGE
 344 See \fBlargefile\fR(5) for the description of the behavior of \fBcp\fR when
 345 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
 346 .SH EXAMPLES
 347 \fBExample 1 \fRCopying a File
 348 .sp
 349 .LP
 350 The following example copies a file:
 351 
 352 .sp
 353 .in +2
 354 .nf
 355 example% cp goodies goodies.old
 356 
 357 example% ls goodies*
 358 goodies goodies.old
 359 .fi
 360 .in -2
 361 .sp
 362 
 363 .LP
 364 \fBExample 2 \fRCopying a List of Files
 365 .sp
 366 .LP
 367 The following example copies a list of files to a destination directory:
 368 
 369 .sp
 370 .in +2
 371 .nf
 372 example% cp ~/src/* /tmp
 373 .fi
 374 .in -2
 375 .sp
 376 
 377 .LP
 378 \fBExample 3 \fRCopying a Directory
 379 .sp
 380 .LP
 381 The following example copies a directory, first to a new, and then to an
 382 existing destination directory
 383 
 384 .sp
 385 .in +2
 386 .nf
 387 example% ls ~/bkup
 388 /usr/example/fred/bkup not found
 389 
 390 example% cp \fB-r\fR ~/src ~/bkup
 391 
 392 example% ls \fB-R\fR ~/bkup
 393 x.c y.c z.sh
 394 
 395 example% cp \fB-r\fR ~/src ~/bkup
 396 
 397 example% ls \fB-R\fR ~/bkup
 398 src x.c y.c z.sh
 399 src:
 400 x.c y.c z.s
 401 .fi
 402 .in -2
 403 .sp
 404 
 405 .LP
 406 \fBExample 4 \fRCopying Extended File System Attributes
 407 .sp
 408 .LP
 409 The following example copies extended file system attributes:
 410 
 411 .sp
 412 .in +2
 413 .nf
 414 $ ls -/ c file1
 415 -rw-r--r--   1 foo   staff          0 Oct 29 20:04 file1
 416                 {AH-----m--}
 417 
 418 $ cp -/ file1 file2
 419 $ ls -/c file2
 420 -rw-r--r--   1 foo  staff          0 Oct 29 20:17 file2
 421                 {AH-----m--}
 422 .fi
 423 .in -2
 424 .sp
 425 
 426 .LP
 427 \fBExample 5 \fRFailing to Copy Extended System Attributes
 428 .sp
 429 .LP
 430 The following example fails to copy extended system attributes:
 431 
 432 .sp
 433 .in +2
 434 .nf
 435 $ ls -/c file1
 436 -rw-r--r--   1 foo    staff          0 Oct 29 20:04 file1
 437                 {AH-----m--}
 438 
 439 $ cp -/ file1 /tmp
 440 cp: Failed to copy extended system attributes from file1 to /tmp/file1
 441 
 442 
 443 $ ls -/c /tmp/file1
 444 -rw-r--r--   1 foo    staff          0 Oct 29 20:09 /tmp/file1
 445                 {}
 446 .fi
 447 .in -2
 448 .sp
 449 
 450 .SH ENVIRONMENT VARIABLES
 451 See \fBenviron\fR(5) for descriptions of the following environment variables
 452 that affect the execution of \fBcp\fR: \fBLANG\fR, \fBLC_ALL\fR,
 453 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
 454 .sp
 455 .LP
 456 Affirmative responses are processed using the extended regular expression
 457 defined for the \fByesexpr\fR keyword in the \fBLC_MESSAGES\fR category of the
 458 user's locale. The locale specified in the \fBLC_COLLATE\fR category defines
 459 the behavior of ranges, equivalence classes, and multi-character collating
 460 elements used in the expression defined for \fByesexpr\fR. The locale specified
 461 in \fBLC_CTYPE\fR determines the locale for interpretation of sequences of
 462 bytes of text data a characters, the behavior of character classes used in the
 463 expression defined for the \fByesexpr\fR. See \fBlocale\fR(5).
 464 .SH EXIT STATUS
 465 The following exit values are returned:
 466 .sp
 467 .ne 2
 468 .na
 469 \fB\fB0\fR\fR
 470 .ad
 471 .RS 6n
 472 All files were copied successfully.
 473 .RE
 474 
 475 .sp
 476 .ne 2
 477 .na
 478 \fB\fB>0\fR\fR
 479 .ad
 480 .RS 6n
 481 An error occurred.
 482 .RE
 483 
 484 .SH ATTRIBUTES
 485 See \fBattributes\fR(5) for descriptions of the following attributes:
 486 .SS "/usr/bin/cp"
 487 .TS
 488 box;
 489 c | c
 490 l | l .
 491 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 492 _
 493 CSI     Enabled
 494 _
 495 Interface Stability     Committed
 496 .TE
 497 
 498 .SS "/usr/xpg4/bin/cp"
 499 .TS
 500 box;
 501 c | c
 502 l | l .
 503 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 504 _
 505 CSI     Enabled
 506 _
 507 Interface Stability     Committed
 508 .TE
 509 
 510 .SH SEE ALSO
 511 \fBchmod\fR(1), \fBchown\fR(1), \fBsetfacl\fR(1), \fButime\fR(2),
 512 \fBfgetattr\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5), \fBfsattr\fR(5),
 513 \fBlargefile\fR(5), \fBlocale\fR(5), \fBstandards\fR(5)
 514 .SH NOTES
 515 The permission modes of the source file are preserved in the copy.
 516 .sp
 517 .LP
 518 A \fB--\fR permits the user to mark the end of any command line options
 519 explicitly, thus allowing \fBcp\fR to recognize filename arguments that begin
 520 with a \fB-\fR.