Copies files.
cp [ -E{force|ignore|warn} ] [ -f ] [ -h ] [ -i ] [ -p ] [ -I ] [ -U ] [ - ] SourceFile TargetFile
cp [ -E{force|ignore|warn} ] [ -f ] [ -h ] [ -i ] [ -p ] [[ -r | -R ] [ -H | -L | -P ]] [ -I ] [ -U ] [ - ] SourceFile ... TargetDirectory
cp [ -E{force|ignore|warn} ] [ -f ] [ -h ] [ -i ] [ -p ] { -r | -R } [ -H | -L | -P ] [ -I ] [ -U ] [ - ] SourceDirectory ... TargetDirectory
The cp command copies the source file specified by the SourceFile parameter to the destination file specified by the TargetFile parameter. If the target file exists, cp overwrites the contents, but the mode, owner, and group associated with it are not changed. The last access time of the SourceFile and the last modification time of the TargetFile are set to the time the copy was done. If the TargetFile does not exist, cp creates a new file named TargetFile that has the same mode as the source file except that the sticky bit is not set unless it was done by a superuser; the owner and group of the TargetFile is that of the user. When the TargetFile is a link to another file, cp overwrites the destination link with the content of the source file; the links from the TargetFile remains. Also, the cp command can copy the source files specified by the SourceFile parameter (or directories named by the SourceDirectory parameter) to the directory specified by the TargetDirectory parameter.
If any directories are created by the cp command during the copying process, the newly created directory will have the same mode as the corresponding source directory.
You can also copy special device files. The preferred option for accomplishing this is the -R flag. Specifying -R causes the special files to be re-created under the new path name. Specifying the -r flag causes the cp command to attempt to copy the special file to a regular file.
This command returns the following exit values:
| 0 | All files were copied successfully. |
| >0 | An error occurred. |
cp prog.c prog.bak
This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces it with a copy of the prog.c file.
cp jones /home/nick/clients
This copies the jones file to /home/nick/clients/jones.
cp /home/janet/clients/* /home/nick/customers
This copies only the files in the clients directory to the customers directory.
cp jones lewis smith /home/nick/clients
This copies the jones, lewis, and smith files in your current working directory to the /home/nick/clients directory.
cp programs/*.c .
This copies the files in the programs directory that end with .c to the current directory, signified by the single . (dot). You must type a space between the c and the final dot.
cp -U smith smith.jr
The mv command.