Linux Command: cp command learning and use, linux Command cp Learning

Source: Internet
Author: User

Linux Command: cp command learning and use, linux Command cp Learning

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-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:

Cplog. logtest5

Output:

[Root @ localhosttest] # cplog. logtest5

[Root @ localhosttest] # ll

-Rw-r-r-1rootroot010-2814: 48log. log

Drwxr-xr-x6rootroot409610-2701: 58scf

Drwxrwxrwx2rootroot409610-2814: 47test3.

Drwxr-xr-x2rootroot409610-2814: 53test5.

[Root @ localhosttest] # cdtest5

[Root @ localhosttest5] # ll

-Rw-r-r-1rootroot010-2814: 46log5-1.log

-Rw-r-r-1rootroot010-2814: 46log5-2.log

-Rw-r-r-1rootroot010-2814: 46log5-3.log

-Rw-r-r-1rootroot010-2814: 53log. 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:

Cplog. logtest5

Output:

[Root @ localhosttest] # cplog. logtest5

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

[Root @ localhosttest] # cp-alog.logtest5

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

[Root @ localhosttest] # cdtest5/

[Root @ localhosttest5] # ll

-Rw-r-r-1rootroot010-2814: 46log5-1.log

-Rw-r-r-1rootroot010-2814: 46log5-2.log

-Rw-r-r-1rootroot010-2814: 46log5-3.log

-Rw-r-r-1rootroot010-2814: 48log. 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 @ localhosttest] # cp-atest3test5

[Root @ localhosttest] # ll

-Rw-r-r-1rootroot010-2814: 48log. log

Drwxr-xr-x6rootroot409610-2701: 58scf

Drwxrwxrwx2rootroot409610-2814: 47test3.

Drwxr-xr-x3rootroot409610-2815: 11test5

[Root @ localhosttest] # cdtest5/

[Root @ localhosttest5] # ll

-Rw-r-r-1rootroot010-2814: 46log5-1.log

-Rw-r-r-1rootroot010-2814: 46log5-2.log

-Rw-r-r-1rootroot010-2814: 46log5-3.log

-Rw-r-r-1rootroot010-2814: 48log. log

Drwxrwxrwx2rootroot409610-2814: 47test3.

The target directory does not exist:

[Root @ localhosttest] # cp-atest3test4

[Root @ localhosttest] # ll

-Rw-r-r-1rootroot010-2814: 48log. log

Drwxr-xr-x6rootroot409610-2701: 58scf

Drwxrwxrwx2rootroot409610-2814: 47test3.

Drwxrwxrwx2rootroot409610-2814: 47test4.

Drwxr-xr-x3rootroot409610-2815: 11test5

[Root @ localhosttest] #

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-slog.loglog_link.log

Output:

[Root @ localhosttest] # cp-slog.loglog_link.log

[Root @ localhosttest] # ll

Lrwxrwxrwx1rootroot710-2815: 18log_link.log-> log. log

-Rw-r-r-1rootroot010-2814: 48log. log

Drwxr-xr-x6rootroot409610-2701: 58scf

Drwxrwxrwx2rootroot409610-2814: 47test3.

Drwxrwxrwx2rootroot409610-2814: 47test4.

Drwxr-xr-x3rootroot409610-2815: 11test5

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.