CP command in Linux (file copy)

Source: Internet
Author: User
Tags file copy

The CP command is used to copy one or more source files or directories to a specified destination file or directory. It can copy a single source file into a specific file with a specified file name or a directory that already exists. The CP command also supports copying multiple files at once, and when copying multiple files at a time, the target file parameter must be a directory that already exists, or an error will occur.

Grammar
CP (option) (parameter)
Options
-A: This parameter has the same effect as specifying the "-DPR" parameter;-D: When a symbolic connection is copied, the destination file or directory is also established as a symbolic connection, pointing to the original file or directory connected to the source file or directory;-F: Forcibly copying a file or directory regardless of whether the destination file or directory already exists ;-I: Ask the user before overwriting the existing file;-L: Make a hard connection to the source file, not copy the file;-P: Preserves the properties of the source file or directory;-R/R: Recursively handles all files under the specified directory with subdirectories;-S: Establish symbolic connections to source files instead of copying files ;-U: Copy files only when the source file is changed at the same time as the target file is updated or when the target file corresponding to the name does not exist;-S: Replaces the default suffix of the file with the specified suffix "SUFFIX" when backing up the file;-B: Back up the target file before overwriting the existing file target ;-V: Displays details of what the command does.
Parameters
    • Source file: Make a list of source files. by default, the CP command cannot replicate the directory, and if you want to copy the directory, you must use the -R option ;
    • Destination file: Specify the destination file. When the source file is multiple files, the target file is required to be the specified directory.
Instance

If a file is copied to a target file and the target file already exists, the contents of the target file will be destroyed. All parameters in this command can be either an absolute pathname or a relative path name. Usually in . .. the form of points or dots. For example, the following command copies the specified file to the current directory:

Cp.. /mary/homework/assign.

The directory specified by all destination files must be present, and the CP command cannot create a directory. If you do not have file copy permissions, the system displays an error message.

Copy file to directory and /usr/men/tmp rename to File1

CP File/usr/men/tmp/file1

/usr/mencopy all files and their subdirectories under the directory to the directory /usr/zh

Cp-r/usr/men/usr/zh

Interactively /usr/men copy all. c files in the directory that begin with M to the directory /usr/zh

Cp-i/usr/men M*.c/usr/zh

When we use the CP command to copy files under Linux, sometimes we will need to overwrite some files with the same name, when overwriting the file, we will have to keep pressing Y to determine the execution overwrite. The number of files is not much good, but if hundreds of estimates press Y to vomit blood, so toss a half-day summed up a method:

CP aaa/*/BBB
Copy directory AAA under All to the/BBB directory, at this time if the/BBB directory has the same name as the AAA file, you need to press Y to confirm and will skip the sub-directory under the AAA directory.
Cp-r aaa/*/bbb
This time you still need to press Y to confirm the operation, but the subdirectories are not ignored.
Cp-r-a aaa/*/bbb
Still need to press Y to confirm the operation, and the AAA directory and subdirectories and file attributes are also passed to the/bbb.
\cp-r-a aaa/*/bbb
Successfully, no prompt was pressed for y, the directory attribute was passed, and the directory was not skipped.

CP command in Linux (file copy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.