Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux scp Command

Linux scp Command

Linux Command Reference Linux Command Reference

The Linux scp command is used to copy files and directories between Linux systems.

scp is short for secure copy. scp is a command for secure remote file copying over ssh login on Linux systems.

scp is encrypted,rcp is not encrypted; scp is an enhanced version of rcp.

On Linux systems,scp command is a very useful tool scp is short for "secure copy". It is based on the SSH (Secure Shell) protocol, ensuring the security of data transfer.

Syntax



scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]

[-l limit] [-o ssh_option] [-P port] [-S program]

[[user@]host1:]file1 [...] [[user@]host2:]file2

Simple syntax:



scp [选项] [源文件] [目标路径]

Common options:

Here are some commonly used scp Command options:

Full option description:

Copy files from local to a remote host

Suppose you have a file example.txt, and you want to copy it to the /home/user/ directory on the remote host, you can use the following command:



scp example.txt user@remote_host:/home/user/

In this command:

Copy files from a remote host to local

If you want to copy a file on a remote host example.txt to the current local directory, you can use the following command:



scp user@remote_host:/home/user/example.txt .

In this command:

Recursively copy directories

If you want to copy an entire directory and its contents, you can use the -r option. For example, copy the local my_directory directory to the remote host's /home/user/ directory:



scp -r my_directory user@remote_host:/home/user/

Specify the port number

If the remote host's SSH service runs on a non-default port (for example, 2222), you can use the -P option to specify the port number:



scp -P 2222 example.txt user@remote_host:/home/user/

Preserve file attributes

If you want to preserve the modification time, access time, and permissions of files when copying, you can use -p Option:



scp -p example.txt user@remote_host:/home/user/

Enable compression

For large files or slow networks, you can use the -C option to enable compression to speed up transfer:



scp -C large_file.zip user@remote_host:/home/user/


Application Example

1. Copy from local to remote

Command format:



scp local_file remote_username@remote_ip:remote_folder 

或者 

scp local_file remote_username@remote_ip:remote_file 

或者 

scp local_file remote_ip:remote_folder 

或者 

scp local_file remote_ip:remote_file 

Application examples:



scp /home/space/music/1.mp3 root@www.runoob.com:/home/root/others/music 

scp /home/space/music/1.mp3 root@www.runoob.com:/home/root/others/music/001.mp3 

scp /home/space/music/1.mp3 www.runoob.com:/home/root/others/music 

scp /home/space/music/1.mp3 www.runoob.com:/home/root/others/music/001.mp3 

Command format for copying directories:



scp -r local_folder remote_username@remote_ip:remote_folder 

或者 

scp -r local_folder remote_ip:remote_folder 

Application examples:



scp -r /home/space/music/ root@www.runoob.com:/home/root/others/ 

scp -r /home/space/music/ www.runoob.com:/home/root/others/ 

The above command copies the local music directory to the remote others directory.

2. Copy from remote to local

To copy from remote to local, simply swap the order of the last two parameters of the local-to-remote command, as in the following examples

Application examples:



scp root@www.runoob.com:/home/root/others/music /home/space/music/1.mp3 

scp -r www.runoob.com:/home/root/others/ /home/space/music/

Description

1. If the remote server's firewall has set a specific port for the scp command, we need to use the -P parameter to set the command's port number. The command format is as follows:



# scp command using port 4588

scp -P 4588 remote@www.runoob.com:/usr/local/sin.sh /home/administrator

2. When using the scp command, make sure the user has permission to read the corresponding files on the remote server; otherwise, the scp command will not work.


Notes

Linux Command Reference Linux Command Reference