cp Command

Purpose

Copies files.

Syntax

To copy a file to another file, use the following syntax:

cp [ -d ] [ -e ] [ -E {force|ignore|warn} ] [ -f ] [ -h ] [ -i ] [ -p ] [ -I ] [ -U ] [ -- ] SourceFile TargetFile

To copy a file to a directory, use the following syntax:

cp [ -d ] [ -e ] [ -E {force|ignore|warn} ] [ -f ] [ -h ] [ -i ] [ -p ] [[ -r | -R ] [ -H | -L | -P ]] [ -I ] [ -U ] [ -- ] SourceFile ... TargetDirectory

To copy a directory to a directory, use the following syntax:

cp [ -d ] [ -e ] [ -E {force|ignore|warn} ] [ -f ] [ -h ] [ -i ] [ -p ] { -r | -R } [ -H | -L | -P ] [ -I ] [ -U ] [ -- ] SourceDirectory ... TargetDirectory

Description

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.

Note: If one of the source parameters is a directory, you need to specify one of the -r or -R flags.

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.

Note: The I/O buffer size for the read and write system calls generated by this command can be configured by using the AIX_STDBUFSZ environment variable.

Flags

Item Description
-d Specifies that the source file is stored in decrypted (clear-text) format on target.
-e Specifies that the source file is stored in encrypted form, if the target file system is an Encrypted File System (EFS).
-E The -E option requires one of the following arguments. If you omit the -E option, warn is the default behavior.
force
Fails the cp operation on a file if the fixed extent size or space reservation of the file cannot be preserved.
ignore
Ignores any errors in preserving extent attributes.
warn
Issues a warning if the space reservation or the fixed extent size of the file cannot be preserved.
-f Specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command.
-h Forces the cp command to copy symbolic links. The default is to follow symbolic links, that is, to copy files to which symbolic links point.
-H Take actions based on the type and contents of the file referenced by any symbolic link specified as a SourceFile operand.
-i Prompts you with the name of a file to be overwritten. This 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 or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file.
-I Suppresses the warning message during ACL conversion.
-L Take actions based on the type and contents of the file referenced by any symbolic link specified as a SourceFile operand or any symbolic links encountered during traversal of a file hierarchy.
-p Duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory:
  • The time of the last data modification and the time of the last access. If this duplication fails for any reason, the cp command will write a diagnostic message to standard error. The nanoseconds field of the SourceFile/SourceDirectory is not duplicated for last modification time or last access time.
  • The user ID and group ID. If this duplication fails for any reason, the cp command may write a diagnostic message to standard error.
  • The file permission bits and the S_ISUID and S_ISGID bits. If this duplication fails for any reason, the cp command will write a diagnostic message to standard error.

If the user ID or group ID cannot be duplicated, the file permission bits S_ISUID and S_ISGID are cleared.

In order to preserve the owner ID and group ID, permission modes, modification and access times, user must have the appropriate file access permissions (user should be a superuser or have the same owner ID as the destination file)

The target file will not be deleted if these characteristics cannot be preserved.

Access control lists (ACLs) associated with the SourceFile are preserved if the target filesystem supports the same. If the source file contains NFS4 ACL and the target filesystem does not support NFS4 ACL, the NFS4 ACL is converted to AIXC.

When ACL conversion succeeds, a warning message is printed out the stderr.

If the source file is encrypted and the -p flag is specified, the cp command preserves the EFS information. 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 using the -e flag, then even if the user specifies the -p flag, the copy does not preserve attributes like the time of the last data modification, the time of the last access and so on. As long as the encryption or decryption status remains the same, the -p flag preserves the file attributes and EFS information.

-P Take actions on any symbolic link specified as a SourceFile operand or any symbolic link encountered during traversal of a file hierarchy.
-r Copies file hierarchies under the file or directory specified by the SourceFile or SourceDirectory parameter (recursive copy). The -r flag processes special files in the same manner as regular files.
-R Copies file hierarchies under the regular files and directories from the directory specified by the SourceFile or SourceDirectory parameter to the directory specified by the TargetDirectory parameter. Special file types, such as first-in, first-out (FIFO) files and block and character device files, are re-created instead of copied. Symbolic links are followed unless the -h flag is specified. (The -R flag is preferred to the -r flag.)
If none of the -H, -L, or -P options were specified, it is unspecified which of those options will be used as the default. Consider the following:
  • If the -H option was specified, the cp command will take action based on the type and contents of the file referenced by any symbolic link specified as a SourceFile operand.
  • If the -L option was specified, the cp command will take action based on the type and contents of the file referenced by any symbolic link specified as a SourceFile operand or any symbolic links encountered during traversal of a file hierarchy.
  • If the -P option was specified, the cp command will copy any symbolic link specified as a SourceFile operand and any symbolic links encountered during traversal of a file hierarchy and will not follow any symbolic links.
-U Copies Extended Attributes (EA), Access Control Lists ( ACL ) in the SourceFile to the TargetFile. If the EA is not supported on the target filesystem then it is ignored. If the source ACL type is not supported on the target filesystem then it is converted to the compatible ACL type supported by the target filesystem.
-- Indicates that parameters following the -- (dash, dash) flag are to be interpreted as file names. This null flag allows the specification of file names that start with a - (minus sign).
The following table shows the encryption or decryption status of the target file under different conditions:
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 EFS inheritance 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
Note: It is not permitted to overwrite an encrypted file with a plain-text file and vice versa unless you specify the -f flag. The encryption status of the target depends on the -e or -d flag, the encryption inheritance if you do not specify the -e or -d flag with the -f flag, and the encryption status of the source file if the encryption inheritance is not active.

Exit Status

This command returns the following exit values:

Item Description
0 All files were copied successfully.
>0 An error occurred.

Examples

  1. To make a copy of a file in the current directory, enter:
    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.
  2. To copy a file in your current directory into another directory, enter:
    cp jones /home/nick/clients
    This copies the jones file to /home/nick/clients/jones.
  3. To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
    cp -p smith smith.jr
    This copies the smith file 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 the smith file. The smith.jr file also inherits the smith file's access control protection.
  4. To copy all the files in a directory to a new directory, enter:
    cp /home/janet/clients/* /home/nick/customers
    This copies only the files in the clients directory to the customers directory.
  5. To copy a directory, including all its files and subdirectories, to another directory, enter:
    cp /home/janet/clients/* /home/nick/customers
    Note: A directory cannot be copied into itself.
    This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory.
  6. To copy a specific set of files to another directory, enter:
    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.
  7. To use pattern-matching characters to copy files, enter:
    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.
  8. To copy a file to a new file and preserve the ACL and EA associated with the source file, enter:
    cp -U smith smith.jr

Files

/usr/bin/cp
Contains the cp command.