The
Linux CP Command Copy command is very useful, and the CP command can copy files (directories) under the specified path to another file or directory, equivalent to DOS copy and Xcopy commands. The CP command syntax is as follows:
CP [Options] <source file or directory> <target file or directory>
Or
CP [Options] source1 source2 source3 ... directory
The first command above is a copy of a single file or directory, and the next one is copied to the final directory for multiple files.
The options option includes:
-a preserves the link and file attributes, recursively copies the directory, equivalent to the following D, P, r three option combinations.
-D Keep links when copying.
-F Deletes the destination file that already exists without prompting.
-I will give a confirmation prompt before overwriting the target file, which is an interactive copy.
-P Copies the contents of the source file and also copies its modification time and access rights to the new file.
-R If the source file is a directory file, the CP will recursively replicate all subdirectories and files in the directory. Of course, the destination file must be a directory name.
-L do not copy, just link file.
-s copy into the symbolic link file (symbolic link), the "shortcut" file;
-U if destination is newer than source old destination.
Example of CP command usage:
1, copy the document FILE1 into File2, after the name is changed file2
Cp-i file1 file2
Or
CP file1 File2
2, copy the document FILE1 to the Dir1 directory, after the name has not been copied file1
Cp-i file1 Dir1
Or
CP File1 Dir1
3, the directory Dir1 copied to the Dir2 directory, the replication results directory was renamed DIR2
Cp-r Dir1 Dir2
4, the directory Dir1 all files including folders, are copied to the Dir2 directory
Cp-r dir1/*.* Dir2
Common errors:
1. Prompt cp:omitting Directory Error
When replicating a directory, use the-r option to copy recursively, as follows:
Cp-r Dir1 Dir2
Instance:
First, copy a directory under the/opt/a/to the/opt/b/directory
Cp-r/opt/a//opt/b/A. Record under/opt/a/to the/opt/b/directory