Transferring files between computers is a common task in networked environments. For those using Unix or Linux systems, the SCP (Secure Copy Protocol) command is a powerful and secure way to copy files and directories across a network. In this article, we will introduce the SCP command, its syntax, options, and practical usage, as well as troubleshooting tips and alternatives.

 

What is SCP Command SSH?

The SCP command is a network protocol that uses SSH (Secure Shell) to securely transfer files between Unix or Linux systems. Unlike the standard CP (copy) command, SCP encrypts both the files and the authentication credentials, ensuring that data remains secure during transit. This encryption makes SCP a preferred method for transferring sensitive data over potentially insecure networks. If you want to know more about What is SSH command, you can check our blog on this topic. 

 

SCP Command Syntax

Understanding the syntax of the SCP command is essential for effectively using it. The basic syntax is:

scp [option] [user_name@source_host:path/to/source/file] [user_name@target_host:target/path]

Here’s a breakdown of the components:

  • scp: The command itself.

  • [option]: Optional flags to modify the command's behavior.

  • [user_name@source_host:path/to/source/file]: The source file's location, including the username and host (if remote).

  • [user_name@target_host:target/path]: The target location for the copied file, including the username and host (if remote).

If the user_name is omitted, the command defaults to the current user. If the paths are omitted, SCP assumes the files are located on the local machine.



SCP Command Windows Options

The SCP command includes numerous options to customize and optimize file transfers. Here are some of the most commonly used options explained in simpler terms:

  • -1: Forces the use of SSH protocol version 1.

  • -2: Forces the use of SSH protocol version 2, which is more secure.

  • -4: Restricts SCP to use only IPv4 addresses.

  • -6: Restricts SCP to use only IPv6 addresses.

  • -B: Enables batch mode, which prevents the command from asking for any user input during the transfer.

  • -b [buffer_size]: Sets the buffer size for data transfer. By default, this is usually set to 32 KB.

  • -C: Compresses the data as it is being transferred, which can speed up the process over slower connections.

  • -c [cipher]: Specifies the cipher to be used for encrypting the data transfer. The default is generally a standard, secure cipher.

  • -D [debug_level]: Sets the debug level, where higher values produce more detailed output useful for troubleshooting.

  • -d: Ensures that the destination directory exists before attempting to copy the file.

  • -F [file]: Uses a specific SSH configuration file instead of the default.

  • -h: Displays help information for the SCP command and its options.

  • -i [file]: Uses a specific identity file (private key) for public key authentication.

  • -l [limit]: Limits the bandwidth used during the transfer, specified in kilobits per second.

  • -o [ssh_option]: Allows setting custom SSH options in the same way they are defined in the SSH configuration file.

  • -P [port]: Specifies a non-default port for the SSH connection (default is 22).

  • -q: Enables quiet mode, which suppresses non-error messages.

  • -Q: Disables the display of file transfer statistics.

  • -r: Recursively copies entire directories and their contents.

  • -S [program]: Specifies the program used for the encrypted connection (typically SSH).

  • -u: Deletes the source file after it has been successfully copied.

  • -v: Enables verbose mode, which provides detailed information about the transfer process, useful for debugging.



How to Use SCP Command to Copy File from Local to Remote

Copying a file from a local machine to a remote server using SCP is straightforward. Here’s an example command:

scp Desktop/sample_example.txt [email protected]:/home/remote_dir

Copy a file from a local server to a remote server using the scp command.

In this command:

  • Desktop/sample_example.txt is the source file on the local machine.

  • [email protected] specifies the username (root) and the IP address of the remote server.

  • /home/remote_dir is the target directory on the remote server where the file will be copied.

This command will securely transfer the specified file to the target directory on the remote server. If you want to learn how to copy folder/directory in Linux, check our article about this topic. 

 

SCP - r Command in Linux

The -r option allows you to copy directories recursively, which is useful for transferring entire directories with their contents. Here’s how to use SCP command Linux:

scp -r example [email protected]:/home/remote_dir

In this command:

  • -r enables recursive copying.

  • example is the directory on the local machine.

  • [email protected]:/home/remote_dir specifies the username, IP address, and target directory on the remote server.

This command will copy the entire example directory and its contents to the specified directory on the remote server.

 

How to Copy Multiple Files with SCP Command Mac

You can use SCP to copy multiple files in a single command by listing the files sequentially. Here’s an example:

scp example/sample1.txt example/sample2.txt [email protected]:/home/remote_dir

Copy multiple files from local server to remote server using the scp command.

In this command:

  • example/sample1.txt and example/sample2.txt are the files to be copied.

  • [email protected]:/home/remote_dir specifies the username, IP address, and target directory on the remote server.

This command will copy both files to the specified directory on the remote server. You can check another article from us if you want to learn how to Move Files in Linux

 

SCP Command Examples

Understanding how to use the SCP command line can be greatly enhanced by looking at practical Linux SCP command examples. Here are some common scenarios where SCP is used to copy files and directories.

 

1. Copy a File from Local to Remote Server

To copy a file from your local machine to a remote server, use the following command:

scp Desktop/sample_example.txt [email protected]:/home/remote_dir

In this example:

  • Desktop/sample_example.txt is the file on your local machine.

  • [email protected] is the username and IP address of the remote server.

  • /home/remote_dir is the destination directory on the remote server.

 

2. Copy a File from Remote Server to Local Host

To copy a file from a remote server to your local machine, use:

scp 147.182.143.27:/home/remote_dir/sample_example.txt home/Desktop

Copy a file from a remote server to a local server using the scp command.

Here:

  • 147.182.143.27:/home/remote_dir/sample_example.txt specifies the remote server's file path.

  • home/Desktop is the local directory where the file will be copied.

 

3. Copy a File Between Two Remote Servers

To copy a file directly between two remote servers:

scp [email protected]:/home/remote_dir/sample_example.txt [email protected]:home/Desktop

In this command:

  • [email protected]:/home/remote_dir/sample_example.txt is the source file on the first remote server.

  • [email protected]:home/Desktop is the destination directory on the second remote server.

 

4. Copy Multiple Files

To copy multiple files from your local machine to a remote server:

scp example/sample1.txt example/sample2.txt [email protected]:/home/remote_dir

This example shows:

  • example/sample1.txt and example/sample2.txt are the local files to be copied.

  • [email protected]:/home/remote_dir is the destination directory on the remote server.

 

5. Copy a Directory Recursively

To copy an entire directory and its contents recursively:

scp -r example [email protected]:/home/remote_dir

Here:

  • -r enables recursive copying.

  • example is the local directory.

  • [email protected]:/home/remote_dir is the remote directory where the files will be copied.

 

6. Copy a File Using a Specific Port

To use a specific port (other than the default port 22):

scp -P 2222 Desktop/sample_example.txt [email protected]:/home/remote_dir

In this command:

  • -P 2222 specifies port 2222.

  • Desktop/sample_example.txt is the local file.

  • [email protected]:/home/remote_dir is the remote destination.

 

7. Copy a File in Quiet Mode

To copy a file without showing progress and non-error messages:

scp -q Desktop/sample_example.txt [email protected]:/home/remote_dir

Here:

  • -q enables quiet mode.

  • Desktop/sample_example.txt is the local file.

  • [email protected]:/home/remote_dir is the remote destination.

 

8. Copy a File in Verbose Mode

To copy a file and see detailed debugging information:

scp -v Desktop/sample_example.txt [email protected]:/home/remote_dir

Copy a file using the scp command in verbose mode.

This command:

  • -v enables verbose mode.

  • Desktop/sample_example.txt is the local file.

  • [email protected]:/home/remote_dir is the remote destination.

 

9. Limit Bandwidth During File Transfer

To limit the bandwidth used by SCP:

scp -l 800 Desktop/sample_example.txt [email protected]:/home/remote_dir

In this case:

  • -l 800 limits the bandwidth to 800 Kbit/s.

  • Desktop/sample_example.txt is the local file.

  • [email protected]:/home/remote_dir is the remote destination.

 

10. Speed Up File Transfer with Compression

To compress files during transfer to speed up the process:

scp -C Desktop/sample_example.txt [email protected]:/home/remote_dir

Here:

  • -C enables compression.

  • Desktop/sample_example.txt is the local file.

  • [email protected]:/home/remote_dir is the remote destination.




SCP Command Not Found?

If you encounter the error "scp command not found," it likely means that SCP command Powershell is not installed on your system. On Linux, you can install it using your package manager. For example, on Debian-based systems, use:

sudo apt-get install openssh-client

On Red Hat-based systems, use:

sudo yum install openssh-clients

For macOS, SCP is usually included with the operating system. If not, you can install it using Homebrew:

brew install openssh

 

How to Add Password to SCP Command?

For security reasons, SCP does not allow passwords to be included directly in the command. Instead, you should use SSH keys for authentication. Here’s how to set up passwordless authentication:

1. Generate an SSH key pair on your local machine:

ssh-keygen

2. Copy the public key to the remote server:

ssh-copy-id user@remote_host

Once set up, you can use SCP without entering a password each time.

 

SCP Command Alternatives

While SCP is widely used, there are several alternatives for secure file transfer:

  • rsync: More efficient for large file transfers as it only copies differences between source and destination.

  • sftp: Uses the same protocol as SCP but provides a more interactive interface.

  • rclone: Supports various cloud storage services in addition to local and remote file transfers.

  • FTP/FTPS: An older protocol for file transfers, with FTPS adding SSL/TLS encryption.

 

Final Words

The SCP command is an invaluable tool for anyone working with remote servers, providing a simple and secure method for transferring files across different systems. By understanding its various options and syntax, you can customize file transfers to suit specific needs, such as using different ports, enabling compression, or limiting bandwidth. In this article, we provided SCP command in Linux examples and other operating systems to help you understand how these commands work.