1cp(1) User Commands cp(1)
2
3
4
6 cp - copy files
7
9 /usr/bin/cp [-fip@/] source_file target_file
10
11
12 /usr/bin/cp [-fip@/] source_file... target
13
14
15 /usr/bin/cp -r | -R [-H | -L | -P] [-fip@/] source_dir... target
16
17
18 /usr/bin/cp -R | -R [-H | -L | -P] [-fip@/] source_dir... target
19
20
21 /usr/xpg4/bin/cp [-fip@/] source_file target_file
22
23
24 /usr/xpg4/bin/cp [-fip@/] source_file... target
25
26
27 /usr/xpg4/bin/cp -r | -R [-H | -L | -P] [-fip@/] source_dir... target
28
29
30 /usr/xpg4/bin/cp -R | -R [-H | -L | -P] [-fip@/] source_dir... target
31
32
34 In the first synopsis form, neither source_file nor target_file are
35 directory files, nor can they have the same name. The cp utility copies
36 the contents of source_file to the destination path named by tar‐
37 get_file. If target_file exists, cp overwrites its contents, but the
38 mode (and ACL if applicable), owner, and group associated with it are
39 not changed. The last modification time of target_file and the last
40 access time of source_file are set to the time the copy was made. If
41 target_file does not exist, cp creates a new file named target_file
42 that has the same mode as source_file except that the sticky bit is not
43 set unless the user is super-user. In this case, the owner and group of
44 target_file are those of the user, unless the setgid bit is set on the
45 directory containing the newly created file. If the directory's setgid
46 bit is set, the newly created file has the group of the containing
47 directory rather than of the creating user. If target_file is a link to
48 another file, cp overwrites the link destination with the contents of
49 source_file; the link(s) from target_file remains.
50
51
52 In the second synopsis form, one or more source_files are copied to the
53 directory specified by target. It is an error if any source_file is a
54 file of type directory, if target either does not exist or is not a
55 directory.
56
57
58 In the third or fourth synopsis forms, one or more directories speci‐
59 fied by source_dir are copied to the directory specified by target.
60 Either the -r or -R must be specified. For each source_dir, cp copies
61 all files and subdirectories.
62
64 The following options are supported for both /usr/bin/cp and
65 /usr/xpg4/bin/cp:
66
67 -f Unlink. If a file descriptor for a destination file cannot be
68 obtained, this option attempts to unlink the destination file and
69 proceed.
70
71
72 -H Takes actions based on the type and contents of the file refer‐
73 enced by any symbolic link specified as a source_file operand.
74
75 If the source_file operand is a symbolic link, then cp copies the
76 file referenced by the symbolic link for the source_file operand.
77 All other symbolic links encountered during traversal of a file
78 hierarchy are preserved.
79
80
81 -i Interactive. cp prompts for confirmation whenever the copy would
82 overwrite an existing target. An affirmative response means that
83 the copy should proceed. Any other answer prevents cp from over‐
84 writing target.
85
86
87 -L Takes actions based on the type and contents of the file refer‐
88 enced by any symbolic link specified as a source_file operand or
89 any symbolic links encountered during traversal of a file hierar‐
90 chy.
91
92 Copies files referenced by symbolic links. Symbolic links encoun‐
93 tered during traversal of a file hierarchy are not preserved.
94
95
96 -p Preserve. The cp utility duplicates not only the contents of
97 source_file, but also attempts to preserve its ACL, access and
98 modification times, extended attributes, extended system
99 attributes, file mode, and owner and group ids.
100
101 If cp is unable to preserve the access and modification times,
102 extended attributes, or the file mode, cp does not consider it a
103 failure. If cp is unable to preserve the owner and group id, the
104 copy does not fail, but cp silently clears the S_ISUID and
105 S_ISGID bits from the file mode of the target. The copy fails if
106 cp is unable to clear these bits. If cp is unable to preserve the
107 ACL or extended system attributes, the copy fails. If the copy
108 fails, then a diagnostic message is written to stderr and (after
109 processing any remaining operands) cp exits with a non-zero exit
110 status.
111
112
113 -P Takes actions on any symbolic link specified as a source_file op‐
114 erand or any symbolic link encountered during traversal of a file
115 hierarchy.
116
117 Copies symbolic links. Symbolic links encountered during traver‐
118 sal of a file hierarchy are preserved.
119
120
121 -r Recursive. cp copies the directory and all its files, including
122 any subdirectories and their files to target. Unless the -H, -L,
123 or -P option is specified, the -L option is used as the default
124 mode.
125
126
127 -R Same as -r, except pipes are replicated, not read from.
128
129
130 -@ Preserves extended attributes. cp attempts to copy all of the
131 source file's extended attributes along with the file data to the
132 destination file.
133
134
135 -/ Preserves extended attributes and extended system attributes.
136 Along with the file's data, the cp utility attempts to copy
137 extended attributes and extended system attributes from each
138 source file, and extended system attributes associated with
139 extended attributes to the destination file. If cp is unable to
140 copy extended attributes or extended system attributes, then a
141 diagnostic message is written to stderr and (after processing any
142 remaining operands) exits with a non-zero exit status.
143
144
145
146 Specifying more than one of the mutually-exclusive options -H, -L, and
147 -P is not considered an error. The last option specified determines the
148 behavior of the utility.
149
150 /usr/bin/cp
151 If the -p option is specified with either the -@ option or the -/
152 option, /usr/bin/cp behaves as follows
153
154 o When both -p and -@ are specified in any order, the copy
155 fails if extended attributes cannot be copied.
156
157 o When both -p and -/ are specified in any order, the copy
158 fails if extended system attributes cannot be copied.
159
160 /usr/xpg4/bin/cp
161 If the -p option is specified with either the -@ option or the -/
162 option, /usr/xpg4/bin/cp behaves as follows:
163
164 o When both -p and -@ are specified, the last option specified
165 determines whether the copy fails if extended attributes
166 cannot be preserved.
167
168 o When both -p and -/ are specified, the last option specified
169 determines whether the copy fails if extended system
170 attributes cannot be preserved.
171
173 The following operands are supported:
174
175 source_file A pathname of a regular file to be copied.
176
177
178 source_dir A pathname of a directory to be copied.
179
180
181 target_file A pathname of an existing or non-existing file, used for
182 the output when a single file is copied.
183
184
185 target A pathname of a directory to contain the copied files.
186
187
189 See largefile(5) for the description of the behavior of cp when encoun‐
190 tering files greater than or equal to 2 Gbyte ( 2^31 bytes).
191
193 Example 1 Copying a File
194
195
196 The following example copies a file:
197
198
199 example% cp goodies goodies.old
200
201 example% ls goodies*
202 goodies goodies.old
203
204
205
206 Example 2 Copying a List of Files
207
208
209 The following example copies a list of files to a destination direc‐
210 tory:
211
212
213 example% cp ~/src/* /tmp
214
215
216
217 Example 3 Copying a Directory
218
219
220 The following example copies a directory, first to a new, and then to
221 an existing destination directory
222
223
224 example% ls ~/bkup
225 /usr/example/fred/bkup not found
226
227 example% cp -r ~/src ~/bkup
228
229 example% ls -R ~/bkup
230 x.c y.c z.sh
231
232 example% cp -r ~/src ~/bkup
233
234 example% ls -R ~/bkup
235 src x.c y.c z.sh
236 src:
237 x.c y.c z.s
238
239
240
241 Example 4 Copying Extended File System Attributes
242
243
244 The following example copies extended file system attributes:
245
246
247 $ ls -/ c file1
248 -rw-r--r-- 1 foo staff 0 Oct 29 20:04 file1
249 {AH-----m--}
250
251 $ cp -/ file1 file2
252 $ ls -/c file2
253 -rw-r--r-- 1 foo staff 0 Oct 29 20:17 file2
254 {AH-----m--}
255
256
257
258 Example 5 Failing to Copy Extended System Attributes
259
260
261 The following example fails to copy extended system attributes:
262
263
264 $ ls -/c file1
265 -rw-r--r-- 1 foo staff 0 Oct 29 20:04 file1
266 {AH-----m--}
267
268 $ cp -/ file1 /tmp
269 cp: Failed to copy extended system attributes from file1 to /tmp/file1
270
271
272 $ ls -/c /tmp/file1
273 -rw-r--r-- 1 foo staff 0 Oct 29 20:09 /tmp/file1
274 {}
275
276
277
279 See environ(5) for descriptions of the following environment variables
280 that affect the execution of cp: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
281 LC_MESSAGES, and NLSPATH.
282
283
284 Affirmative responses are processed using the extended regular expres‐
285 sion defined for the yesexpr keyword in the LC_MESSAGES category of the
286 user's locale. The locale specified in the LC_COLLATE category defines
287 the behavior of ranges, equivalence classes, and multi-character col‐
288 lating elements used in the expression defined for yesexpr. The locale
289 specified in LC_CTYPE determines the locale for interpretation of
290 sequences of bytes of text data a characters, the behavior of character
291 classes used in the expression defined for the yesexpr. See locale(5).
292
294 The following exit values are returned:
295
296 0 All files were copied successfully.
297
298
299 >0 An error occurred.
300
301
303 See attributes(5) for descriptions of the following attributes:
304
305 /usr/bin/cp
306 ┌─────────────────────────────┬─────────────────────────────┐
307 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
308 ├─────────────────────────────┼─────────────────────────────┤
309 │Availability │SUNWcsu │
310 ├─────────────────────────────┼─────────────────────────────┤
311 │CSI │Enabled │
312 ├─────────────────────────────┼─────────────────────────────┤
313 │Interface Stability │Committed │
314 └─────────────────────────────┴─────────────────────────────┘
315
316 /usr/xpg4/bin/cp
317 ┌─────────────────────────────┬─────────────────────────────┐
318 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
319 ├─────────────────────────────┼─────────────────────────────┤
320 │Availability │SUNWxcu4 │
321 ├─────────────────────────────┼─────────────────────────────┤
322 │CSI │Enabled │
323 ├─────────────────────────────┼─────────────────────────────┤
324 │Interface Stability │Committed │
325 └─────────────────────────────┴─────────────────────────────┘
326
328 chmod(1), chown(1), setfacl(1), utime(2), fgetattr(3C), attributes(5),
329 environ(5), fsattr(5), largefile(5), locale(5), standards(5)
330
332 The permission modes of the source file are preserved in the copy.
333
334
335 A -- permits the user to mark the end of any command line options
336 explicitly, thus allowing cp to recognize filename arguments that begin
337 with a -.
338
339
340
341SunOS 5.11 30 Oct 2007 cp(1)