1.\"- 2.\" Copyright (c) 1989, 1990, 1993, 1994 3.\" The Regents of the University of California. All rights reserved. 4.\" 5.\" This code is derived from software contributed to Berkeley by 6.\" the Institute of Electrical and Electronics Engineers, Inc. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.Dd March 28, 2024 33.Dt CP 1 34.Os 35.Sh NAME 36.Nm cp 37.Nd copy files 38.Sh SYNOPSIS 39.Nm 40.Oo 41.Fl R 42.Op Fl H | Fl L | Fl P 43.Oc 44.Op Fl f | i | n 45.Op Fl alNpsvx 46.Ar source_file target_file 47.Nm 48.Oo 49.Fl R 50.Op Fl H | Fl L | Fl P 51.Oc 52.Op Fl f | i | n 53.Op Fl alNpsvx 54.Ar source_file ... target_directory 55.Nm 56.Op Fl f | i | n 57.Op Fl alNPpsvx 58.Ar source_file target_file 59.Nm 60.Op Fl f | i | n 61.Op Fl alNPpsvx 62.Ar source_file ... target_directory 63.Sh DESCRIPTION 64In the first synopsis form, the 65.Nm 66utility copies the contents of the 67.Ar source_file 68to the 69.Ar target_file . 70In the second synopsis form, 71the contents of each named 72.Ar source_file 73is copied to the destination 74.Ar target_directory . 75The names of the files themselves are not changed. 76If 77.Nm 78detects an attempt to copy a file to itself, the copy will fail. 79.Pp 80The following options are available: 81.Bl -tag -width flag 82.It Fl H 83If the 84.Fl R 85option is specified, symbolic links on the command line are followed. 86(Symbolic links encountered in the tree traversal are not followed.) 87.It Fl L 88If the 89.Fl R 90option is specified, all symbolic links are followed. 91.It Fl P 92No symbolic links are followed. 93This is the default if the 94.Fl R 95option is specified. 96.It Fl R 97If 98.Ar source_file 99designates a directory, 100.Nm 101copies the directory and the entire subtree connected at that point. 102If the 103.Ar source_file 104ends in a 105.Pa / , 106the contents of the directory are copied rather than the 107directory itself. 108This option also causes symbolic links to be copied, rather than 109indirected through, and for 110.Nm 111to create special files rather than copying them as normal files. 112Created directories have the same mode as the corresponding source 113directory, unmodified by the process' umask. 114.Pp 115Note that 116.Nm 117copies hard linked files as separate files. 118If you need to preserve hard links, consider using 119.Xr tar 1 , 120.Xr cpio 1 , 121or 122.Xr pax 1 123instead. 124.It Fl a 125Archive mode. 126Same as 127.Fl RpP . 128.It Fl f 129For each existing destination pathname, remove it and 130create a new file, without prompting for confirmation 131regardless of its permissions. 132(The 133.Fl f 134option overrides any previous 135.Fl i 136or 137.Fl n 138options.) 139.It Fl i 140Cause 141.Nm 142to write a prompt to the standard error output before copying a file 143that would overwrite an existing file. 144If the response from the standard input begins with the character 145.Sq Li y 146or 147.Sq Li Y , 148the file copy is attempted. 149(The 150.Fl i 151option overrides any previous 152.Fl f 153or 154.Fl n 155options.) 156.It Fl l 157Create hard links to regular files in a hierarchy instead of copying. 158.It Fl N 159When used with 160.Fl p , 161suppress copying file flags. 162.It Fl n 163Do not overwrite an existing file. 164(The 165.Fl n 166option overrides any previous 167.Fl f 168or 169.Fl i 170options.) 171.It Fl p 172Cause 173.Nm 174to preserve the following attributes of each source 175file in the copy: modification time, access time, 176file flags, file mode, ACL, user ID, and group ID, as allowed by permissions. 177.Pp 178If the user ID and group ID cannot be preserved, no error message 179is displayed and the exit value is not altered. 180.Pp 181If the source file has its set-user-ID bit on and the user ID cannot 182be preserved, the set-user-ID bit is not preserved 183in the copy's permissions. 184If the source file has its set-group-ID bit on and the group ID cannot 185be preserved, the set-group-ID bit is not preserved 186in the copy's permissions. 187If the source file has both its set-user-ID and set-group-ID bits on, 188and either the user ID or group ID cannot be preserved, neither 189the set-user-ID nor set-group-ID bits are preserved in the copy's 190permissions. 191.It Fl s 192Create symbolic links to regular files in a hierarchy instead of copying. 193.It Fl v 194Cause 195.Nm 196to be verbose, showing files as they are copied. 197.It Fl x 198File system mount points are not traversed. 199.El 200.Pp 201For each destination file that already exists, its contents are 202overwritten if permissions allow. 203Its mode, user ID, and group 204ID are unchanged unless the 205.Fl p 206option was specified. 207.Pp 208In the second synopsis form, 209.Ar target_directory 210must exist unless there is only one named 211.Ar source_file 212which is a directory and the 213.Fl R 214flag is specified. 215.Pp 216If the destination file does not exist, the mode of the source file is 217used as modified by the file mode creation mask 218.Pf ( Ic umask , 219see 220.Xr csh 1 ) . 221If the source file has its set-user-ID bit on, that bit is removed 222unless both the source file and the destination file are owned by the 223same user. 224If the source file has its set-group-ID bit on, that bit is removed 225unless both the source file and the destination file are in the same 226group and the user is a member of that group. 227If both the set-user-ID and set-group-ID bits are set, all of the above 228conditions must be fulfilled or both bits are removed. 229.Pp 230Appropriate permissions are required for file creation or overwriting. 231.Pp 232Symbolic links are always followed unless the 233.Fl R 234flag is set, in which case symbolic links are not followed, by default. 235The 236.Fl H 237or 238.Fl L 239flags (in conjunction with the 240.Fl R 241flag) cause symbolic links to be followed as described above. 242The 243.Fl H , 244.Fl L 245and 246.Fl P 247options are ignored unless the 248.Fl R 249option is specified. 250In addition, these options override each other and the 251command's actions are determined by the last one specified. 252.Pp 253If 254.Nm 255receives a 256.Dv SIGINFO 257(see the 258.Cm status 259argument for 260.Xr stty 1 ) 261signal, the current input and output file and the percentage complete 262will be written to the standard output. 263.Sh EXIT STATUS 264.Ex -std 265.Sh EXAMPLES 266Make a copy of file 267.Pa foo 268named 269.Pa bar : 270.Pp 271.Dl $ cp foo bar 272.Pp 273Copy a group of files to the 274.Pa /tmp 275directory: 276.Pp 277.Dl $ cp *.txt /tmp 278.Pp 279Copy the directory 280.Pa junk 281and all of its contents (including any subdirectories) to the 282.Pa /tmp 283directory: 284.Pp 285.Dl $ cp -R junk /tmp 286.Sh COMPATIBILITY 287Historic versions of the 288.Nm 289utility had a 290.Fl r 291option. 292This implementation supports that option, however, its behavior 293is different from historical 294.Fx 295behavior. 296Use of this option 297is strongly discouraged as the behavior is 298implementation-dependent. 299In 300.Fx , 301.Fl r 302is a synonym for 303.Fl RL 304and works the same unless modified by other flags. 305Historical implementations 306of 307.Fl r 308differ as they copy special files as normal 309files while recreating a hierarchy. 310.Pp 311The 312.Fl a , 313.Fl l , 314.Fl N , 315.Fl n , 316.Fl s , 317.Fl v , 318and 319.Fl x 320options are non-standard and their use in scripts is not recommended. 321.Sh SEE ALSO 322.Xr mv 1 , 323.Xr rcp 1 , 324.Xr umask 2 , 325.Xr fts 3 , 326.Xr symlink 7 327.Sh STANDARDS 328The 329.Nm 330command is expected to be 331.St -p1003.2 332compatible. 333.Sh HISTORY 334A 335.Nm 336command appeared in 337.At v1 . 338