cp Command
Purpose
Copies the content from a source file to a destination file.
Syntax
cp [ -d ] [ -e ] [ -E { force|ignore|warn } ] [ -f ] [ -h ] [ -i ] [ -p ] [ -I ] [ -S ] [ -U ] [ -- ] SourceFile TargetFile
Description
During the copying process, if the cp command creates a directory, the mode of the new directory is same as the corresponding source directory.
You can copy the special device files by using the -R flag. When you specify the -R flag, the special files are re-created under the new path name. When you specify the -r flag, the cp command attempts to copy the special file to a regular file.
Flags
| Item | Description |
|---|---|
| -d | Specifies that the source file is stored in a decrypted or clear-text format on the target file. |
| -e | Specifies that the source file is stored in an encrypted form, if the target file system is an Encrypted File System (EFS). |
| -E | The -E option requires one of the following arguments:
|
| -f | Specifies the removal of the target file if it cannot be opened for write operations. The removal of the target file precedes the copying operation of the cp command. |
| -h | Forces the cp command to copy the symbolic links. The default is to follow the symbolic links. Symbolic files are to copy the files to which the symbolic links point. |
| -H | Acts based on the type and contents of the file that is referenced by any symbolic link that is specified as a SourceFile operand. |
| -i | Prompts the name of a file that you must overwrite. This prompt occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. If you enter y the cp command continues. Any other answer prevents the cp command from overwriting the file. |
| -I | Suppresses the warning message during the Access control lists (ACLs) conversion. |
| -L | Acts based on the type and contents of the file that is referenced by any symbolic link that is specified as a SourceFile parameter operand or symbolic links that are encountered during the traversal of a file hierarchy. |
| -p | Duplicates the following characteristics of each SourceFile or
SourceDirectory in the corresponding TargetFile and/or
TargetDirectory:
The target file is not deleted if these characteristics cannot be preserved. If the user ID or a group ID cannot be duplicated, the file permissionS_ISUID and S_ISGID bits are cleared. You must have the appropriate file access permissions to preserve the owner ID and the group ID, permission modes, modification and access times, the user must have the appropriate file access permissions. The user must be a superuser or must have the same owner ID as the destination file. ACLs that are associated with the SourceFile parameter are preserved if the target filesystem supports the same. If the source file contains the Network File System (NFS4) ACL and the target filesystem does not support the NFS4 ACL, the NFS4 ACL is converted to AIX® Classic (AIXC) ACL type. When ACL conversion succeeds, a warning message is printed in the stderr file, which is the standard error output file.The cp command preserves the Encrypting File System (EFS) information if the source file is encrypted and the -p flag is specified. Generally, the -e or -d flag takes precedence over the -p flag. If a user requests to convert a clear-text file to an encrypted format by using the -e flag, even if the user specifies the -p flag, the copy does not preserve attributes like the time of the last data modification and the time of the last access. As long as the encryption or decryption status remains the same, the -p flag preserves the file attributes and the EFS information. |
| -P | Acts on any symbolic link that is specified as a SourceFile operand or any symbolic link that is encountered during the traversal of a file hierarchy. |
| -r | Copies the file hierarchies under the file or directory that is specified by the SourceFile or SourceDirectory parameter or recursive copy. The -r flag processes the special files in a manner similar to the regular files. |
| -R | Copies the file hierarchies under the regular files and directories from the directory that
is specified by the SourceFile or SourceDirectory parameter to
the directory specified by the TargetDirectory parameter. The special file types
such as first-in, first-out (FIFO) files and block and character device files are re-created instead
of copying. The symbolic links are followed unless the -h flag is specified.
The -R flag is preferred to the -r flag. You can specify
the following flags along with the -R flag:
|
| -S | Preserves the sparseness of the source file when it is copied to the
destination file if the source file is sparse. A |
| -U | Copies the Extended Attributes (EA) and ACLs in the SourceFile parameter to the TargetFile parameter. EA is ignored if the EA is not supported on the target filesystem. If the source ACL type is not supported on the target filesystem, it is converted to the compatible ACL type supported by the target filesystem. |
| -- | Indicates that the parameters that follow the dash, dash (--) flag are to be interpreted as the file names. This null flag allows the specification of the file names that start with a minus sign (-). |
| Explicit flag for the cp command | Source file | Target file system | Result |
|---|---|---|---|
| -e (encrypted) | Non-EFS | Non-EFS | Error |
| -e | Non-EFS | EFS | Encrypted file |
| -e | EFS | EFS | Encrypted file |
| -e | EFS | Non-EFS | Error |
| -d (decrypted) | Non-EFS | Non-EFS | Clear-text file |
| -d | Non-EFS | EFS | Clear-text file |
| -d | EFS | Non-EFS | Clear-text file |
| -d | EFS | EFS | Clear-text file |
| No explicit flag | Non-EFS | Non-EFS | Clear-text file |
| No explicit flag | Non-EFS | EFS | If the target directory is an EFS inheritance that is enabled, the target file is an encrypted file. Otherwise, the target file is a clear-text file. |
| No explicit flag | EFS | EFS | Encrypted file |
| No explicit flag | EFS | Non-EFS | Error |
Exit Status
The cp command returns the following exit values:
| Item | Description |
|---|---|
| 0 | Files are copied successfully. |
| >0 | An error occurred. |
Examples
- To create a copy of a file in the current directory, enter the following command:
This command copies the prog.c file to the prog.bak file. If the prog.bak file does not exist, the cp command creates the file. If the prog.bak file exists, the cp command replaces it with a copy of the prog.c file.cp prog.c prog.bak - To copy a file in your current directory into another directory, enter the following command:
This command copies thecp jones /home/nick/clientsjonesfile to the /home/nick/clients/jones file. - To copy a file to a new file and preserve the modification date, time, and ACL that is
associated with the source file, enter the following command:
This command copies thecp -p smith smith.jrsmithfile to the smith.jr file. Instead of creating the file with the current date and time stamp, the system gives the smith.jr file the same date and time as thesmithfile. The smith.jr file inherits the access control protection of thesmithfile. - To copy all the files in a directory to a new directory, enter the following command:
This command copies only the files in thecp /home/janet/clients/* /home/nick/customersclientsdirectory to thecustomersdirectory. - To copy a directory, including all its files and sub directories to another directory, enter the
following command:
cp /home/janet/clients/* /home/nick/customersNote: A directory cannot be copied into itself.This command copies theclientsdirectory, including all its files, sub directories, and the files in those sub directories, to thecustomers/clientsdirectory. - To copy a specific set of files to another directory, enter the following command:
This command copies thecp jones lewis smith /home/nick/clientsjonesfile,lewisfile, and thesmithfile in the working directory to the /home/nick/clients directory. - To use the pattern-matching characters to copy the files, enter the following command:
This command copies the files in thecp programs/*.c .programsdirectory that end with.cfile to the current directory, signified by the single dot (.). Insert a space between thecand the final dot. - To copy a file to a new file and to preserve the ACL and EA associated with the source file,
enter the following command:
cp -U smith smith.jr - To preserve the sparseness of the source file while you copy, enter the following
command:
cp -S file.c sparse_file.c
Files
- /usr/bin/cp
- Contains the cp command.