Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the cp and scp commands for replication on Linux systems

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use the cp and scp commands used for replication in the Linux system". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the cp and scp commands for replication in the Linux system.

Cp command

Usage: cp [option]. [- T] Source file target file

Or: cp [options]... Source file. Catalogue

Or: cp [options]... -t directory source file.

Copy the source file to the destination file, or copy multiple source files to the destination directory.

The parameters that must be used for long options are also required for short options.

-a,-- archive equals-dR-- preserve=all

-- backup [= CONTROL creates a backup for each existing target file

-b is similar to-- backup but does not accept parameters

-- copy-contents copies the contents of special files in recursive processing

-d equals-- no-dereference-- preserve=links

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

Do not need to select this option if it exists)

-I,-- ask before interactive override (invalidate the previous-n option)

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

-l,-- link links the file without copying it

-L,-- dereference always follows symbolic links

-n,-- no-clobber do not overwrite existing files (invalidate the previous-I option)

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

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

-- preserve [= attribute list keeps the specified attributes (default: mode, ownership, timestamp), if

Additional attributes may be maintained: environment, links, xattr, etc.

-c same as-- preserve=context

The sno-preserve= attribute list does not retain the specified file attributes

-- create all directories in the source file path in the destination directory before parents replication

-R,-r,-- recursive recursively copies everything in the directory and its subdirectories

-- reflink [= WHEN] controls the clone / CoW copy. Please check the inner image below.

-- remove-destination deletes an existing destination before attempting to open the destination file

File (relative to the-- force option)

-- sparse=WHEN controls how sparse files are created

-- strip-trailing-slashes deletes the slash at the end of all source files / directories in the parameter

-s,-- symbolic-link only creates symbolic links and does not copy files

-S,-- suffix= suffix specifies the suffix of the backup file

-t,-- target-directory= directory sets the source files / directories specified by all parameters

Copy to destination directory

-T,-- no-target-directory treats the target directory as a normal file

-u,-- update copy only when the SOURCE file is newer

Than the destination file or when the

Destination file is missing

-v,-- verbose explain what is being done

-x,-- one-file-system stay on this file system

-Z,-- context=CONTEXT set security context of copy to CONTEXT

-- help displays this help and exits

-- version displays version information and exits

By default, the sparsity of the source file is judged only by a simple method, and the corresponding target file is also

Is considered sparse. This is because the-- sparse=auto parameter is used by default. If you explicitly use

The sparse=always parameter creates the target file regardless of whether the source file contains a sufficiently long 0 sequence.

Built as sparse parts.

Use the-- sparse=never parameter to disable the creation of sparse files.

Lightweight replication is performed when the-- reflink [= always] parameter is specified, that is, only when the data block is modified

Only replicate under the circumstances. If replication fails or-- reflink=auto is also specified, the standard replication mode is returned.

Backup files are suffixed with "~" unless with the-- suffix option or SIMPLE_BACKUP_SUFFIX

Environment variable assignment. Version control can be done through the-- backup option or the VERSION_CONTROL environment

Variable to select. The following are the available variable values:

None, off does not backup (even if the-- backup option is used)

Numbered, t backup files are sorted with numbers

Existing, nil use numbers if digital backup files already exist, otherwise backup by normal method

Simple, never always back up in the normal way

There is a special case: if both the-- force and-- backup options are specified, while the source and destination files

If it is the same regular file that already exists, cp will back up the source file.

Example:

The code is as follows:

[root@localhost www] # cp-p test.rb / home/test # test.rb copy to the test directory and retain the attributes of the original file

[root@localhost www] # cp-r Dir/ / home/test # copy the Dir directory to the test directory

[root@localhost www] # cp-fr Dir/* / home/test # force all files under Dir to copy to the test directory

[root@localhost www] # cp test.rb {, .bak} # back up test.rb with a .bak suffix at the end

Scp command

SCP command syntax

Scp [- 1245BCpqrv] [- c cipher] [F ssh_config] [- I identity_file] [- l limit] [- o ssh_option] [- P port] [- S program] [[user@] host1:] file1 […] [[suer@] host2:] file2

SCP command description

Scp copies files between hosts. He uses ssh (1) for data transmission. And use the same authentication and security. Scp will request a password during authentication all files may require a special description of the server and user to indicate that the file will be copied to / from a server. File replication between two remotely logged-in servers is allowed.

SCP command options

-1 force scp to use protocol 1

-2 force scp to use protocol 2

-4 force scp to use the URL of IPV4

-6 force scp to use the URL of IPV6

-B Select batch mode (prevent password entry)

-C allows compression. Mark-C to ssh (1) to allow compression

-c cipher selects cipher to encrypt the data transfer. This option is passed directly to ssh (1)

-F ssh_config sets a variable user profile to ssh. This option is passed directly to ssh (1)

-I identity_file selects the file that is certified by RSA to read the private password. This option can be passed directly to ssh (1)

-l limit limits the transmission bandwidth, that is, the speed of the Kbit/s

-o ssh_option can transfer the configuration format from ssh_config to ssh. This mode is useful for illustrating scp that does not have breakers in a separate scp file. The options are as follows. And their value can be found in ssh_config (5)

-P port specifies the connection remote connection port. Note that this option needs to be in uppercase mode. Because-p has already retained the number and mode.

-S program specifies an encryption program. This program must read all ssh (1) options.

-p specify the number of modifications, connections, and mode for the original file

-Q turn off the progress parameters

-r copy the entire folder recursively

-S program specifies an encryption program. This program must read all ssh (1) options.

-V redundancy mode. Have scp and ssh (1) print their troubleshooting information, which is useful in troubleshooting connections, authentication, and configuration.

Example:

The code is as follows:

[root@localhost www] # scp-r tank@192.16.1.108:/var/www/blog / home/www/blog # Log in as tank and copy the server folder locally

[root@localhost www] # scp / home/www/blog/index.php 192.16.1.108:/var/www/blog # copy the local file to the server and enter the user. Default is the current user.

Copy from local to remote server using the specified port

The code is as follows:

[root@localhost test] # scp-P 20022-r.. / vhost/ zhuqiang@192.168.0.200:/home/test/

At this point, I believe you have a better understanding of "how to use the cp and scp commands used for replication in the Linux system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 227

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report