8.4.5 cp command (copies files or directories)
Syntax
cp [-f|-i] [-p] [-u] [-R|-r [-H|-L|-P]] copy-source-file-name copy-target-file-name cp [-f|-i] [-p] [-u] [-R|-r [-H|-L|-P]] copy-source ... copy-target-directory-name
Description
The command copies files or directories.
Arguments
-f
- --force
-
This command does not issue warning when the file at the destination is overwritten. If the -f and -i options are both specified, the option specified last takes effect.
-i
- --interactive
-
This command specifies that when the file at the destination is overwritten, a warning is to be issued asking the user to respond. Copying is 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.
If the -f and -i options are both specified, the option specified last takes effect.
-p
- --preserve
-
This command saves the attributes of the source file.
In Windows, the last modified date and time and the last access date and time of each source file are retained. Directory information is not retained.
In UNIX, the owner, group, access permissions, last modified date and time, and last access date and time of each source file are retained.
-u
- --update
-
This command specifies that copying is to not be executed when the destination file already exists and the last modified date and time are the same as or later than those of the source when copying a file other than directory.
When copying a symbolic link, whether to copy the symbolic link is determined based on the last modified date and time if the destination is a symbolic link. If the destination is a file, whether to copy the symbolic link is determined based on the last modified date and time of the destination file.
The last modified date and time of a file is determined upon rounding off values smaller than a second.
-R|-r
- --recursive
-
This command recursively copies a directory.
In Windows, the directory delimiter at the end is ignored even if you specify the symbolic link to the directory having the directory delimiter at the end for the copy source. To copy the directory of the link destination of the symbolic link, specify this option along with the -H or -L option.
- -H
-
This command follows the symbolic link specified in the command line when this option is specified together with the -R option or -r option.
This command does not follow symbolic links encountered while traversing the tree.
This option is applied to the source.
This option is ignored if either the -R or -r option is not specified. If more than one of the -H, -L, and -P options are specified, the option specified last takes effect.
-L
- --dereference
-
If this option is simultaneously specified with the -R or -r option, the command follows all symbolic links that are encountered.
This option is applied to the source.
This option is ignored if either the -R or -r option is not specified. If more than one of the -H, -L, and -P options are specified, the option specified last takes effect.
-P
- --no-dereference
-
If this option is simultaneously specified with the -R or -r option, the command does not follow any symbolic links.
This option is applied to the source.
This option is ignored if either the -R or -r option is not specified. If more than one of the -H, -L, and -P options are specified, the option specified last takes effect.
- copy-source-file-name
-
This command specifies the name of the file to be copied.
- copy-target-file-name
-
This command specifies the name of the file at the destination.
- copy-source
-
This command specifies the file or directory to be copied.
- copy-target-directory-name
-
This command specifies the directory 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 and output are performed in the binary mode. No conversion of end-of-line codes is performed.
-
In Windows, if you specify the -f option and attempt to overwrite a file in the copy destination, depending on the status of the Windows environment in which the command is executed, the error Permission denied might be output.
In UNIX, when a general user preserves the attributes of the copy source file by specifying the -p option of the cp command 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.
-
If the destination is new (including cases when the destination is the same) with specification of the -u option, copying is normally terminated without an error when a file or directory is not copied.
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 [-fipu] [-Rr [-H | -L | -P]] source target cp [-fipu] [-Rr [-H | -L | -P]] source ... directory
Display an error message if a file does not exist.
C:\TEMP>%ADSH_OSCMD_DIR%\cp file99 file123 cp: file99: No such file or directory