Linux Learning Note 7---command CP

Source: Internet
Author: User
Tags log log

The CP command is used to copy files or directories and is one of the most commonly used commands in a Linux system.

The CP command is used to copy one or more source files or directories to a specified destination file or directory. It can copy a single source file into a specific file with a specified file name or a directory that already exists. The CP command also supports copying multiple files at once, and when copying multiple files at a time, the target file parameter must be a directory that already exists, or an error will occur.

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 creates a backup for each existing target file, and the new file or directory has the same creation time as the original file or directory.

-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 before overwriting (invalidates the previous-n option)

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

-L: Make a hard connection to the source file instead of copying the file;

-S: Create a symbolic connection to the source file instead of copying the file;

-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. -P: Preserves the properties of the source file or directory;

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:

[email protected] test]# CP log.log TEST5

[email protected] test]# LL

-rw-r--r--1 root root 0 10-28 14:48 log.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 10-28 14:47 test3

Drwxr-xr-x 2 root root 4096 10-28 14:53 test5

[Email protected] test]# CD TEST5

[email protected] test5]# LL

-rw-r--r--1 root root 0 10-28 14:46 log5-1.log

-rw-r--r--1 root root 0 10-28 14:46 log5-2.log

-rw-r--r--1 root root 0 10-28 14:46 log5-3.log

-rw-r--r--1 root root 0 10-28 14:53 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]# CP log.log TEST5

CP: Do you want to overwrite "Test5/log.log"? N

[Email protected] test]# cp-a log.log test5

CP: Do you want to overwrite "Test5/log.log"? Y

[Email protected] test]# CD test5/

[email protected] test5]# LL

-rw-r--r--1 root root 0 10-28 14:46 log5-1.log

-rw-r--r--1 root root 0 10-28 14:46 log5-2.log

-rw-r--r--1 root root 0 10-28 14:46 log5-3.log

-rw-r--r--1 root root 0 10-28 14:48 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]st test]# cp-a test3 test5

[email protected] test]# LL

-rw-r--r--1 root root 0 10-28 14:48 log.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 10-28 14:47 test3

Drwxr-xr-x 3 root root 4096 10-28 15:11 test5

[Email protected] test]# CD test5/

[email protected] test5]# LL

-rw-r--r--1 root root 0 10-28 14:46 log5-1.log

-rw-r--r--1 root root 0 10-28 14:46 log5-2.log

-rw-r--r--1 root root 0 10-28 14:46 log5-3.log

-rw-r--r--1 root root 0 10-28 14:48 log.log

DRWXRWXRWX 2 root root 4096 10-28 14:47 test3

The destination directory does not exist when:

[Email protected] test]# cp-a test3 test4

[email protected] test]# LL

-rw-r--r--1 root root 0 10-28 14:48 log.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 10-28 14:47 test3

DRWXRWXRWX 2 root root 4096 10-28 14:47 test4

Drwxr-xr-x 3 root root 4096 10-28 15:11 test5

[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. If the target directory does not exist, the entire source directory is copied to a new directory, a bit similar to copy one, but must be added to the parameter-a, otherwise it cannot be copied.

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]# LL

lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log log.log

-rw-r--r--1 root root 0 10-28 14:48 log.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 10-28 14:47 test3

DRWXRWXRWX 2 root root 4096 10-28 14:47 test4

Drwxr-xr-x 3 root root 4096 10-28 15:11 test5

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!

When we use the CP command to copy files under Linux, sometimes we will need to overwrite some files with the same name, when overwriting the file, we will have to keep pressing Y to determine the execution overwrite. The number of files is not much good, but if hundreds of estimates press Y to vomit blood, so toss to a long time summed up a method: CP aaa/*/bbb Copy directory AAA under All to/BBB directory, at this time if the/BBB directory has the same name as AAA file, You need to press Y to confirm and skip the subdirectories under the AAA directory. Cp-r aaa/*/bbb This time still needs to press Y to confirm the operation, but does not ignore subdirectories. Cp-r-a aaa/*/bbb still needs to press Y to confirm the operation, and the AAA directory and subdirectories and file attributes are also passed to the/bbb. \cp-r-a aaa/*/bbb successful, no prompt pressed Y, passed the Directory property, did not skip the directory.

I use the CP command:

1.CP sourcefile dirname: Copy a file to the directory diraname below.

2.CP dir/* dir: Copy all files under one directory to directory diraname.

3.CP-PR dir dir2: Copy all the files under a folder to the directory Dir2 below.

4.cp-a dir1 dir2: Copy a file to the directory Diraname below, and the folder under the new path has the same properties as the source file.

If the content is written in soft and hard-linked files, the source file changes.

Linux Learning Note 7---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.