cp Command

Purpose

Copies the content from a source file to a destination file.

Syntax

To copy a file to another file, use the following syntax:
cp [ -d ] [ -e ] [ -E { force|ignore|warn } ] [ -f ] [ -h ] [ -i ] [ -p ] [ -I ] [ -S ] [ -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 that the SourceFile parameter specifies to the destination file. The TargetFile parameter specifies the destination file. If the target file exists, the cp command overwrites the contents, but the mode, owner, and group that is associated with it are not changed. The last access time of the SourceFile parameter and the last modification time of the TargetFile parameter are set to the time that the copy was done. If the TargetFile parameter does not exist, the cp command creates a new file that is named as the TargetFile parameter. The TargetFile parameter has the same mode as the source file except that the sticky bit is not set until the superuser sets it. The superuser is the owner and the group of the TargetFile parameter is that of the user. When the TargetFile parameter is a link to another file, the cp command overwrites the destination link with the content of the source file. The links from the TargetFile parameter remains. The cp command copies the source files that are specified by the SourceFile parameter or directories that are named by the SourceDirectory parameter to the directory that is specified by the TargetDirectory parameter.
Note: If one of the source parameters is a directory, you must specify the -r or -R flags.

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.

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

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:
force
Fails the copy operation on a file if the fixed extent size or space reservation of the file cannot be preserved.
ignore
Ignores any errors in the preserving extent attributes.
warn
Issues a warning if the space reservation or the fixed extent size of the file cannot be preserved.
If you do not specify the -E flag, the warn argument is the default behavior.
-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 time of the last data modification and the time of the last access. If this duplication fails for any reason, the cp command writes a diagnostic message to a standard error. The nanoseconds field of the SourceFile or the SourceDirectory parameter is not duplicated for last modification time or for the last access time.
  • The user ID and group ID. If this duplication fails for any reason, the cp command writes a diagnostic message to a standard error.
  • The file permission bits and the S_ISUID and S_ISGID bits. If this duplication fails for any reason, the cp command writes a diagnostic message to a standard error.

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 permission S_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:
-H
If you specify the -H option, the cp command acts based on the type and contents of the file that is referenced by any symbolic link, which is specified as a SourceFile operand.
-L
If you specify the -L option, the cp command acts based on the type and contents of the file that is referenced by any symbolic link, which is specified as a SourceFile operand or as any symbolic links that are encountered during the traversal of a file hierarchy.
-P
If you specify the -P option, the cp command copies any symbolic link that is specified as a SourceFile operand and any symbolic links that are encountered during the traversal of a file hierarchy and does not follow any of the symbolic links.
-S Preserves the sparseness of the source file when it is copied to the destination file if the source file is sparse.

A sparse file is a type of computer file that attempts to use the file system space more efficiently when the blocks that are allocated to the file are empty.

-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 (-).
The following table shows the encryption or decryption status of the target file under different conditions:
Table 1. Target file encryption or decryption status
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
Note: Do not overwrite an encrypted file with a plain-text file and a plain-text file with an encrypted file 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

The cp command returns the following exit values:

Item Description
0 Files are copied successfully.
>0 An error occurred.

Examples

  1. To create a copy of a file in the current directory, enter the following command:
    cp prog.c prog.bak
    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.
  2. To copy a file in your current directory into another directory, enter the following command:
    cp jones /home/nick/clients
    This command copies the jones file to the /home/nick/clients/jones file.
  3. 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:
    cp -p smith smith.jr
    This command 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 inherits the access control protection of the smith file.
  4. To copy all the files in a directory to a new directory, enter the following command:
    cp /home/janet/clients/* /home/nick/customers
    This command copies only the files in the clients directory to the customers directory.
  5. To copy a directory, including all its files and sub directories to another directory, enter the following command:
    cp /home/janet/clients/* /home/nick/customers
    Note: A directory cannot be copied into itself.
    This command copies the clients directory, including all its files, sub directories, and the files in those sub directories, to the customers/clients directory.
  6. To copy a specific set of files to another directory, enter the following command:
    cp jones lewis smith /home/nick/clients
    This command copies the jones file, lewis file, and the smith file in the working directory to the /home/nick/clients directory.
  7. To use the pattern-matching characters to copy the files, enter the following command:
    cp programs/*.c .
    This command copies the files in the programs directory that end with .c file to the current directory, signified by the single dot (.). Insert a space between the c and the final dot.
  8. 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
  9. 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.