Linux Common commands: 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 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.

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 (you do not need to select this when the-N option exists)

-I,--interactive before overwriting (invalidates the previous-n option)

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

-L,--link link files without copying

-L,--dereference always follow the symbolic link

-N,--no-clobber do not overwrite existing files (invalidates 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 additional properties may be maintained: 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:

[Email protected] test]#CPlog.log Test5[[email protected] test]# ll-rw-r--r--1Root root0 Ten- -  -: -LOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -TEST3DRWXR-xr-x2Root root4096 Ten- -  -: -Test5[[email protected] test]# CD Test5[[email protected] test5]# ll-rw-r--r--1Root root0 Ten- -  -: $log5-1. Log-rw-r--r--1Root root0 Ten- -  -: $log5-2. Log-rw-r--r--1Root root0 Ten- -  -: $log5-3. Log-rw-r--r--1Root root0 Ten- -  -: -Log.log

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

Output:

[Email protected] test]#CPLog.log test5CP: Do you want to overwrite "Test5/log.log"?N[[email protected] test]#CP-a log.log test5CP: Do you want to overwrite "Test5/log.log"?Y[[email protected] test]# CD TEST5/[email protected] test5]# ll-rw-r--r--1Root root0 Ten- -  -: $log5-1. Log-rw-r--r--1Root root0 Ten- -  -: $log5-2. Log-rw-r--r--1Root root0 Ten- -  -: $log5-3. Log-rw-r--r--1Root root0 Ten- -  -: -Log.log

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:

[Email protected] test]#CP-a test3 test5 [[email protected] test]# ll-rw-r--r--1Root root0 Ten- -  -: -LOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -TEST3DRWXR-xr-x3Root root4096 Ten- -  the: OneTest5[[email protected] test]# CD TEST5/[email protected] test5]# ll-rw-r--r--1Root root0 Ten- -  -: $log5-1. Log-rw-r--r--1Root root0 Ten- -  -: $log5-2. Log-rw-r--r--1Root root0 Ten- -  -: $log5-3. Log-rw-r--r--1Root root0 Ten- -  -: -log.logdrwxrwxrwx2Root root4096 Ten- -  -: -Test3

When the destination directory does not exist:

[Email protected] test]#CP-a test3 test4[[email protected] test]# ll-rw-r--r--1Root root0 Ten- -  -: -LOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -test3drwxrwxrwx2Root root4096 Ten- -  -: -TEST4DRWXR-xr-x3Root root4096 Ten- -  the: OneTest5[[email protected] test]#

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:

Cp-s Log.log Log_link.log

Output:

[Email protected] test]#CP-s Log.log log_link.log[[email protected] test]# lllrwxrwxrwx1Root root7 Ten- -  the: -Log_link.logLog.log-rw-r--r--1Root root0 Ten- -  -: -LOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -test3drwxrwxrwx2Root root4096 Ten- -  -: -TEST4DRWXR-xr-x3Root root4096 Ten- -  the: OneTest5

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!

Linux Common commands: CP command

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.