Cp command, cp command

Source: Internet
Author: User

Cp command (conversion), cp command

Original article: http://www.cnblogs.com/peida/archive/2012/10/29/2744185.html

The cp command is used to copy files or directories. It is one of the most common commands in Linux. In general, shell sets an alias. When copying a file under the command line, if the target file already exists, it will ask whether to overwrite the file, whether or not you use the-I parameter. However, if the cp is executed in a shell script, if the-I parameter is not displayed, the system does not ask whether to overwrite it. This indicates that the command line and shell script are executed differently.

1. Command Format:

Usage:

Cp [Option]... [-T] source objective

Or: cp [Option]... source... directory

Or: cp [Option]...-t directory source...

2. command functions:

Copy the source file to the target file, or copy multiple source files to the target directory.

3. command parameters:

-A, -- archive equals to-dR -- preserve = all

-- Backup [= CONTROL creates a backup for each existing target file

-B is similar to -- backup but does not accept parameters

-- Copy-contents: In recursive processing, copying special file content

-D equals -- no-dereference -- preserve = links

-F, -- force: if the target file cannot be opened, remove it and try again (when-n Option

This option does not need to be selected if it exists)

-I, -- interactive overwrite the previous query (invalidate the previous-n option)

-H follows the command line symbolic link in the source file

-L, -- link file without copying

-L, -- dereference always follows the Symbolic Link

-N, -- no-clobber do not overwrite existing files (invalidate the previous-I option)

-P, -- no-dereference does not follow the symbolic link in the source file

-P equals to -- preserve = mode, ownership, timestamp

-- Preserve [= the attribute list retains the specified attribute (default: mode, ownership, timestamp). If

Additional attributes may be maintained: environment, Link, xattr, etc.

-R,-r, -- recursive: copy the Directory and all projects in the directory

4. Command instance:

Example 1: copy a single file to the target directory. The file does not exist in the target file.

Command:

Cp log. log test5

Output:

[Root @ localhost test] # cp log. log test5

[Root @ localhost test] # ll

-Rw-r -- 1 root 0 10-28 log. log

Drwxr-xr-x 6 root 4096 10-27 0scf

Drwxrwxrwx 2 root 4096 10-28 test3

Drwxr-xr-x 2 root 4096 10-28 test5

[Root @ localhost test] # cd test5

[Root @ localhost test5] # ll

-Rw-r -- 1 root 0 10-28 log5-1.log

-Rw-r -- 1 root 0 10-28 log5-2.log

-Rw-r -- 1 root 0 10-28 log5-3.log

-Rw-r -- 1 root 0 10-28 14:53 log. log

Note:

When the-a parameter is not included, the time of the two files is different. When the-a parameter is included, the time of the two files is the same.

 

Example 2: If the target file exists, you will be asked if it is overwritten.

Command:

Cp log. log test5

Output:

[Root @ localhost test] # cp log. log test5

Cp: overwrite "test5/log. log "? N

[Root @ localhost test] # cp-a log. log test5

Cp: overwrite "test5/log. log "? Y

[Root @ localhost test] # cd test5/

[Root @ localhost test5] # ll

-Rw-r -- 1 root 0 10-28 log5-1.log

-Rw-r -- 1 root 0 10-28 log5-2.log

-Rw-r -- 1 root 0 10-28 log5-3.log

-Rw-r -- 1 root 0 10-28 log. log

Note:

If the target file exists, you will be asked whether to overwrite it. This is because cp is the alias of cp-I. When the target file exists, even if the-f flag is added, the system will also ask whether to overwrite the file.

 

Example 3: copy the entire directory

Command:

Output:

When the target directory exists:

[Root @ localhost test] # cp-a test3 test5

[Root @ localhost test] # ll

-Rw-r -- 1 root 0 10-28 log. log

Drwxr-xr-x 6 root 4096 10-27 0scf

Drwxrwxrwx 2 root 4096 10-28 test3

Drwxr-xr-x 3 root 4096 10-28 test5

[Root @ localhost test] # cd test5/

[Root @ localhost test5] # ll

-Rw-r -- 1 root 0 10-28 log5-1.log

-Rw-r -- 1 root 0 10-28 log5-2.log

-Rw-r -- 1 root 0 10-28 log5-3.log

-Rw-r -- 1 root 0 10-28 log. log

Drwxrwxrwx 2 root 4096 10-28 test3

The target directory does not exist:

[Root @ localhost test] # cp-a test3 test4

[Root @ localhost test] # ll

-Rw-r -- 1 root 0 10-28 log. log

Drwxr-xr-x 6 root 4096 10-27 0scf

Drwxrwxrwx 2 root 4096 10-28 test3

Drwxrwxrwx 2 root 4096 10-28 test4

Drwxr-xr-x 3 root 4096 10-28 test5

[Root @ localhost test] #

Note:

Note that the results of the existence of the target directory are different. If the target directory exists, the entire source directory is copied to the target directory.

 

Example 4: Create a log_link.log link for the copied log. log

Command:

Cp-s log. log log_link.log

Output:

[Root @ localhost test] # cp-s log. log log_link.log

[Root @ localhost test] # ll

Lrwxrwxrwx 1 root 7 10-28 log_link.log-> log. log

-Rw-r -- 1 root 0 10-28 log. log

Drwxr-xr-x 6 root 4096 10-27 0scf

Drwxrwxrwx 2 root 4096 10-28 test3

Drwxrwxrwx 2 root 4096 10-28 test4

Drwxr-xr-x 3 root 4096 10-28 test5

 

Note:

The log_link.log is caused by the-s parameter and is created with a "shortcut". Therefore, you can see that it is on the rightmost side of the file, the location where the file is linked is displayed!

 

 

 

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.