cp copies files. Copying means: the file is afterwards physically present in two locations.
cp has four formats. The command copies
one file to another with a different name (Format 1)
one or more files to a different directory, with the copy retaining the same basename as the corresponding original (Format 2).
each file file in the file hierarchy to one of the target paths specified below (formats 3 and 4)
Syntax
Format 1: |
cp[ -fip] file copyfile |
Format 2: |
cp[ -fip] file ... directory |
Format 3: |
cp -R[ -fip] file ... directory |
Format 4: |
cp -r[ -fip] file ... directory |
Copy one file
If it is impossible to obtain a file descriptor for copyfile (step 3.a.ii.), then an attempt is made to call unlink() for copyfile. Processing is continued.
(interactive) If the named copyfile already exists, cp will ask you to confirm whether this file may be overwritten. A y answer means that the copy operation should proceed. Any other answer prevents cp from overwriting copyfile.
The following properties of all files are duplicated in the corresponding copyfile:
If it is impossible to duplicate user ID or group ID then the bits for S_ISUID and S_ISGID are duplicated. The sequence for the duplication of the properties listed above is not defined. copyfile is not deleted if these properties cannot be retained.
Name of the original file.
Name of the copy file. Unless the -p option is used, the copy will have the same mode as the original file, and the user and group ID of the copy will be those of the user who called cp, but the time of last modification of the copy will be set to the time the copy was made. Caution! If copyfile is a link to a file, all links will be retained. The contents of copyfile will be overwritten with the contents of file.
Copy files to another directory
see Format 1
Name of the original file. You can give a list of names and thus copy several files at once. Each of the copies is assigned the same basic file name (basename) as the corresponding original. Caution!
Name of the directory in which the copies are to be placed. This must not be the directory in which the original files are located. Unless the -p option is set, the copies will have the same modes as the originals, and the user and group ID of the copies will be those of the user who called cp, but the time of last modification of each copy will be set to the time the copy was made.
see Format 1
copies data hierarchies. If the option -R is set then the following steps are performed:
If this procedure should fail for any reason then cp writes a message to the standard error output. Processing of file is halted. However, other files may be processed as appropriate.
see Format 2
see Format 1
Copies file hierarchies. The way in which special files are handled is implementation-dependent. If option -r is set then the way in which special files are handled is implementation-dependent.
see Format 2 |
Procedure
If directory already exists and is actually a directory, then the name of the target path for the individual files in the file hierarchy consists of a string formed by directory, a slash and the relative path name of the file corresponding to the directory which contains file. If directory does not yet exist and two operands are specified, then the name of the corresponding target path for file is the directory directory. The name of the corresponding target path for all other files in the file hierarchy consists of directory, a slash and the relative path name of the file corresponding to file. An error occurs if directory does not exist and more than two operands are specified. An error also occurs if directory exists and is a file type as defined in the XSH specification but is not a directory. In the following description file is the file to be copied irrespective of whether it is specified via an operand or whether it is specified in a file operand as a file in a file hierarchy. The term copyfile designates the file which is specified by the target path. The following steps are performed for each file:
|
Error
The named file does not exist.
|
Locale
The following environment variables affect the execution of cp: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_COLLATE Determines the internationalized environment for the behavior of ranges, equivalence classes and multicharacter collating elements used in the extended regular expressions defined for the affirmative response. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments). LC_CTYPE governs character classes, character conversion (shifting) and the behavior of character classes in regular expressions. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
The file techlit is to be copied before it is changed. The copy is to be called tl and to be located in the same directory as techlit.
|
Example 2
All files from the current directory with names beginning with fil are to be copied into the directory /home/do/save, and their times of last modification are to be duplicated.
|
See also
chmod, ln, mv, rm |