cp - Copy files

Synopsis

cp [-r | -R [-H | -L | -P] ] [-fhipt] source_file target_file

cp [-r | -R [-H | -L | -P] ] [-fhipt] source_file ... target_directory

Description

In the first synopsis form, the cp utility copies the contents of the source_file to the target_file.

In the second synopsis form, the cp utility copies the contents of each named source_file to a file in the destination target_directory. The names of the files themselves are not changed. The target_directory must exist unless there is only one named source_file which is a directory and the -R flag is specified.

If cp detects an attempt to copy a file to itself, the copy will fail.

If target_file does not exist, the mode of the source_file is used, as modified by the file creation mask, when creating target_file. The S_ISUID and S_ISGID file permission bits are never set when creating a new file.

If target_file already exists and the -t option is not specified, its contents are overwritten as binary data and the CCSID attribute is changed to match the CCSID attribute of source_file. The file permission bits, owner, and group of target_file are unchanged. You can force the data to be copied as text data by using the -t option. You can force the file permission bits, owner, and group to be copied using the -p option.

Note that when copying to members in the QSYS.LIB file system, many attributes of source_file cannot be preserved because they are associated with the file object and not the member.

Symbolic links are always followed unless the -h option is specified or the -R option is specified with the -H or the -L options. The -H, -L and -P options are ignored unless the -R option is specified. In addition, these options override each other and the command's actions are determined by the last one specified.

Options

-H
If the -R option is specified, symbolic links on the command line are followed. Symbolic links encountered in the tree traversal are not followed and the symbolic link is copied instead of the file pointed to by the symbolic link.
-L
If the -R option is specified, both symbolic links on the command line and symbolic links encountered in the tree traversal are followed.
-P
If the -R option is specified, no symbolic links are followed. A symbolic link encountered in the tree traversal is copied instead of the file pointed to by the symbolic link.
-R
If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. This option causes cp to create special files rather than copying them as normal files. Created directories have the same mode as the corresponding source directory, unmodified by the file creation mask.
-f
Remove target_file if it cannot be opened for write operations. A new file is created before the data is copied.
-h
Copy symbolic links instead of the file pointed to by the symbolic link.
-i
Write a prompt to standard error before copying a file that would overwrite an existing file. If the response from the standard input begins with the first character for the YES response in the current locale, the file copy is attempted.
-p
Preserve in the copy as many of the modification time, access time, file permission bits, owner, and group as allowed by permissions.

If the owner and group cannot be preserved, no error message is displayed and the exit value is not altered.

The S_ISUID and S_ISGID file permission bits are only copied when both the owner and group of the file are successfully copied.

Note:
This option has no effect when copying to the QSYS.LIB file system.
-r
Same as -R except this option copies special files in the same manner as regular files. The -R flag is preferred to the -r flag.
-t
When the target file exists, treat the data in source_file as text data and translate the data to the CCSID associated with target_file as it is copied. The CCSID attribute of target_file is not changed.

Exit status

  • 0 on success
  • >0 if an error occurred.
Examples
  1. Copy the file, "file1", into the subdirectory, "data.dir".
    
    cp file1 data.dir
    
  2. Copy all the files with the .java extension from the "code" subdirectory into the subdirectory, "code/old_code.dir" and prompt the user for overwrite verification only if the file already exists in the subdirectory, "code/old_code.dir".
    
    cp -i code/*.java code/old_code.dir