Copy files (POSIX)
cp [-f|-i] [-Rrp] [QNX extensions] source_file target_file cp [-f|-i] [-Rrp] [QNX extensions] source_file... target_dir
[UTC] dd/mm/yy [HH:MM:SS]
specified in 24 hour time (the time portion is optional). To use Coordinated Universal Time, specify UTC; to use the local time, don't include UTC.
If you're running cp from the shell, and you want to specify both the date and the time, you must use the quotation marks. If you want to specify only the date (dd/mm/yy), quotation marks aren't needed.
When even using -B fails, one can resort to using the cat utility to copy data.
cp -c file /home/eric/source/file
cp performs the equivalent of:
mkdir -p /home/eric/source cp file /home/eric/source/file
cp -d myprog to //61/hd/bin //73/hd/bin //15/hd/bin
performs the equivalent of:
cp myprog //61/hd/bin cp myprog //73/hd/bin cp myprog //15/hd/binWhen you don't specify -d, the word "to" is treated like any other filename.
Specifying -D will cause cp to descend past device boundaries. For example:
cp -R //1/ //2/backupwill, when //1/ is a local hard disk, cause the contents of the disk to be backed up, but the //1/dev directory will be skipped, since it does not exist on the hard disk device. The addition of -D, as follows:
cp -RD //1/ //2/backupwould cause cp to also attempt to back up the contents of the //1/dev directory. Note that in this particular example only the disk devices (block special files) would actually have their data backed up to files in //2/backup/dev because cp won't copy character special files on recursive copies.
The combination of -i and -f works as if only -i were specified, except that when you answer yes to the prompt, the destination is always unlinked first-even if you have write permission for the destination file. When only -i is specified, the destination is unlinked only when you do not have write permission for the destination file. See note about POSIX compatibility of the current -f behavior.
For example, an option of -m ug=rx copies only files for which user and group have read and execute permission.
The QNX extended options listed above that are deprecated may not be present in future versions of QNX. |
cp [-f|-i] [-Rrp] [QNX extensions] source_file target_file
The cp utility copies the contents of the source file to the destination file named by target_file. This first syntax form is assumed when the destination file isn't an existing directory and there's only one source file.
cp [-f|-i] [-Rrp] [QNX extensions] source_file... target_dir
For each source_file, cp copies the contents of the file to a destination file in the existing directory named by target_dir. The destination's filename under the target directory will be the same as its basename (final path component), unless it is a directory (see Recursive Copying). For example:
cp dir/dir/myfile /existingdir
copies the contents of dir/dir/myfile to the file /existingdir/myfile.
This second form is assumed when the destination file is an existing directory or when more than one source file is specified.
By default cp duplicates file contents and also duplicates the file times of the original file. This is not standard UNIX behavior for cp. To disable this, either disable the duplication of file times by using the -t or set POSIX_STRICT - see Environment variables for details.
Unless the -R (recursive) option is specified, cp refuses to copy any source_file that is a directory.
For duplicating lists of files, see the pax -rw utility, which is another POSIX utility for duplicating files. Sets of files which match complex criteria may be selected with the find utility and piped into pax. The abilities built into cp to accomplish the same thing (e.g. pattern matching) are deprecated and will be removed in a future release. |
What cp does when a destination file already exists depends on the options used. When neither -f nor -i is specified, you'll be prompted only if you don't have write permission for the existing destination file. When this happens, you'll be asked if you want to unlink the file first. If you don't, cp goes on to any remaining files. You'll be prompted only if stdin is a tty. Otherwise, cp will print a diagnostic message to stderr and skip that file.
If you're copying to removable media, such as a floppy or removable disk, and the media becomes full, cp performs the following steps:
When doing a recursive copy of a directory, the destination must be a directory. If more than one item is being copied, the directory must already exist. If a single directory is being copied, the cp command will create the destination directory (all intermediate directories must already exist unless the -c option is specified).
There are two recursive copying modes available with cp. The default is the historical QNX behavior which is to copy the files and directories underneath the source directory to the destination directory. The source directory itself will not be duplicated within the destination directory.
The other mode, specified by the -Munix option, causes cp to duplicate the source directory within the destination directory (unless a single directory is being copied and the destination directory does not yet exist, in which case -Munix and -Mqnx modes do the same thing).
In -Mqnx mode, cp -r /bin //2/bin will copy the contents of /bin to //2/bin (so, for example, /bin/sh will be copied to //2/bin/sh etc.).
In -Munix mode, cp -Munix -r /bin //2/bin would duplicate /bin in //2/bin, i.e. the destination would be //2/bin/bin and, for example, the file /bin/sh would be copied to //2/bin/bin/sh. This is analogous to the way the mv utility treats destinations.
The default behavior is currently -Mqnx, but this will change in a future major revision of QNX.
A pattern-matching special character that is quoted is a pattern that matches the special character itself. When not quoted, such special characters have special meaning in the specification of patterns. The pattern-matching special characters and the contexts in which they have their special meaning are as follows:
Note that the characters ?, *, and [ are not special when used inside a bracket expression.
The concatenation of patterns matching a single character is a valid pattern that matches the concatenation of the characters matched by each of the concatenated patterns. For example, the pattern a[bc] matches the strings ab and ac.
The concatenation of one or more patterns matching a single character with one or more asterisks (*) is a valid pattern. In such patterns, each asterisk matches a string of zero or more characters, up to the first character that matches the character following the asterisk in the pattern. For example, the pattern a*d matches the strings ad, abd, and abcd, but not the string abc.
When an asterisk is the first or last character in a pattern, it matches zero or more characters that precede or follow the characters matched by the remainder of the pattern. For example, the pattern a*d* matches the strings ad, abcd, abcdef, aaaad, and adddd. The pattern *a*d matches the strings ad, abcd, efabcd, aaaad, and adddd.
Copy file1, file2, and file3 from the current working directory to the /home/eric directory:
cp file1 file2 file3 /home/eric
Do a raw block copy from the floppy drive on node 4 to the floppy drive on node 6:
cp //4/dev/fd0 //6/dev/fd0
Perform a backup of the entire contents of the home directory to floppy disks (assuming that /f0 is a mount point for /dev/f0), in the (default) QNX recursive-copy mode:
cp -rvp /home /f0/home
Do the same in UNIX recursive-copy mode:
cp -Munix -rvp /home /f0
Recursively copy the /home/eric directory to the /home/ejohnson directory (assuming /home/ejohnson does not yet exist) (will work in either -Munix or -Mqnx mode):
cp -rv /home/eric /home/ejohnson
Do the same in -Mqnx mode if the directory ejohnson already exists:
cp -Mqnx -rv /home/eric /home/ejohnson
Do the same in -Munix mode if the directory ejohnson already exists:
cp -Munix -rv /home/eric/. /home/ejohnson
Recursively copy the contents of the current directory into //2/ in -Mqnx or -Munix modes:
cp -Rpv . //2/
Do the same in -Munix mode only:
cp -Munix -Rpv * //2/
In the previous example, doing cp -Mqnx -Rpv * //2/ would have resulted in copying the contents of the directories named on the command line into //2/ (i.e. the file ./bin/ls would have been copied to //2/ls, and the directory ./usr/bin would have been //2/bin in the destination). |
Recursively copy the /home/eric directory to the /backups/eric directory:
cp -rv /home/eric /backups/eric
Do the same in UNIX-style recursive copy mode:
cp -Munix -rv /home/eric /backups
If you specify the -r option, or there's more than one source file, the input files specified by each source_file operand, including those files contained within named directories, must be either regular files, block special files, or directories.
If you use the -R option, FIFOs will be duplicated in the destination directory structure, but contents of the source FIFOs won't be copied. Any block special or character special files encountered in the source files will result in an error, since cp cannot create these at the destination.
Affects whether file modification times are copied, and, if set to on, causes the QNX Extension options to be disabled.
The setting of the POSIX_STRICT environment variable affects the -p and -t options, as follows:
POSIX_STRICT | Option | Action |
---|---|---|
set | neither -p nor -t | if destination doesn't exist, duplicate mode only |
set | -p | duplicate time, mode; if run by root, also duplicate userid, groupid |
set | -pt | if run by root, duplicate userid, groupid |
set | -t | if destination doesn't exist, duplicate mode only |
unset | neither -p nor -t | duplicate time and mode |
unset | -p | duplicate time, mode; if run by root, also duplicate userid, groupid |
unset | -pt | if run by root, duplicate userid, groupid |
unset | -t | if destination doesn't exist, duplicate mode only |
If cp is copying multiple files or doing a recursive copy, but the -R option is not specified, cp will refuse to copy FIFO and character special files.
If you specify the -R option, and cp attempts but fails to copy a particular file in a specified file hierarchy, it continues to process the remaining files in the hierarchy.