One Linux command per day (8): CP command

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. 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 (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,--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

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

[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.

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!

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.