xref: /freebsd/bin/cp/cp.1 (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
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.\"	@(#)cp.1	8.3 (Berkeley) 4/18/94
33.\"
34.Dd February 23, 2022
35.Dt CP 1
36.Os
37.Sh NAME
38.Nm cp
39.Nd copy files
40.Sh SYNOPSIS
41.Nm
42.Oo
43.Fl R
44.Op Fl H | Fl L | Fl P
45.Oc
46.Op Fl f | i | n
47.Op Fl alpsvx
48.Ar source_file target_file
49.Nm
50.Oo
51.Fl R
52.Op Fl H | Fl L | Fl P
53.Oc
54.Op Fl f | i | n
55.Op Fl alpsvx
56.Ar source_file ... target_directory
57.Nm
58.Op Fl f | i | n
59.Op Fl alPpsvx
60.Ar source_file target_file
61.Nm
62.Op Fl f | i | n
63.Op Fl alPpsvx
64.Ar source_file ... target_directory
65.Sh DESCRIPTION
66In the first synopsis form, the
67.Nm
68utility copies the contents of the
69.Ar source_file
70to the
71.Ar target_file .
72In the second synopsis form,
73the contents of each named
74.Ar source_file
75is copied to the destination
76.Ar target_directory .
77The names of the files themselves are not changed.
78If
79.Nm
80detects an attempt to copy a file to itself, the copy will fail.
81.Pp
82The following options are available:
83.Bl -tag -width flag
84.It Fl H
85If the
86.Fl R
87option is specified, symbolic links on the command line are followed.
88(Symbolic links encountered in the tree traversal are not followed.)
89.It Fl L
90If the
91.Fl R
92option is specified, all symbolic links are followed.
93.It Fl P
94No symbolic links are followed.
95This is the default if the
96.Fl R
97option is specified.
98.It Fl R
99If
100.Ar source_file
101designates a directory,
102.Nm
103copies the directory and the entire subtree connected at that point.
104If the
105.Ar source_file
106ends in a
107.Pa / ,
108the contents of the directory are copied rather than the
109directory itself.
110This option also causes symbolic links to be copied, rather than
111indirected through, and for
112.Nm
113to create special files rather than copying them as normal files.
114Created directories have the same mode as the corresponding source
115directory, unmodified by the process' umask.
116.Pp
117Note that
118.Nm
119copies hard linked files as separate files.
120If you need to preserve hard links, consider using
121.Xr tar 1 ,
122.Xr cpio 1 ,
123or
124.Xr pax 1
125instead.
126.It Fl a
127Archive mode.
128Same as
129.Fl RpP .
130.It Fl f
131For each existing destination pathname, remove it and
132create a new file, without prompting for confirmation
133regardless of its permissions.
134(The
135.Fl f
136option overrides any previous
137.Fl i
138or
139.Fl n
140options.)
141.It Fl i
142Cause
143.Nm
144to write a prompt to the standard error output before copying a file
145that would overwrite an existing file.
146If the response from the standard input begins with the character
147.Sq Li y
148or
149.Sq Li Y ,
150the file copy is attempted.
151(The
152.Fl i
153option overrides any previous
154.Fl f
155or
156.Fl n
157options.)
158.It Fl l
159Create hard links to regular files in a hierarchy instead of copying.
160.It Fl n
161Do not overwrite an existing file.
162(The
163.Fl n
164option overrides any previous
165.Fl f
166or
167.Fl i
168options.)
169.It Fl p
170Cause
171.Nm
172to preserve the following attributes of each source
173file in the copy: modification time, access time,
174file flags, file mode, ACL, user ID, and group ID, as allowed by permissions.
175.Pp
176If the user ID and group ID cannot be preserved, no error message
177is displayed and the exit value is not altered.
178.Pp
179If the source file has its set-user-ID bit on and the user ID cannot
180be preserved, the set-user-ID bit is not preserved
181in the copy's permissions.
182If the source file has its set-group-ID bit on and the group ID cannot
183be preserved, the set-group-ID bit is not preserved
184in the copy's permissions.
185If the source file has both its set-user-ID and set-group-ID bits on,
186and either the user ID or group ID cannot be preserved, neither
187the set-user-ID nor set-group-ID bits are preserved in the copy's
188permissions.
189.It Fl s
190Create symbolic links to regular files in a hierarchy instead of copying.
191.It Fl v
192Cause
193.Nm
194to be verbose, showing files as they are copied.
195.It Fl x
196File system mount points are not traversed.
197.El
198.Pp
199For each destination file that already exists, its contents are
200overwritten if permissions allow.
201Its mode, user ID, and group
202ID are unchanged unless the
203.Fl p
204option was specified.
205.Pp
206In the second synopsis form,
207.Ar target_directory
208must exist unless there is only one named
209.Ar source_file
210which is a directory and the
211.Fl R
212flag is specified.
213.Pp
214If the destination file does not exist, the mode of the source file is
215used as modified by the file mode creation mask
216.Pf ( Ic umask ,
217see
218.Xr csh 1 ) .
219If the source file has its set-user-ID bit on, that bit is removed
220unless both the source file and the destination file are owned by the
221same user.
222If the source file has its set-group-ID bit on, that bit is removed
223unless both the source file and the destination file are in the same
224group and the user is a member of that group.
225If both the set-user-ID and set-group-ID bits are set, all of the above
226conditions must be fulfilled or both bits are removed.
227.Pp
228Appropriate permissions are required for file creation or overwriting.
229.Pp
230Symbolic links are always followed unless the
231.Fl R
232flag is set, in which case symbolic links are not followed, by default.
233The
234.Fl H
235or
236.Fl L
237flags (in conjunction with the
238.Fl R
239flag) cause symbolic links to be followed as described above.
240The
241.Fl H ,
242.Fl L
243and
244.Fl P
245options are ignored unless the
246.Fl R
247option is specified.
248In addition, these options override each other and the
249command's actions are determined by the last one specified.
250.Pp
251If
252.Nm
253receives a
254.Dv SIGINFO
255(see the
256.Cm status
257argument for
258.Xr stty 1 )
259signal, the current input and output file and the percentage complete
260will be written to the standard output.
261.Sh EXIT STATUS
262.Ex -std
263.Sh EXAMPLES
264Make a copy of file
265.Pa foo
266named
267.Pa bar :
268.Pp
269.Dl $ cp foo bar
270.Pp
271Copy a group of files to the
272.Pa /tmp
273directory:
274.Pp
275.Dl $ cp *.txt /tmp
276.Pp
277Copy the directory
278.Pa junk
279and all of its contents (including any subdirectories) to the
280.Pa /tmp
281directory:
282.Pp
283.Dl $ cp -R junk /tmp
284.Sh COMPATIBILITY
285Historic versions of the
286.Nm
287utility had a
288.Fl r
289option.
290This implementation supports that option, however, its behavior
291is different from historical
292.Fx
293behavior.
294Use of this option
295is strongly discouraged as the behavior is
296implementation-dependent.
297In
298.Fx ,
299.Fl r
300is a synonym for
301.Fl RL
302and works the same unless modified by other flags.
303Historical implementations
304of
305.Fl r
306differ as they copy special files as normal
307files while recreating a hierarchy.
308.Pp
309The
310.Fl l ,
311.Fl s ,
312.Fl v ,
313.Fl x
314and
315.Fl n
316options are non-standard and their use in scripts is not recommended.
317.Sh SEE ALSO
318.Xr mv 1 ,
319.Xr rcp 1 ,
320.Xr umask 2 ,
321.Xr fts 3 ,
322.Xr symlink 7
323.Sh STANDARDS
324The
325.Nm
326command is expected to be
327.St -p1003.2
328compatible.
329.Sh HISTORY
330A
331.Nm
332command appeared in
333.At v1 .
334