cp command (copies a file or directories)
- Organization of this page
Format
cp [-f|-i] [-p] [-R|-r [-H|-L|-P]] copy-source-file-name copy-target-file-name cp [-f|-i] [-p] [-R|-r [-H|-L|-P]] copy-source ... copy-target-directory-name
Description
The command copies files or directories.
Arguments
- -f
-
Specifies that no warning is to be issued if the target file will be overwritten. When the -f and -i options are both specified, the one specified last takes effect.
- -i
-
Specifies that if the target file will be overwritten, a warning is to be issued asking the user to confirm the copy processing. Copying will be performed if the response from the standard input begins with the letter y or Y. If the response begins with any other character, or if the standard input is not available, processing is interrupted and the command terminates with a return code of 0.
When the -f and -i options are both specified, the one specified last takes effect.
- -p
-
Specifies that the attributes of the copy source file are to be kept intact (preserved).
In Windows, the most recent modification date and time and the most recent access date and time of the source file are preserved, but directory information is not preserved.
In UNIX, the owner, group, access permissions, last modification date and time, and last access date and time of each source file are preserved.
- -R|-r
-
Specifies that directories are to be copied recursively.
- -H
-
In UNIX, when the -R or -r option is specified, specifies that symbolic links on the command line are to be followed.
Symbolic links encountered while traversing the tree are not followed.
This option is ignored if neither the -R nor the -r option is specified. If more than one of the -H, -L, and -P options is specified, the one specified last takes effect.
In Windows, this option is ignored.
- -L
-
In UNIX, when the -R or -r option is specified, specifies that all symbolic links that are encountered are to be followed.
This option is ignored if neither the -R nor the -r option is specified. If more than one of the -H, -L, and -P options is specified, the one specified last takes effect.
In Windows, this option is ignored.
- -P
-
In UNIX, when the -R or -r option is specified, specifies that symbolic links are not to be followed.
This option is ignored if neither the -R nor the -r option is specified. If more than one of the -H, -L, and -P options is specified, the one specified last takes effect.
In Windows, this option is ignored.
- copy-source-file-name
-
Specifies the name of the file to be copied.
- copy-target-file-name
-
Specifies the name of the target file into which the source file's contents are to be copied.
- copy-source
-
Specifies a file or directory to be copied.
- copy-target-directory-name
-
Specifies the directory into which the copy source's contents are to be copied.
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 or greater |
Error termination |
Notes
-
In Windows, a target file is created with the file name specified for the copy source. However, uppercase letters in file names are converted to lowercase when copied. For example, if the name of the target file is A.txt and you execute cp a.txt tmpdir, the name of the file in tmpdir will be a.txt.
-
In Windows, file input/output is performed in the binary mode. No conversion of end-of-line codes is performed.
-
In UNIX, when a general user preserves the attributes of the copy source file by specifying the -p option but the owner of the source file is not the user executing the cp command, the owner, group, and access permission information (setuid bit, setgid bit, and sticky bit) of the source file will not be preserved.
Usage examples
-
Specify the -i option to ask the user for confirmation before overwriting the target file.
C:\TEMP>%ADSH_OSCMD_DIR%\cp -i file1.txt file2.txt overwrite file2.txt? y C:\TEMP>%ADSH_OSCMD_DIR%\cp -i file1.txt file2.txt overwrite file2.txt? n
-
Display an option error message.
This message might vary depending on the platform on which the command is executed. The following shows an example in Windows:
C:\TEMP>%ADSH_OSCMD_DIR%\cp -w cp: illegal option -- w usage: cp [-fip] [-Rr [-H | -L | -P]] source target cp [-fip] [-Rr [-H | -L | -P]] source ... directory
-
Display an error message if the file does not exist.
C:\TEMP>%ADSH_OSCMD_DIR%\cp file99 file123 cp: file99: No such file or directory