Linux Base Command (8) CP command

Source: Internet
Author: User
Tags backup linux

The CP command is used to copy files or directories and is one of the most commonly used commands in Linux systems. Typically, the shell sets an alias, and when the file is copied at the command line, if the target file already exists, it asks if it is overwritten, regardless of whether or not you use the-i parameter. However, if the CP is executed in a shell script, there is no-I argument without asking whether or not to overwrite. This shows how the command line and shell scripts are executed 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 is similar to--backup but does not accept parameters

--copy-contents in recursive processing is the copying of special file contents

-D equals--no-dereference--preserve=links

-F,--force remove the target file if it cannot be opened and try again (when-n option

Does not need to be selected when it is present

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

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

-L,--link linked file without copying

-L,--dereference always follow symbolic links

-N,--no-clobber do not overwrite existing files (make the previous-I option invalid)

-P,--no-dereference does not follow symbolic links in source files

-P equals--preserve= mode, ownership, timestamp

--The Preserve[= property list retains the specified attribute (default: Mode, ownership, timestamp), if

May retain additional properties: environment, links, xattr, etc.

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

4. Command instance:

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

Command:

CP Log.log TEST5

Output:

[Root@localhost test]# CP Log.log TEST5

[Root@localhost test]# LL

-rw-r--r--1 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

[Root@localhost test]# CD TEST5

[Root@localhost test5]# LL

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

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

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

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

Description

The time of two files is not the same when the-a argument is not taken. When the-a parameter is taken, the time for the two files is the same.

Instance two: When the target file exists, it asks if the overwrite

Command:

CP Log.log TEST5

Output:

[Root@localhost test]# CP Log.log TEST5

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

[Root@localhost test]# cp-a log.log test5

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

[Root@localhost test]# CD test5/

[Root@localhost test5]# LL

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

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

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

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

Description

When the destination file exists, it asks if it is overwritten. This is because the CP is the alias of the Cp-i. When the target file exists, it is queried for overwrite even if the-f flag is added.

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.