One Linux command per day (8): CP command

Source: Internet
Author: User

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 target file already exists, it asks whether to overwrite it, whether or not 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.

3. Command parameters:

-a,–archive equals-dr–preserve=all.

–backup[=control Create a backup for each existing target file

-B similar to –backup but does not accept parameters

–copy-contents in recursive processing is to copy special file contents

-D equals –no-dereference–preserve=links

-f,–force If the destination file cannot be opened, remove it and try again (when the-n option

There is no need to select this option)

-i,–interactive Ask before overwriting (invalidates the previous-n option)

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

-l,–link Linked files without copying

-l,–dereference always follow 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 –preserve= mode, ownership, timestamp

The –preserve[= property list retains the specified property (default: Mode, ownership, timestamp), if

May keep attached properties: environment, links, xattr, etc.

-R,-r,–recursive copy all items in directory and directory

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.log TEST5

Output:

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:

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:

Output:

When the destination directory exists:

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 Log.log to create a link file Log_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!

One Linux command per day (8): CP command

Related Article

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.