Copy files (POSIX)
cp [-f|-i] [-Rrp] [QNX Neutrino extensions] source_file target_file cp [-f|-i] [-Rrp] [QNX Neutrino extensions] source_file... target_dir
QNX Neutrino, Linux, Microsoft Windows
cp -c file /home/eric/source/file
cp performs the equivalent of:
mkdir -p /home/eric/source cp file /home/eric/source/file
The combination of -i and -f works as if you specified only -i, 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 you specify only -i, the destination is unlinked only when you don't have write permission for the destination file.
QNX has, in the past, copied the contents of the directories named on the command line into the target directory. UNIX copies the directory itself into the target directory (like mv). In either case, if there's only one directory being copied and the destination names a directory that doesn't exist, cp creates the destination directory and then copies the contents of the source directory into the destination directory.
The default mode under QNX Neutrino is different from that under QNX 4. |
For more information, see “Recursive Copying,” below.
cp -R / /hd/backup
causes cp to back up the contents of the disk, including the contents of the /dev directory.
In this particular example, only the disk devices (block special files) actually have their data backed up to files in /hd/backup/dev because cp doesn't copy character special files on recursive copies. |
The addition of -N, as follows:
cp -RN / /hd/backup
causes the contents of the disk to be backed up, but the /dev directory is skipped, since it doesn't exist on the hard disk device.
For example, if you select options -R and -n, you'll find that cp -VRn is more useful than cp -vRn, because the -v option in this case might let cp go away and put you back at the prompt without providing you with any feedback.
There are two syntax forms for cp:
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 you specify more than one source file.
Unless you specify the -R (recursive) option, 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. You can select sets of files that match complex criteria by using find, and then pipe them to pax. |
What cp does when a destination file already exists depends on the options used. If you don't specify -f or -i, cp prompts you only if you don't have write permission for the existing destination file. When this happens, you're asked if you want to unlink the file first. If you don't, cp goes on to any remaining files. You're prompted only if stdin is a tty. Otherwise, cp prints a diagnostic message to stderr and skips that file.
If you're copying to removable media, such as a floppy or removable disk, and the media becomes full, cp closes and removes the incompletely copied destination file, displays a message, then exits.
When doing a recursive copy of a directory, the destination must be a directory. If you're copying more than one item, the directory must already exist. If you're copying a single directory, cp creates the destination directory (all intermediate directories must already exist unless you specify the -c option).
There are two recursive copying modes available with cp:
The default mode under QNX Neutrino is different from that under QNX 4. |
In the default -Munix mode, cp -r /bin /mydir/bin duplicates /bin in /mydir/bin, i.e. the destination is /mydir/bin/bin and, for example, the file /bin/sh is copied to /mydir/bin/bin/sh. This is analogous to the way the mv utility treats destinations.
In -Mqnx mode, cp -Mqnx -r /bin /mydir/bin copies the contents of /bin to /mydir/bin (so, for example, /bin/sh is copied to /mydir/bin/sh).
Copy file1, file2, and file3 from the current working directory to the /home/eric directory:
cp file1 file2 file3 /home/eric
Perform a backup of the entire contents of the home directory to floppy disks (assuming that /f0 is a mountpoint for /dev/fd0), in the (default) UNIX recursive-copy mode:
cp -rvp /home /f0
Do the same in QNX recursive-copy mode:
cp -Mqnx -rvp /home /f0/home
Recursively copy the /home/eric directory to the /home/ejohnson directory, assuming /home/ejohnson doesn't yet exist (this works 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 /mydir/ in -Mqnx or -Munix mode:
cp -Rpv . /mydir/
Do the same in -Munix mode only:
cp -Munix -Rpv * /mydir/
Using -Mqnx instead of -Munix in the previous example copies the contents of the directories named on the command line into /mydir/ (i.e. the file ./bin/ls is copied to /mydir/ls, and the directory ./usr/bin is /mydir/bin in the destination). |
Recursively copy the /home/eric directory to the /backups/eric directory:
cp -rv /home/eric /backups
Do the same in QNX-style recursive copy mode:
cp -Mqnx -rv /home/eric /backups/eric
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 are duplicated in the destination directory structure, but contents of the source FIFOs aren't copied. If cp encounters any block special or character special files in the source files, an error occurs, because cp can't create them at the destination.
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 the destination doesn't exist, duplicate the mode only. |
Set | -p | Duplicate the time and mode; if run by root, also duplicate the user ID and group ID. |
Set | -pt | If run by root, duplicate the user ID and group ID. |
Set | -t | If destination doesn't exist, duplicate the mode only. |
Unset | Neither -p nor -t | Duplicate the time and mode. |
Unset | -p | Duplicate the time and mode; if run by root, also duplicate the user ID and group ID. |
Unset | -pt | If run by root, duplicate the user ID and group ID. |
Unset | -t | If destination doesn't exist, duplicate the mode only. |
If cp is copying multiple files or doing a recursive copy, but you didn't specify the -R option, cp refuses 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.