How To Use SCP to Copy From Local to Remote or vice-a-versa In Linux?

There are various ways to copy a file or directory from remote to local or local to the remote machine. One of them is by using the SCP command.

SCP stands for Secure copy which is a remote file copy program available in Linux or Unix systems. It copies files between hosts on a network.

This is based on SSH, and uses the same authentication and security as SSH.

NOTE:
SCP asks for a password if it needed for authentication. While copying a file, if the file already exists on the remote system then it will replace the content of that file.

Syntax of SCP Command

The syntax of scp is similar to cp command which is used locally for copying a file –

The basic SCP syntax is as below (without the square bracket):

scp [option] [[email protected]_host:/path/to/local/file_or_directory] [[email protected]_host:/path/to/remote/dir_or_filename]

If the username is same on both remote and local you can drop the username from the command:

scp [option] [local_host:/path/to/local/file_or_directory] [remote_host:/path/to/remote/dir_or_filename]

Some Examples of using SCP

Copying a file to a host

scp [option] [[email protected]_host:/path/to/local/file] [[email protected]_host:/path/to/remote/dir]

Copying a directory from the remote host:

scp -r [email protected]:/remote/source_dir local_target_dir

If the remote host uses a port other than 22 then it can be specified in the command

scp -p port_no [[email protected]_host:/path/to/local/file] local_target_dir

Options used with scp command

Options Descriptions
-P Specifies the port to connect on the remote host
-p Preserves the modification time, access time and modes from the original file
-r Recursively copy entire directories
-v Enables verbose mode, This is helpful in debugging connections, authentication and configuration problem
-T Disable the strict filename checking
-4 Forces scp to use IPv4 addresses
-6 Forces scp to use IPv6 addresses only
-B Select batch mode (It prevents asking password or passphrases)
-C Use this option to enable compression
-c Select the cipher to use for encrypting the data transfer
-F Specifies an alternative per-user configuration file for SSH
-i Select the file from which the identity (private key) for public-key authentication is read
-l Limits the used bandwidth specified in Kbit/s
-o Use to pass options to SSH in the format used in SSH_config

 

Using SCP to Copy Files

While sopying and file or directory you just need to understand which is the source and which is the destination machine.
Accordingly place the host and path information in the SCP command.

SCP From Local to Remote

Below are several examples that shows how to copy files or directories from local to remote Linux using SCP.

Now let’s look at the direction and understand the source and destination.

Local -> Remote
Source is Local machine and Destination is Remote machine

Copy a single file

To copy a file with the name let’s say file.txt to remote system’s /home directory, use the following command in your terminal.

scp /home/file.txt [email protected]:/home

Copying multiple files

Instead of copying the whole directory containing multiple files. You can copy more than one file by using the following command.

scp /home/file1.txt file2.txt file3.txt [email protected]:/home

Copying a folder

Now if you want to copy a directory name dir1 from local to the remote system then use the following command in your terminal.

scp -r /home/dir1 [email protected]:/home/
Important is the -r option.

SCP From Remote to Local

Below are several examples that shows how to copy files or directories from remote to local Linux machine using SCP.

Now let’s look at the direction and understand the source and destination.

Remote -> Local
Source is Remote machine and Destination is Local machine

Copying a single file

Now you want to copy a file name file.txt from home directory of the remote system to local system’s /home directory.

Use the following command in your terminal.

scp [email protected]:/home/file.txt /home/

Copying multiple files

Similarly, if you want to copy more than one file without copying the whole directory from remote to the local machine. Then you should use the following command in your terminal.

scp [email protected]:/home/file1.txt file2.txt file3.txt /home/

Copying a folder

Now if you want to copy a directory name dir1 from the remote system to local, then use the following command in your terminal:

scp -r [email protected]:/home/ /home/dir1
Important is the -r option.

Copying file to a specific port

If you want to use a specific SSH port (if not 22) then use option -P (in uppercase) in your command. Use the following command in your terminal.

scp -P 21 [email protected]:/home/file.txt /home/
Important is the -P option.

Conclusion

At times it may feel like the SCP command is a little hard to pick up.

The more you use it, the more you get familiar with this command.

To know more about scp and the options see the manual page of scp. To display it use the following command in your terminal:

man scp

You can always leave a comment if you need any help from me.

Leave a Comment