[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.1 cp: Copy files and directories

cp copies files (or, optionally, directories). The copy is completely independent of the original. You can either copy one file to another, or copy arbitrarily many files to a destination directory. Synopsis:

 
cp [option]... source dest
cp [option]... source... 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.

Generally, files are written just as they are read. For exceptions, see the `--sparse' option below.

By default, cp does not copy directories. However, the `-R', `-a', and `-r' options cause cp to copy recursively by descending into source directories and copying files to corresponding destination directories.

By default, cp follows symbolic links only when not copying recursively. This default can be overridden with the `--no-dereference' (`-d'), `--dereference' (`-L'), and `-H' options. If more than one of these options is specified, the last one silently overrides the others.

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 (see section 2.1 Backup options). This is useful when you simply want to make a backup of an existing file before changing it.

The program accepts the following options. Also see 2. Common options.

`-a'
`--archive'
Preserve as much as possible of the structure and attributes of the original files in the copy (but do not attempt to preserve internal directory structure; i.e., `ls -U' may list the entries in a copied directory in a different order). Equivalent to `-dpR'.

`-b'
`--backup[=method]'
See section 2.1 Backup options. Make a backup of each file that would otherwise be overwritten or removed. As a special case, cp makes a backup of source when the force and backup options are given and source and dest are the same name for an existing, regular file. One useful application of this combination of options is this tiny Bourne shell script:

 
#!/bin/sh
# Usage: backup FILE...
# Create a GNU-style backup of each listed FILE.
for i in "$@"; do
  cp --backup --force "$i" "$i"
done

`-d'
`--no-dereference'
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.

`-f'
`--force'
When copying without this option and an existing destination file cannot be opened for writing, the copy fails. However, with `--force'), when a destination file cannot be opened, cp then unlinks it and tries to open it again. Contrast this behavior with that enabled by `--link' and `--symbolic-link', whereby the destination file is never opened but rather is unlinked unconditionally. Also see the description of `--remove-destination'.

`-H'
If a command line argument specifies a symbolic link, then copy the file it points to rather than the symbolic link itself. However, copy (preserving its nature) any symbolic link that is encountered via recursive traversal.

`-i'
`--interactive'
Prompt whether to overwrite existing regular destination files.

`-l'
`--link'
Make hard links instead of copies of non-directories.

`-L'
`--dereference'
Always follow symbolic links.

`-p'
`--preserve'
Preserve the original files' owner, group, permissions, and timestamps. In the absence of this option, each destination file is created with the permissions of the corresponding source file, minus the bits set in the umask. See section 3. File permissions.

`-P'
`--parents'
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.

Warning: the meaning of `-P' will change in the future to conform to POSIX. Use `--parents' for the old meaning, and `--no-dereference' for the new.

`-r'
Copy directories recursively, copying any non-directories and special files (e.g., symbolic links, FIFOs and device files) as if they were regular files. This means trying to read the data in each source file and writing it to the destination. It is usually a mistake to apply cp -r to special files like FIFOs and the ones typically found in the `/dev' directory. In most cases, cp -r will hang indefinitely trying to read from FIFOs and special files like `/dev/console', and it will fill up your destination disk if you use it to copy `/dev/zero'. Use the `--recursive' (`-R') option instead if you want to copy special files, preserving their special nature rather than reading from them to copy their contents.

`-R'
`--recursive'
Copy directories recursively, preserving non-directories (contrast with `-r' just above).

`--remove-destination'
Remove each existing destination file before attempting to open it (contrast with `-f' above).

`--sparse=when'
A sparse file contains holes---a sequence of zero bytes that does not occupy any physical disk blocks; the `read' system call reads these as zeroes. This can both save considerable disk space and increase speed, since many binary files contain lots of consecutive zero 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 (the most notable example is `efs' filesystems in SGI IRIX 5.3 and earlier), but the output file is on another type of filesystem.

`never'
Never make the output file sparse. This is useful in creating a file for use with the mkswap command, since such a file must not have any holes.

`--strip-trailing-slashes'
Remove any trailing slashes from each source argument. See section 2.4 Trailing slashes.

`-s'
`--symbolic-link'
Make symbolic links instead of copies of non-directories. All source file names 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 suffix'
`--suffix=suffix'
Append suffix to each backup file made with `-b'. See section 2.1 Backup options.

`--target-directory=directory'
Specify the destination directory. See section 2.3 Target directory.

`-v'
`--verbose'
Print the name of each file before copying it.

`-V method'
`--version-control=method'
Change the type of backups made with `-b'. The method argument can be `none' (or `off'), `numbered' (or `t'), `existing' (or `nil'), or `never' (or `simple'). See section 2.1 Backup options.

`-x'
`--one-file-system'
Skip subdirectories that are on different filesystems from the one that the copy started on. However, mount point directories are copied.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on May 3, 2002 using texi2html