xref: /freebsd/bin/cp/cp.1 (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
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 July 9, 2025
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 , Fl -dereference
88If the
89.Fl R
90option is specified, all symbolic links are followed.
91.It Fl P , Fl -no-dereference
92No symbolic links are followed.
93This is the default if the
94.Fl R
95option is specified.
96.It Fl R , Fl -recursive
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 , Fl -archive
125Archive mode.
126Same as
127.Fl RpP .
128.It Fl f , Fl -force
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 , Fl -interactive
140Write a prompt to the standard error output before copying a file
141that would overwrite an existing file.
142If the response from the standard input begins with the character
143.Sq Li y
144or
145.Sq Li Y ,
146the file copy is attempted.
147(The
148.Fl i
149option overrides any previous
150.Fl f
151or
152.Fl n
153options.)
154.It Fl l , Fl -link
155Create hard links to regular files in a hierarchy instead of copying.
156.It Fl N
157When used with
158.Fl p ,
159suppress copying file flags.
160.It Fl n , Fl -no-clobber
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
170Preserve the following attributes of each source
171file in the copy: modification time, access time,
172file flags, file mode, ACL, user ID, and group ID, as allowed by permissions.
173.Pp
174If the user ID and group ID cannot be preserved, no error message
175is displayed and the exit value is not altered.
176.Pp
177If the source file has its set-user-ID bit on and the user ID cannot
178be preserved, the set-user-ID bit is not preserved
179in the copy's permissions.
180If the source file has its set-group-ID bit on and the group ID cannot
181be preserved, the set-group-ID bit is not preserved
182in the copy's permissions.
183If the source file has both its set-user-ID and set-group-ID bits on,
184and either the user ID or group ID cannot be preserved, neither
185the set-user-ID nor set-group-ID bits are preserved in the copy's
186permissions.
187.It Fl -sort
188Visit and traverse sources in (non-localized) lexicographical order.
189Normally,
190.Nm
191visits the sources in the order they were listed on the command line,
192and if recursing, traverses their contents in whichever order they
193were returned in by the kernel, which may be the order in which they
194were created, lexicographical order, or something else entirely.
195With
196.Fl -sort ,
197the sources are both visited and traversed in lexicographical order.
198This is mostly useful for testing.
199.It Fl s , Fl -symbolic-link
200Create symbolic links to regular files in a hierarchy instead of copying.
201.It Fl v , Fl -verbose
202Be verbose, showing both the source and destination path of each file
203as is copied.
204.It Fl x , Fl -one-file-system
205Do not traverse file system mount points.
206.El
207.Pp
208For each destination file that already exists, its contents are
209overwritten if permissions allow.
210Its mode, user ID, and group
211ID are unchanged unless the
212.Fl p
213option was specified.
214.Pp
215In the second synopsis form,
216.Ar target_directory
217must exist unless there is only one named
218.Ar source_file
219which is a directory and the
220.Fl R
221flag is specified.
222.Pp
223If the destination file does not exist, the mode of the source file is
224used as modified by the file mode creation mask
225.Pf ( Ic umask ,
226see
227.Xr csh 1 ) .
228If the source file has its set-user-ID bit on, that bit is removed
229unless both the source file and the destination file are owned by the
230same user.
231If the source file has its set-group-ID bit on, that bit is removed
232unless both the source file and the destination file are in the same
233group and the user is a member of that group.
234If both the set-user-ID and set-group-ID bits are set, all of the above
235conditions must be fulfilled or both bits are removed.
236.Pp
237Appropriate permissions are required for file creation or overwriting.
238.Pp
239Symbolic links are always followed unless the
240.Fl R
241flag is set, in which case symbolic links are not followed, by default.
242The
243.Fl H
244or
245.Fl L
246flags (in conjunction with the
247.Fl R
248flag) cause symbolic links to be followed as described above.
249The
250.Fl H ,
251.Fl L
252and
253.Fl P
254options are ignored unless the
255.Fl R
256option is specified.
257In addition, these options override each other and the
258command's actions are determined by the last one specified.
259.Pp
260If
261.Nm
262receives a
263.Dv SIGINFO
264(see the
265.Cm status
266argument for
267.Xr stty 1 )
268signal, the current input and output file and the percentage complete
269will be written to the standard output.
270.Sh EXIT STATUS
271.Ex -std
272.Sh EXAMPLES
273Make a copy of file
274.Pa foo
275named
276.Pa bar :
277.Pp
278.Dl $ cp foo bar
279.Pp
280Copy a group of files to the
281.Pa /tmp
282directory:
283.Pp
284.Dl $ cp *.txt /tmp
285.Pp
286Copy the directory
287.Pa junk
288and all of its contents (including any subdirectories) to the
289.Pa /tmp
290directory:
291.Pp
292.Dl $ cp -R junk /tmp
293.Sh COMPATIBILITY
294Historic versions of the
295.Nm
296utility had a
297.Fl r
298option.
299This implementation supports that option, however, its behavior
300is different from historical
301.Fx
302behavior.
303Use of this option
304is strongly discouraged as the behavior is
305implementation-dependent.
306In
307.Fx ,
308.Fl r
309is a synonym for
310.Fl RL
311and works the same unless modified by other flags.
312Historical implementations
313of
314.Fl r
315differ as they copy special files as normal
316files while recreating a hierarchy.
317.Pp
318The
319.Fl a ,
320.Fl l ,
321.Fl N ,
322.Fl n ,
323.Fl s ,
324.Fl v ,
325and
326.Fl x
327options are non-standard and their use in scripts is not recommended.
328.Sh SEE ALSO
329.Xr mv 1 ,
330.Xr umask 2 ,
331.Xr fts 3 ,
332.Xr symlink 7
333.Sh STANDARDS
334The
335.Nm
336command is expected to be
337.St -p1003.2
338compatible.
339.Sh HISTORY
340A
341.Nm
342command appeared in
343.At v1 .
344