Linux---SCP, cp command __linux

Source: Internet
Author: User
Tags file copy mongodb scp command secure copy

The SCP is secure copy, a command that is used to remotely copy files under a Linux system. The CP command is similar to it, but the CP is only copied on the local computer and not across the server, and the SCP transport is encrypted.
Cp-is mainly used on the same computer, in different directories between the copy files.
scp-is a secure remote file copy command based on SSH login under Linux. The SCP commands for Linux can replicate files and directories between Linux servers.

about SCP
1. Format
SCP [parameters] [Original path] [Target path]

2. Parameter
-1
Force SCP Command Usage protocol SSH1
-2
Force SCP Command Usage protocol SSH2
-4
Force SCP command to use only IPV4 addressing
-6
Force SCP command to use only IPV6 addressing
-B
Use batch mode (transfer password or phrase not queried during transport)
-C
Allow compression. (Pass the-c flag to SSH to turn on the compression feature)
-P
Keep the original file modification time, access time and access rights.
-Q
The transport progress bar is not displayed.
-R
Recursively copies the entire directory.
-V
Display the output in detail. SCP and SSH (1) will display debugging information for the entire process. This information is used to debug connections, verify, and configure problems.
-C cipher
This option will be passed directly to SSH by encrypting the data in cipher.

3. Application
(1) Copy from local server to remote server
Copy File Command format:
SCP Local_file Remote_username@remote_ip:remote_folder
Or
SCP Local_file Remote_username@remote_ip:remote_file
Or
SCP Local_file Remote_ip:remote_folder
Or
SCP Local_file Remote_ip:remote_file

1th, 2 specified user name, the command to enter the user password after the execution, the 1th specifies only the remote directory, the file name is unchanged, and the 2nd specifies the filename; 3rd, 4 do not specify a username, the command needs to enter the user name and password, 3rd only specified a remote directory, file name unchanged, The 4th specifies a filename.

Instance:
Scp/home/linux/soft/scp.zip Root@www.mydomain.com:/home/linux/others/soft
Scp/home/linux/soft/scp.zip Root@www.mydomain.com:/home/linux/others/soft/scp2.zip
Scp/home/linux/soft/scp.zip Www.mydomain.com:/home/linux/others/soft
Scp/home/linux/soft/scp.zip Www.mydomain.com:/home/linux/others/soft/scp2.zip

Copy Directory command format:
Scp-r Local_folder Remote_username@remote_ip:remote_folder
Or
Scp-r Local_folder Remote_ip:remote_folder
The 1th specifies the user name, the command needs to enter the user password after execution;
The 2nd does not specify the user name, the command needs to enter the user name and password after execution;
Example:
Scp-r/home/linux/soft/root@www.mydomain.com:/home/linux/others/
Scp-r/home/linux/soft/www.mydomain.com:/home/linux/others/

(2) Copy from remote server to local server

Example 1, copying files from a distance to a local directory
Command:
SCP root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz/opt/soft/
Role:
Download nginx-0.5.38.tar.gz files from the/opt/soft/directory on the 192.168.120.204 machine to the local/opt/soft/directory

Example 2, copying directories from afar to local
Command:
Scp-r root@192.168.120.204:/opt/soft/mongodb/opt/soft/
Role:
Download the MongoDB directory from the/opt/soft/on the 192.168.120.204 machine to the local/opt/soft/directory

Example 3, upload local files to the remote machine specified directory
Command:
Scp/opt/soft/nginx-0.5.38.tar.gz Root@192.168.120.204:/opt/soft/scptest
Role:
Copy the files from the local opt/soft/directory nginx-0.5.38.tar.gz to the Opt/soft/scptest directory of the remote machine 192.168.120.204

Example 4, upload local directory to remote machine specified directory
Command:
Scp-r/opt/soft/mongodb Root@192.168.120.204:/opt/soft/scptest
Role:
Upload the local directory/opt/soft/mongodb to the/opt/soft/scptest directory on the remote machine 192.168.120.204.

about CP
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 root    0 10-28 14:48
drwxr-xr-x 6 root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
2 root R  oot 4096 10-28 14:53 test5
[root@localhost test]# cd test5
[root@localhost 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 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 root 0 10-28 14:46 Log5-1.log-
R w-r--r--1 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 ro OT 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.
Example three: Copying an entire directory
Command:
Output:
When the target directory exists:

[Root@localhost test]# cp-a test3 test5 
[root@localhost test]# ll
-rw-r--r--1 root root    0 10-28 14:48 Log.lo G
drwxr-xr-x 6 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
[root@localhost test]# cd test5/
[root@localhost 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:4 7 Test3

The destination directory does not exist is:

[Root@localhost test]# cp-a test3 test4
[root@localhost 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 4096 10-28 15:11 test5
[root@localhost test]#

Description:
Note Whether the destination directory exists or not, the results are different. When the target directory exists, the entire source directory is copied to the destination directory.

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.