Linux command: CP

Source: Internet
Author: User

Introduction to CP Commands:

The CP command is used to copy files or directories and is one of the most commonly used commands in a Linux system. In general, the shell sets an alias, and when the file is copied at the command line, if the destination file already exists, it asks whether to overwrite it, regardless of whether you use the-i parameter. However, if you execute the CP in a shell script, you do not ask for overrides if you do not have the-i parameter. This means that the command line and shell scripts perform somewhat differently.


1. command format:

Usage:

CP [Options] ... [-t] source purpose

OR: CP [options] ... Source... Directory

OR: CP [options] ...-t directory source ...

2. Command function:

Copy the source file to the destination file, or copy multiple source files to the destination directory. directive English original meaning: copy

3. Command parameters:

-A,--archive equals-DR--preserve=all Archive Replication * * *

Same AS-DR--preserve=all

Create a backup of each existing target file

-B like--backup but does don't accept an argument

Similar to--backup but does not accept parameters

--copy-contents copy contents of special files when recursive

In recursive processing is to copy special file contents

-d Same as--no-dereference--preserve=links

-F,--force if an existing destination file cannot be opened, remove it and try again

If the destination file cannot be opened, remove it and try again (you do not need to select this when the-n option exists)

-I,--interactive prompt before overwrite (Overrides a previous-n option)

Ask before overwriting (invalidates the previous-n option)

-H Follow command-line symbolic links in SOURCE

Follow command-line symbolic links in the source file

-L,--link link files instead of copying

do not copy linked files

-L,--dereference always follow symbolic links in SOURCE * * *

Always follow the symbolic link to copy the file that the link points to

-N,--no-clobber do not overwrite an existing file (Overrides a previous-i option)

Do not overwrite existing files (invalidate the previous-i option)

-P,--no-dereference never follow symbolic links in SOURCE * * *

Do not follow the symbolic link in the source file, Keep copying this link

-p Same as--preserve=mode,ownership,timestamps

equals--preserve= mode, ownership, timestamp

--preserve[=attr_list] Preserve the specified attributes (default:

mode,ownership,timestamps),

Keep the specified property (default: Mode, ownership, timestamp),

If possible additional attributes:context, links, xattr

If you can keep attached properties: environment, links, xattr, etc.

-R,-R,--recursive copy directories recursively

Copy all items in a directory and directory

-S,--symbolic-link
Make symbolic links instead of copying

Generate symbolic links instead of copying files


4. Command instance:

Instance one: Copy a single file to the destination directory, the file does not exist in the destination file

Command:

CP Log.1 Test1

Description

when the-a parameter is not taken, the time of the two files is different. When the-a parameter is taken, the time of the two files is consistent.


Instance two: When the target file is present, it asks whether to overwrite

Command:

CP log.2 Test2

Description

When the destination file exists, it asks whether to overwrite it. This is because the CP is an alias for Cp-i. When the destination file is present, even if the-f flag is added, the overwrite is also queried.



Example three: Copying the entire directory

Command: cp-a log3.log test3

Description

Note whether the destination directory exists or not, the result is different. When the destination directory is present, the entire source directory is copied to the target directory.

Example four: Copy the Log4.log to create a link file Log4_link.log

Command:

Description

The log_link.log is caused by the-s parameter and is created as a "shortcut", so you will see at the far right of the file where the file is "linked" to!


example five: Replication Link File

Command: CP- l /etc/rc/tmp/rc.2 copy link to file

CP- p /etc/rc/tmp/rc.3 keep copying this links

Description

Note whether the destination directory exists or not, the result is different. When the destination directory is present, the entire source directory is copied to the target directory.


[[email protected] tmp]# CP- l /etc/rc/tmp/rc.2
[email protected] tmp]# LL
Total 8
-rwxr-xr-x. 1 root root 2617 Oct 7 09:14 RC
-rwxr-xr-x. 1 root root 2617 Oct 7 09:23 rc.2
[[email protected] tmp]# CP- p /etc/rc/tmp/rc.3
[email protected] tmp]# LL
Total 8
-rwxr-xr-x. 1 root root 2617 Oct 7 09:14 RC
-rwxr-xr-x. 1 root root 2617 Oct 7 09:23 rc.2
lrwxrwxrwx. 1 root root 7 Oct 7 09:23 rc.3-RC.D/RC



This article is from the "Wind No Trace" blog, please be sure to keep this source http://wangfx.blog.51cto.com/1697877/1859050

Linux command: CP

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.