cp copies files (or, optionally, directories).
You can either copy one file to a given destination,
or copy arbitrarily many files to a destination directory.
If the last argument names an existing directory,
cp copies each source
file into that directory (retaining the same name). Otherwise,
if only two files are given, it copies the first onto the second. It
is an error if the last argument is not a directory and more than two
non-option arguments are given.
(Thus, if /a is a directory, then cp -r /a /b will copy /a to /b/a
and /a/x to /b/a/x in case a directory /b existed already, but it will
copy /a to /b and /a/x to /b/x if there was no /b beforehand,
while it will fail in case there was an ordinary file /b.)
The modes of the files and directories created will be the same
as those of the original files, ANDed by 0777, and modified by
the users umask (unless the -p option was specified).
(But during the recursive copy of directories, newly created
directories will temporarily get their final mode ORed with
S_IRWXU (0700), so as to allow the process to read, write
and search the newly created directory.)
Nothing is done when copying a file to itself (except possibly
producing an error message).
When copying to a different existing file, it is opened
using open(path, O_WRONLY | O_TRUNC).
When copying to a new file it is created
using open(path, O_WRONLY | O_CREAT, mode).
If this fails, the file existed, and the -f option was given,
cp tries to delete (unlink) the existing file, and if this succeeds
proceeds as for a new file.
Remove existing destination files if required. (See above.)
-i
Prompt whether to overwrite existing regular destination files.
(Write a question on stderr, and read the answer from stdin.
Only copy upon an affirmative answer.)
-p
Preserve the original files owner, group, permissions
(including the set-user-ID and set-group-ID bits), time of last modification
and time of last access.
In case duplication of owner or group fails,
the set-user-ID and set-group-ID bits are cleared.
(Note that afterwards source and copy may well have different
times of last access, since the copy operation is an access
to the source file.)
-R
Copy directories recursively, and do the right thing when
objects other than ordinary files or directories are encountered.
(Thus, the copy of a FIFO or special file is a FIFO or special file.)
-r
Copy directories recursively, and do something unspecified
with objects other than ordinary files or directories.
(Thus, it is allowed, in fact encouraged, to have the -r option
a synonym for -R. However, silly behaviour, like that of the
GNU 4.0 version of
cp is not forbidden.)
POSIX.1-2001 adds three options that specify how to handle
symbolic links. When doing a non-recursive copy, symbolic links
are followed. When doing a recursive copy using the -r option,
the results are implementation-defined. When doing a recursive
copy using the -R option:
-H
Follow the symbolic links given in the parameter list.
Do not follow symbolic links encountered during the recursive copy,
but just copy them as symbolic link.
-L
Follow all symbolic links, both those that occur in the parameter list
and those encountered during the recursive copy.
-P
Do not follow any symbolic links, neither those that occur
in the parameter list nor those encountered during the recursive copy.
Just copy them as symbolic link.
There is no default: one should specify the desired behaviour.
Generally, files are written just as they are read. For exceptions,
see the
--sparse option below.
By default, cp does not copy directories (see
-r below).
cp generally refuses to copy a file onto itself, with the following
exception: if
--force --backup is specified with
source and
dest identical, and referring to a regular file,
cp will make a backup file, either regular or numbered, as specified in
the usual ways. This is useful when you simply want to make a backup
of an existing file before changing it.
Preserve as much as possible of the structure and attributes of the
original files in the copy (but do not preserve directory structure).
Equivalent to
-dpPR.
-b
See the discussion of backups below.
--copy-contents (since file-utils 4.1)
Do the silly things file-utils 4.0 did,
trying to copy the contents of device files and FIFOs during
a recursive copy. Never use this option. With it, cp may well hang
indefinitely reading a FIFO or /dev/tty, or fill the destination disk
copying /dev/zero.
-d
Copy symbolic links as symbolic links rather than copying the
files that they point to, and preserve hard links between source
files in the copies.
With file-utils 4.0 the long option --no-dereference was a
synonym for -d, with file-utils 4.1 it is a synonym for -P,
while -d is equivalent to --no-dereference --preserve=links.
-f, --force
Remove existing destination files in case an open for writing fails,
and never prompt before doing so.
(Thus since file-utils 4.1. With file-utils 4.0 this option was
equivalent to the new --remove-destination.)
-H (since file-utils 4.1)
See POSIX description above.
-i, --interactive
Prompt whether to overwrite existing regular destination files.
-l, --link
Make hard links instead of copies of non-directories.
-L, --dereference (since file-utils 4.1)
See POSIX description above.
--no-preserve=ATTRIBUTES (since file-utils 4.1)
Do not preserve the specified attributes.
See the --preserve option below.
-p, --preserve
Preserve the original files owner, group, permissions, and timestamps.
--preserve=ATTRIBUTES (since file-utils 4.1)
Here ATTRIBUTES can be one of "mode" (permissions), "ownership" (owner
and group), "timestamps", "links", "all" (all of the foregoing).
-P, --no-dereference (since file-utils 4.1)
See POSIX description above.
This replaces the file-utils 4.0 meaning of the -P option, that
was a synonym for --parents. See also -d above.
--parents (in file-utils 4.0 also -P)
Form the name of each destination file by appending to the target
directory a slash and the specified name of the source file. The
last argument given to
cp must be the name of an existing directory. For example, the command:
cp --parents a/b/c existing_dir
copies the file a/b/c to existing_dir/a/b/c, creating any
missing intermediate directories.
-r
In file-utils 4.1: synonym of -R.
In file-utils 4.0:
Copy directories recursively, copying any non-directories and
non-symbolic links (that is, FIFOs and special files) as if they
were regular files. This silly behaviour is obtained in file-utils 4.1
if the --copy-contents option is given.
Here HOW can be one of "yes", "no", "query", specifying that
to all questions the answer is yes, or is no, or must be obtained
by querying the user, respectively.
--remove-destination (since file-utils 4.1)
Remove each existing destination file before copying.
With file-utils 4.0 this option was implied by -f.
--sparse=WHEN
A sparse file contains holes sequences of null bytes (\0) that
do not occupy any physical disk blocks;
read(2)
reads these as null bytes (\0).
This can both save considerable disk space
and increase speed, since many binary files contain lots of
consecutive null bytes. By default,
cp detects holes in input source files via a crude heuristic
and makes the corresponding output file sparse as well.
The
WHEN value can be one of the following:
auto
The default behavior: the output file is sparse if the input
file is sparse.
always
Always make the output file sparse. This is useful when the
input file resides on a filesystem that does not support
sparse files, but the output file is on a filesystem that does.
never
Never make the output file sparse. If you find an application for
this option, let us know.
--strip-trailing-slashes (since file-utils 4.1)
Remove any trailing slashes from each source argument.
(This can change the interpretation in case of a symbolic link
to a directory.)
-s, --symbolic-link
Make symbolic links instead of copies of non-directories. All
source filenames must be absolute (starting with /) unless the
destination files are in the current directory. This option merely
results in an error message on systems that do not support
symbolic links.
-S
Backup suffix, see below.
--target-directory=DIR (since file-utils 4.1)
Specify the destination directory. Meant for use with
xargs(1),
as in "ls | xargs cp --target-directory=../d".
-u, --update
Do not copy a nondirectory that has an existing destination with
the same or newer modification time.
-v, --verbose
Print the name of each file before copying it.
-x, --one-file-system
Skip subdirectories that are on different filesystems from the one
that the copy started on.
The GNU versions of programs like
cp,
mv,
ln,
install and
patch will make a backup of files about to be overwritten, changed or destroyed
if that is desired. That backup files are desired is indicated by
the -b option. How they should be named is specified by the -V option.
In case the name of the backup file is given by the name of the file
extended by a suffix, this suffix is specified by the -S option.
-b, --backup
Make backups of files that are about to be overwritten or removed.
--backup=CONTROL
(Since fileutils-4.1.)
-S SUFFIX, --suffix=SUFFIX
Append
SUFFIX to each backup file made.
If this option is not specified, the value of the
SIMPLE_BACKUP_SUFFIX environment variable is used. And if
SIMPLE_BACKUP_SUFFIX is not set, the default is ~.
-V METHOD, --version-control=METHOD
Specify how backup files are named. The
METHOD argument can be numbered (or t), existing (or nil), or never (or
simple).
If this option is not specified, the value of the
VERSION_CONTROL environment variable is used. And if
VERSION_CONTROL is not set, the default backup type is existing.
This option corresponds to the Emacs variable version-control.
The valid
METHODs are (unique abbreviations are accepted):
t, numbered
Always make numbered backups.
nil, existing
Make numbered backups of files that already have them, simple
backups of the others.
The variables LANG, LC_ALL, LC_COLLATE, LC_CTYPE and LC_MESSAGES have the
usual meaning. For the GNU version, the variables SIMPLE_BACKUP_SUFFIX
and VERSION_CONTROL control backup file naming, as described above.